用JavaScript获取当前的URL?
我想要的只是获取网站的url。 不是从链接中获取的url。 在页面加载,我需要能够抓住网站的完整的,当前的url,并将其设置为一个variables做我喜欢。
使用:
 window.location.href 
正如在评论中指出的那样,下面的代码行得通,但是对于Firefox来说却是一个bug。
 document.URL; 
请参阅DOMStringtypes的URL,只读 。
JavaScript提供了许多方法来检索和更改浏览器地址栏中显示的当前URL。 所有这些方法都使用Location对象,它是Window对象的一个属性。 您可以创build一个具有当前URL的新的Location对象,如下所示:
 var currentLocation = window.location; 
URL的基本结构
 <protocol>//<hostname>:<port>/<pathname><search><hash> 

- 
协议 – 指定用于访问Internet上资源的协议名称。 (HTTP(不带SSL)或HTTPS(带SSL)) 
- 
主机名 – 主机名指定拥有资源的主机。 例如, www.stackoverflow.com。 服务器使用主机的名称提供服务。
- 
端口 – 端口号,用于识别Internet或其他networking消息到达服务器时要转发到的特定进程。 
- 
path名 – 该path提供有关Web客户端要访问的主机内的特定资源的信息。 例如,/ /index.html。
- 
查询 – 查询string跟随path组件,并提供资源可用于某种目的(例如,作为search参数或要处理的数据)的一串信息。 
- 
散列 – URL的锚点部分包含散列符号(#)。 
通过这些Location对象属性,您可以访问所有这些URL组件
- 散列 – 设置或返回URL的锚点部分。
- 主机 – 设置或返回URL的主机名和端口。
- 主机名 – 设置或返回URL的主机名。
- href – 设置或返回整个URL。
- path名 – 设置或返回URL的path名。
- 端口 – 设置或返回服务器用于URL的端口号。
- 协议 – 设置或返回URL的协议。
- search – 设置或返回URL的查询部分
我希望你有你的答案..
同样的问题在不到24小时前被问到 。 引用自己:
使用
window.location对与当前帧关联的位置对象进行读写访问。 如果您只想将地址设置为只读string,则可以使用document.URL,它应该包含与window.location.href相同的值。
获取当前页面的URL:
 window.location.href 
要获得path,您可以使用:
 alert("Url ="+document.location); alert("PathName ="+ window.location.pathname);// Returns path only alert("url ="+window.location.href);// Returns full URL 
 使用: window.location.href 。 
 如上所述,在更新window.location时, document.URL 不会更新 。 请参阅MDN 。 
打开开发工具 ,在控制台中 input以下内容,然后按Enter键 。
 window.location 
例如:下面是当前页面的结果屏幕截图。
  
 
从这里抓取你需要的东西。 🙂
-  使用window.location.href来获取完整的URL。
-  使用window.location.pathname来获取离开主机的URL。
您可以通过使用以下代码来获取当前URL位置的哈希标记 :
JavaScript的:
  // Using href var URL = window.location.href; // Using path var URL = window.location.pathname; 
jQuery :
 $(location).attr('href'); 
 var currentPageUrlIs = ""; if (typeof this.href != "undefined") { currentPageUrlIs = this.href.toString().toLowerCase(); }else{ currentPageUrlIs = document.location.toString().toLowerCase(); } 
上面的代码也可以帮助某人
对于包含查询string的完整URL:
 document.location.toString().toLowerCase(); 
对于主机url:
 window.location 
 获取当前位置对象的方法是window.location 。 
 将它与document.location进行比较,最初只将当前URL作为string返回。 可能为了避免混淆, document.location被document.locationreplace。 
 而且,所有现代浏览器都将document.location映射到window.location 。 
 实际上,为了跨浏览器的安全性,你应该使用window.location而不是document.location 。 
 在jstl中,我们可以使用pageContext.request.contextPath来访问当前的URLpath。 如果您想要进行Ajax调用,请使用以下URL。 
 url = "${pageContext.request.contextPath}" + "/controller/path" 
 例如:对于页面http://stackoverflow.com/posts/36577223这将给http://stackoverflow.com/controller/path 。 
  location.origin+location.pathname+location.search+location.hash; 
 您可以通过location.href获取当前页面的完整链接,并获取当前控制器的链接,请使用: 
 location.href.substring(0, location.href.lastIndexOf('/')); 
好的,获取当前页面的完整URL很容易使用纯JavaScript。 例如,请在此页面上尝试以下代码:
 window.location.href; // use it in console of this page will return // http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser" 
window.location.href属性返回当前页面的URL。
 document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href; 
 <!DOCTYPE html> <html> <body> <h2>JavaScript</h2> <h3>The window.location.href</h3> <p id="root"></p> </body> </html> 
添加结果以供快速参考
window.location的;
  Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript", ancestorOrigins: DOMStringList, origin: "https://stackoverflow.com", replace: ƒ, assign: ƒ, …} 
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"