使用jQuery获取完整的URL,包括问号后的参数?

时间:2022-10-05 18:58:56

I am trying to use jQuery to get the current full URL of a page. The URL of a page looks like this:

我正在尝试使用jQuery来获取页面的当前完整URL。页面的URL如下所示:

http://myurl.com/ebook?page=43

http://myurl.com/ebook?page=43

If I use window.location.pathname it only returns everything before the ? so I would just get

如果我使用window.location.pathname它只返回之前的所有内容?所以我会得到

http://myurl.com/ebook

http://myurl.com/ebook

How can I get the entire URL?

如何获取整个网址?

2 个解决方案

#1


32  

You have:

你有:

window.location.href

and also:

并且:

document.URL

#2


5  

If you want to return the whole URL, you need to use window.location.href.

如果要返回整个URL,则需要使用window.location.href。

Using window.location.pathname only returns the path portion of the URL which excludes the query parameters and fragment (i.e., starting at the / following the host but only up to a ? or #).

使用window.location.pathname只返回URL的路径部分,该部分排除了查询参数和片段(即,从主机开始/后面开始,但最多只能到?或#)。

Example:

例:

> window.location.pathname
"/ebook"
> window.location.href
"http://example.com/ebook?page=43"

Also, see the documentation for window.location for more available properties.

另外,请参阅window.location的文档以获取更多可用属性。

#1


32  

You have:

你有:

window.location.href

and also:

并且:

document.URL

#2


5  

If you want to return the whole URL, you need to use window.location.href.

如果要返回整个URL,则需要使用window.location.href。

Using window.location.pathname only returns the path portion of the URL which excludes the query parameters and fragment (i.e., starting at the / following the host but only up to a ? or #).

使用window.location.pathname只返回URL的路径部分,该部分排除了查询参数和片段(即,从主机开始/后面开始,但最多只能到?或#)。

Example:

例:

> window.location.pathname
"/ebook"
> window.location.href
"http://example.com/ebook?page=43"

Also, see the documentation for window.location for more available properties.

另外,请参阅window.location的文档以获取更多可用属性。