我所想要的是获得网站的URL。而不是从一个链接中获取的URL。在页面加载时,我需要能够抓取完整的、当前的网站URL,并将其设置为一个变量,以便我随意使用。
网址信息访问
JavaScript提供了许多方法来检索和改变当前的URL,它显示在浏览器的地址栏中。
所有这些方法都使用Location
对象,它是Window
对象的一个属性。
您可以创建一个新的Location
对象,该对象具有当前的URL,如下所示。
var currentLocation = window.location;
基本的URL结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
protocol: 指定在互联网上访问资源时使用的协议名称(HTTP(无SSL)或HTTPS(有SSL))。 (HTTP(无SSL)或HTTPS(有SSL))
hostname:主机名称指定拥有该资源的主机。
例如,www.stackoverflow.com
。
服务器使用主机名提供服务。
port: 用于识别特定进程的端口号,当互联网或其他网络消息到达服务器时,要将其转发到该端口。
路径名:** 路径给出了 Web 客户端要访问的主机内特定资源的信息。
例如,/index.html
。
查询:** 查询字符串跟在路径组件后面,提供了该资源可用于某种目的的信息字符串(例如,作为搜索的参数或作为要处理的数据)。
hash: URL的锚部分,包括哈希符号(#)。
通过这些Location
对象属性,您可以访问所有这些URL组件以及它们可以设置或返回的内容。
我希望你得到你的答案...
好了,使用纯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;
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<p id="root"></p>
</body>
</html>
<!--结束片段-->。
只是也要提一下这些。
如果你需要一个相对路径,只需使用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]
要获得路径,你可以使用:。
<!--开始片段。 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
<!--结束片段-->
您可以通过使用[通过哈希标签获取当前的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
增加结果以便快速参考
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"
在jstl中,我们可以使用pageContext.request.contextPath
访问当前的URL路径。
如果你想进行Ajax调用,请使用以下URL。
url = "${pageContext.request.contextPath}" + "/controller/path"
举例来说,对于""网页,将得到""。
对于网页https://stackoverflow.com/posts/36577223
,将得到https://stackoverflow.com/controller/path
。
用JavaScript获取当前的URL:。
>.toString();。
如果你指的是一个有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=""
即可。