【发布时间】:2010-11-05 07:09:35
【问题描述】:
我想要的只是获取网站 URL。不是从链接中获取的 URL。在页面加载时,我需要能够获取网站的完整当前 URL,并将其设置为变量以随意使用。
【问题讨论】:
标签: javascript url
我想要的只是获取网站 URL。不是从链接中获取的 URL。在页面加载时,我需要能够获取网站的完整当前 URL,并将其设置为变量以随意使用。
【问题讨论】:
标签: javascript url
用途:
window.location.href
正如 cmets 中所述,下面的行有效,但它在 Firefox 中存在错误。
document.URL
【讨论】:
document.URL 属性不会在 window.location 之后更新为锚点 (#),而 window.location.href 会。我没有测试任何其他版本的 Firefox。在 Chrome 20 和 IE9 中未发现使用 document.URL 的问题。
window.location.host 和 window.location.href.toString().split(window.location.host)[1]
document.baseURI 是什么。基本上有 3 种方式获取 url document.baseURI, document.URL, & location.
name="URL" 的框架、图像或表单,那么此属性将在document 对象上被遮蔽,您的代码将中断。在这种情况下,document.URL 将改为引用 DOM 节点。最好使用window.location.href 中的全局对象的属性。
网址信息访问
JavaScript 为您提供了许多检索和更改当前 URL 的方法,该 URL 显示在浏览器的地址栏中。所有这些方法都使用Location 对象,它是Window 对象的一个属性。您可以创建一个具有当前 URL 的新 Location 对象,如下所示:
var currentLocation = window.location;
基本网址结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
协议: 指定用于访问 Internet 上的资源的协议名称。 (HTTP(无 SSL)或 HTTPS(有 SSL))
主机名: 主机名指定拥有资源的主机。例如,www.stackoverflow.com。服务器使用主机名提供服务。
端口:一个端口号,用于识别 Internet 或其他网络消息到达服务器时要转发到的特定进程。
路径名: 路径提供有关 Web 客户端想要访问的主机内特定资源的信息。例如,/index.html。
搜索: 查询字符串跟随路径组件,并提供资源可用于某些目的的信息字符串(例如,作为搜索参数或作为数据进行处理)。
哈希: URL 的锚部分,包括井号 (#)。
通过这些Location 对象属性,您可以访问所有这些 URL 组件以及它们可以设置或返回的内容:
希望你能得到答案..
【讨论】:
window.location的“方法”,而是属性,而here we have an example:var stringPathName = window.location.pathname。
substring 将其删除。但是,当您想使用重定向 document.location = "/page.html"; 将重定向到根页面时,它可能很有用 page.html
search 的位置,但在下面的描述列表中,它被称为 query。也许他们可以调和,或者可以添加进一步的解释。
使用window.location 对与当前帧关联的location object 进行读写访问。如果你只想获取地址为只读字符串,你可以使用document.URL,它应该包含与window.location.href相同的值。
获取当前页面网址:
window.location.href
【讨论】:
document 是规范定义的文档树的根。 window 通常是等价的,但在某些奇怪的情况下可能不会。
好的,使用纯 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"
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.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
【讨论】:
要获取路径,可以使用:
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
【讨论】:
使用:window.location.href。
如上所述,document.URL 在更新window.location 时不会更新。见MDN。
【讨论】:
window.location.href 获取完整的URL。 window.location.pathname 获取离开主机的URL。【讨论】:
您可以使用get the current URL location with a hash tag:
JavaScript:
// Using href
var URL = window.location.href;
// Using path
var URL = window.location.pathname;
jQuery:
$(location).attr('href');
【讨论】:
对于带有查询字符串的完整 URL:
document.location.toString()
对于主机 URL:
window.location
【讨论】:
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
currentPageUrlIs = this.href.toString().toLowerCase();
}else{
currentPageUrlIs = document.location.toString().toLowerCase();
}
上面的代码也可以帮助别人
【讨论】:
添加结果以供快速参考
window.location;
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ, …}
文档位置
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"
位置.主机名
"stackoverflow.com"
【讨论】:
对于那些想要一个实际 URL 对象的人,可能是一个将 URL 作为参数的实用程序:
const url = new URL(window.location.href)
【讨论】:
在 jstl 中,我们可以使用 pageContext.request.contextPath 访问当前 URL 路径。如果要进行 Ajax 调用,请使用以下 URL。
url = "${pageContext.request.contextPath}" + "/controller/path"
例如:对于页面http://stackoverflow.com/posts/36577223,这将给出http://stackoverflow.com/controller/path。
【讨论】:
获取当前位置对象的方式是window.location。
将此与 document.location 进行比较,后者最初仅将当前 URL 作为字符串返回。可能是为了避免混淆,document.location 被替换为 document.URL。
而且,所有现代浏览器都将document.location 映射到window.location。
实际上,为了跨浏览器的安全,您应该使用window.location 而不是document.location。
【讨论】:
您有多种方法可以做到这一点。
1:
location.href;
2:
document.URL;
3:
document.documentURI;
【讨论】:
location.origin+location.pathname+location.search+location.hash;
和
location.href
做同样的事情。
【讨论】:
使用这个:
var url = window.location.href;
console.log(url);
【讨论】:
您可以通过location.href获取当前页面的完整链接
并获取当前控制器的链接,使用:
location.href.substring(0, location.href.lastIndexOf('/'));
【讨论】:
let url = new URL(window.location.href);
// http://127.0.0.1:8000/projects/page/member/assign/2/
/*
hash: ""
host: "127.0.0.1:8000"
hostname: "127.0.0.1"
href: "http://127.0.0.1:8000/projects/page/member/assign/2/"
origin: "http://127.0.0.1:8000"
password: ""
pathname: "/projects/page/member/assign/2/"
port: "8000"
protocol: "http:"
search: ""
*/
【讨论】:
使用 JavaScript 获取当前 URL:
window.location.toString();
window.location.href
【讨论】:
试试
location+''
let url = location+'';
console.log(url);
【讨论】:
如果您指的是具有 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=""。
【讨论】:
首先检查页面是否完全加载
browser,window.location.toString();
window.location.href
然后调用一个函数,该函数接受 url、URL 变量并在控制台上打印,
$(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 );
});
【讨论】: