用javascript获取当前的URL?

Get the current URL with JavaScript?

我只想得到网站的网址。不是从链接中获取的URL。在页面加载时,我需要能够抓取网站的完整、当前的URL,并将其设置为一个变量,以便按我的意愿进行操作。

  • 可能重复:从URL获取协议、域和端口
  • stackoverflow.com/questions/406192/…


用途:

1
window.location.href

正如注释中所指出的,下面的行可以工作,但它是针对Firefox的。

1
document.URL;

请参阅domstring类型的url,readonly。

  • 在火狐12中,document.URL属性在window.location到锚()后不会更新,而window.location.href会更新。我在这里做了一个实况演示:jsfiddle.net/pxgky我没有测试任何其他版本的火狐。在Chrome20和IE9中没有发现使用document.URL的问题。
  • 您还可以得到主机和清晰的位置:window.location.hostwindow.location.href.toString().split(window.location.host)[‌​1]
  • 那么,document.baseURI是关于什么的。基本上有3种方法可以获得url document.baseURIdocument.URLlocation
  • -1:如果您有一个带有name="URL"的帧、图像或窗体,那么该属性将被隐藏在document对象上,您的代码将中断。在这种情况下,document.URL将引用dom节点。更好地使用全局对象的属性,如window.location.href中所示。
  • "window.location.href"获胜
  • 对于寻找原始信息的人来说,这有点令人困惑(我的意思是必须有一个编辑来显示window.location.href而不是document.URL作为答案)
  • 所有内容:-stackoverflow.com/questions/6944744/…
  • 比较是否需要包含尾随斜杠?比如:..mysite.com/


URL信息访问

javascript为您提供了许多方法来检索和更改当前的URL,这些URL显示在浏览器的地址栏中。所有这些方法都使用Location对象,它是Window对象的属性。您可以创建一个新的Location对象,其当前URL如下:

1
var currentLocation = window.location;

基本URL结构

1
<protocol>//<hostname>:<port>/<pathname><search><hash>
  • 协议:指定用于访问Internet上资源的协议名称。(HTTP(不带SSL)或HTTPS(带SSL))

  • host name:host name指定拥有资源的主机。例如,www.stackoverflow.com。服务器使用主机名提供服务。

  • 端口:一个端口号,用于识别一个特定的进程,当Internet或其他网络消息到达服务器时,它将被转发到该进程。

  • 路径名:该路径提供有关Web客户端要访问的主机内特定资源的信息。例如,/index.html

  • 查询:查询字符串跟随路径组件,并提供资源可用于某些目的的信息字符串(例如,作为搜索的参数或作为要处理的数据)。

  • 哈希:URL的锚定部分,包括哈希符号()。

通过这些Location对象属性,您可以访问所有这些URL组件以及它们可以设置或返回的内容:

  • href-整个URL
  • 协议-URL的协议
  • 主机-URL的主机名和端口
  • hostname-URL的主机名
  • 端口-服务器用于URL的端口号
  • 路径名-URL的路径名
  • 搜索-URL的查询部分
  • hash-URL的锚定部分

我希望你能得到答案。

  • 它们不是window.location的"方法",而是属性,这里我们有一个例子:var stringPathName = window.location.pathname
  • @ FabioC。你可以用substring把它取下来。但是,如果您想使用重定向document.location ="/page.html";将重定向到根页面page.html,它可能很有用。
  • 请记住,ie9 pathname没有一个前导斜杠,所以它可能是index.html
  • 这不仅回答了所说的问题。事实上,我大概在一个月前搜索了一个从URL字符串中获取一个或多个特定部分的好方法(我认为这可能是我试图获取的当前页面),尽管其他问题更符合目标,但它们的答案并不像这个问题那样有用和简单。
  • 不过,有一个简短的建议:在上面描述的基本URL结构中,有一个点用于search,但是在下面的描述列表中,它被称为query。也许他们可以和解,或者可以添加进一步的解释。


使用window.location对与当前帧关联的位置对象进行读写访问。如果只想将地址作为只读字符串获取,可以使用document.URL,它应该包含与window.location.href相同的值。

  • 另请参见stackoverflow.com/questions/2430936/&hellip;


获取当前网页的URL:

1
window.location.href

  • 请注意,这是窗口的位置,而不是文档的位置。
  • 是一样的。完整的当前URL引用文档路径(外部地址)。
  • 它是否像document.url那样标准化?(我是指类似W3C文档的内容)
  • @chendral:w3.org/tr/window/位置
  • document是规范定义的文档树的根。window通常是等效的,但在某些奇怪的情况下可能不会。


要获取路径,可以使用:

1
2
3
console.log('document.location', document.location.href);
console.log('location.pathname',  window.location.pathname); // Returns path only
console.log('location.href', window.location.href); // Returns full URL


打开开发人员工具,在控制台中键入以下内容,然后按Enter键。

1
window.location

例:下面是当前页面上结果的屏幕截图。

enter image description here

从这里拿你需要的东西。:)


用途:window.location.href

如上所述,在更新window.location时,document.URL不更新。参见MSDN。


好的,使用纯JavaScript很容易获得当前页面的完整URL。例如,在此页上尝试此代码:

1
2
3
window.location.href;
// use it in the console of this page will return
// http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"

The window.location.href property returns the URL of the current page.

1
document.getElementById("root").innerHTML ="The full URL of this page is:" + window.location.href;
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>

<body>
  JavaScript
  The window.location.href
  <p id="root">
</p>
</body>

</html>

提到这些也不赖:

如果需要相对路径,只需使用window.location.pathname

如果您想知道主机名,可以使用window.location.hostname

如果需要单独获取协议,只需执行window.location.protocol

另外,如果页面上有hash标签,您可以这样得到:window.location.hash标签。

所以window.locatation.href一次处理完所有的…基本上:

1
2
window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
    //true

同时使用window,如果已经在窗口范围内,则无需使用。

因此,在这种情况下,您可以使用:

1
2
3
4
5
6
7
8
9
location.protocol

location.hostname

location.pathname

location.hash

location.href

Get the current URL with JavaScript


  • 使用window.location.href获取完整的URL。
  • 使用window.location.pathname获取离开主机的URL。

  • window.location.pathname不包括查询和哈希片段


您可以使用以下方法获取具有哈希标记的当前URL位置:

JavaScript:

1
2
3
4
5
 // Using href
 var URL = window.location.href;

 // Using path
 var URL = window.location.pathname;

jQuery:

1
$(location).attr('href');

1
2
3
4
5
6
var currentPageUrlIs ="";
if (typeof this.href !="undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase();
}else{
       currentPageUrlIs = document.location.toString().toLowerCase();
}

上面的代码也可以帮助某人

  • 您将.tolowercase()应用于实际更改它的URL。有些服务器区分大小写(Linux等)
  • 您应该口头使用window.href。此代码仅在this==window的上下文中工作,但是这种情况并不总是如此,尤其是当有人粘贴了此解决方案时。
  • 不是"某些"服务器区分大小写。它是大多数服务器。所以我同意Anthonyvo的观点:改变URL的大小写是一个非常糟糕的主意。
  • 你不能破坏一个网址!!!!如果url是http://www.server.com/path/to/Script.php?option=A1B2C3,如果文件系统区分大小写(linux/unix),则不必使用script.php和script.php。即使不区分大小写(Windows,一些Mac OS),?option=A1B2C3?option=A1B2C3甚至?option=A1B2C3也不相同。只有服务器不区分大小写:www.server.com或www.server.com是相同的。


添加结果以供快速参考

window.location;

1
2
3
4
 Location {href:"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
 ancestorOrigins: DOMStringList,
 origin:"https://stackoverflow.com",
 replace: ?, assign: ?,&nbsp;}

document.location

1
2
3
4
5
  Location {href:"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
 origin:"https://stackoverflow.com",
 replace: ?, assign: ?
,&nbsp;}

window.location.pathname

1
"/questions/1034621/get-the-current-url-with-javascript"

window.location.href

1
"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"

location.hostname

1
"stackoverflow.com"

对于包含查询字符串的完整URL:

1
document.location.toString().toLowerCase();

对于主机URL:

1
window.location

  • 您将.tolowercase()应用于实际更改它的URL。有些服务器区分大小写(Linux等)
  • 如前所述,在确保不破坏URL的情况下,不能对其进行tolowercase()。!!!如果url是server.com/path/to/script.php?选项=a1b2c3,如果文件系统区分大小写(linux/unix),则不必使用script.php和script.php。即使不区分大小写(Windows,一些Mac操作系统),?选项=a1b2c3与?选项=A1B2C3,或偶数?选项= A1B2C3。只有服务器不区分大小写:www.server.com或www.server.com是相同的。
  • 谢谢你指点这个。我们只能使用document.location.toString()来获取完整的URL。tolowercase()将在脚本中进行类似indexof的比较。


在JSTL中,我们可以使用pageContext.request.contextPath访问当前的URL路径。如果要执行Ajax调用,请使用以下URL。

1
url ="${pageContext.request.contextPath}" +"/controller/path"

示例:对于http://stackoverflow.com/posts/36577223页,这将给出http://stackoverflow.com/controller/path


获取当前位置对象的方法是window.location

将其与document.location进行比较,后者最初只是以字符串形式返回当前的URL。为了避免混淆,用document.URL代替了document.location

而且,所有的现代浏览器都将document.location映射到window.location

实际上,为了跨浏览器安全,您应该使用window.location,而不是document.location


您可以通过location.href获得当前页面的完整链接。要获取当前控制器的链接,请使用:

1
location.href.substring(0, location.href.lastIndexOf('/'));

1
   location.origin+location.pathname+location.search+location.hash;

使用javascript获取当前URL:

  • window.location.toString();

  • window.location.href


如果您引用的是具有ID的特定链接,则此代码可以帮助您。

1
2
3
4
5
6
7
8
9
10
11
12
13
$(".disapprove").click(function(){
                      var id = $(this).attr("id");

                      $.ajax({
                          url:"<?php echo base_url('index.php/sample/page/"+id+"')?>",
                          type:"post",
                          success:function()
                          {
                            alert("The Request has been Disapproved");
                            window.location.replace("http://localhost/sample/page/"+id+"");
                          }
                      });
                });

我在这里使用Ajax提交一个ID,并使用window.location.replace重定向页面。如前所述,只需添加属性id=""


首先检查页面是否在浏览器中完全加载,然后调用一个函数,该函数接受URL、URL变量并在控制台上打印,

1
2
3
4
5
6
7
8
$(window).load(function(){
   var url = window.location.href.toString();
   var URL = document.URL;
   var wayThreeUsingJQuery = $(location).attr('href');
   console.log(url);
   console.log(URL);
   console.log(wayThreeUsingJQuery );
});