用JavaScript获取当前的URL?

我所想要的是获得网站的URL。而不是从一个链接中获取的URL。在页面加载时,我需要能够抓取完整的、当前的网站URL,并将其设置为一个变量,以便我随意使用。

对该问题的评论 (2)
解决办法

使用。

window.location.href 

正如评论中所指出的,下面这一行可以工作,但对于Firefox来说,它是有问题的。

document.URL;

参见DOMString类型的URL,只读

评论(9)

网址信息访问

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

var currentLocation = window.location;

基本的URL结构

//:/<pathname>
  • protocol: 指定在互联网上访问资源时使用的协议名称(HTTP(无SSL)或HTTPS(有SSL))。 (HTTP(无SSL)或HTTPS(有SSL))

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

  • port: 用于识别特定进程的端口号,当互联网或其他网络消息到达服务器时,要将其转发到该端口。

  • 路径名:** 路径给出了 Web 客户端要访问的主机内特定资源的信息。 例如,/index.html

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

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

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

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

我希望你得到你的答案...

评论(5)

使用window.location来读写与当前帧相关的位置对象。如果你只是想以只读字符串的形式获得地址,你可以使用document.URL,它应该包含与window.location.href相同的值。

评论(1)

获取当前页面的URL。

window.location.href
评论(5)

好了,使用纯JavaScript获取当前页面的完整URL很容易。 例如,在这个页面上试试这段代码。

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

gt.windows.location.href属性返回当前页面的URL。 window.location.href属性返回当前页面的URL。

<!--开始片段。 js hide: false console.true babel: true true babel.false --> -- begin snippet: js hide: false console: true false -->

document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;




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


<!--结束片段--&gt。

只是也要提一下这些。

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

  • 如果你'想得到主机名,可以使用window.location.hostname

  • 如果你需要单独获取协议,使用window.location.protocol

  • 另外,如果你的页面有 "hash "标签,你可以得到这样的标签。 window.location.hash

所以window.location.href可以一次性处理所有的内容...... 基本上。

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

如果已经在窗口范围内,则不需要使用 "window"。

所以,在这种情况下,你可以使用。

location.protocol

location.hostname

location.pathname

location.hash

location.href

[![用JavaScript获取当前URL][1]][1]

[1]: https://i.stack.imgur.com/NUGbD.png

评论(0)

要获得路径,你可以使用:

<!--开始片段。 js hide: false console: true babel.false --> -- begin snippet: js hide: false console: true false -->

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

<!--结束片段-->

评论(0)

打开Developer Tools,在console中输入以下内容,并按Enter

window.location

例。 下面是当前页面的结果截图。

[![在此输入图片描述][1]][1]。

[1]: https://i.stack.imgur.com/EbfAs.png

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

评论(0)

使用: window.location.href

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

评论(0)
  • 使用window.location.href获得完整的URL。
  • 使用window.location.pathname获得离开主机的URL。
评论(1)

您可以通过使用[通过哈希标签获取当前的URL位置][1]。

JavaScript:

 // Using href
 var URL = window.location.href;

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

*jQuery

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

[1]: https://stackoverflow.com/questions/1034621/get-current-url-in-web-browser

评论(0)
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
}else{ 
       currentPageUrlIs = document.location.toString().toLowerCase();
}

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

评论(4)

增加结果以便快速参考

window.location.{{5816278}}}。

 Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
 ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ, …}

&gt.document.location document.location

  Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript", 
ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ
, …}

window.location.pathname

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

window.location.href

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

location.hostname

"stackoverflow.com"
评论(0)

对于完整的URL与查询字符串。

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

对于主机网址。

window.location
评论(3)

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

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

举例来说,对于""网页,将得到""。 对于网页https://stackoverflow.com/posts/36577223,将得到https://stackoverflow.com/controller/path

评论(0)

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

比较一下document.location,它最初只返回当前的URL作为一个字符串。 可能是为了避免混淆,document.location被替换为document.URL

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

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

评论(0)

对于那些想要一个实际的URL对象的人来说,可能是为了一个接受URL作为参数的实用程序。

const url = new URL(window.location.href)

https://developer.mozilla.org/en-US/docs/Web/API/URL

评论(0)
   location.origin+location.pathname+location.search+location.hash;
评论(0)

你可以通过 "location.href "获得当前页面的完整链接。 和获取当前控制器的链接,使用。

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

用JavaScript获取当前的URL:

&gt.toString();。

  • window.location.toString(); &gt.toString(); &gt.toString()
  • window.location.toString(); > &gt
评论(0)

如果你指的是一个有id的特定链接,这个代码可以帮助你。

$(".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=""即可。

评论(0)