2882 javascript url
我想要的只是获取网站URL.不是从链接中获取的URL.在页面加载中,我需要能够获取网站的完整,当前URL并将其设置为变量以便随意使用.
Vol*_*erK 3551
使用:
window.location.href
Run Code Online (Sandbox Code Playgroud)
正如评论中所指出的那样,下面的一行有效,但它对Firefox来说是错误的.
document.URL;
Run Code Online (Sandbox Code Playgroud)
Nik*_*wal 625
URL信息访问
JavaScript为您提供了许多方法来检索和更改当前URL,该URL显示在浏览器的地址栏中.所有这些方法都使用Location
对象,该对象是对象的属性Window
.您可以创建Location
具有当前URL 的新对象,如下所示:
var currentLocation = window.location;
Run Code Online (Sandbox Code Playgroud)
基本URL结构
<protocol>//<hostname>:<port>/<pathname><search><hash>
Run Code Online (Sandbox Code Playgroud)
protocol:指定用于访问Internet上资源的协议名称.(HTTP(不含SSL)或HTTPS(含SSL))
hostname:主机名指定拥有该资源的主机.例如,www.stackoverflow.com
.服务器使用主机名提供服务.
port:用于识别Internet或其他网络消息到达服务器时要转发到的特定进程的端口号.
pathname:该路径提供有关Web客户端要访问的主机中的特定资源的信息.例如,/index.html
.
query:查询字符串在路径组件之后,并提供资源可用于某种目的的信息字符串(例如,作为搜索的参数或要处理的数据).
hash: URL的锚点部分,包括井号(#).
使用这些Location
对象属性,您可以访问所有这些URL组件以及它们可以设置或返回的内容:
我希望你得到答案..
Zan*_*oni 258
获取当前页面URL:
window.location.href
Run Code Online (Sandbox Code Playgroud)
Ali*_*eza 39
好的,使用纯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"
Run Code Online (Sandbox Code Playgroud)
window.location.href属性返回当前页面的URL.
document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location.href</h3>
<p id="root"></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
同样可以提到这些:
此外,如果您需要相对路径,只需使用window.location.href
;
如果你想获得主机名,你可以使用window.location.pathname
;
如果您需要单独获取协议,只需执行 window.location.hostname
此外,如果您的页面有window.location.protocol
标签,您可以像下面这样:hash
所以window.location.hash
一次处理...基本上:
window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
//true
Run Code Online (Sandbox Code Playgroud)
window.location.href
如果已经在窗口范围内也没有必要使用...
那么,在这种情况下,您可以使用:
location.protocol
location.hostname
location.pathname
location.hash
location.href
Run Code Online (Sandbox Code Playgroud)
San*_*hah 38
要获得路径,您可以使用:
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
Run Code Online (Sandbox Code Playgroud)
tmh*_*tmh 34
打开开发人员工具,在控制台中输入以下内容,然后按Enter键.
window.location
Run Code Online (Sandbox Code Playgroud)
例如:下面是当前页面上结果的屏幕截图.
从这里抓住你需要的东西.:)
Sid*_*jee 28
要获取路径,您可以使用:
http://www.example.com:8082/index.php#tab2?foo=789
Property Result
------------------------------------------
window.location.host www.example.com:8082
window.location.hostname www.example.com
window.location.port 8082
window.location.protocol http:
window.location.pathname index.php
window.location.href http://www.example.com:8082/index.php#tab2
window.location.hash #tab2
window.location.search ?foo=789
window.location.origin https://example.com
Run Code Online (Sandbox Code Playgroud)
kis*_*ore 19
window.location.href
以获得完整的URL.window.location.pathname
得到URL离开主机.Mes*_*Qin 19
// http://127.0.0.1:8000/projects/page/2?name=jake&age=34
let url = new URL(window.location.href);
/*
hash: ""
host: "127.0.0.1:8000"
hostname: "127.0.0.1"
href: "http://127.0.0.1:8000/projects/page/2?username=jake&age=34"
origin: "http://127.0.0.1:8000"
password: ""
pathname: "/projects/page/2"
port: "8000"
protocol: "http:"
search: "?name=jake&age=34"
username: ""
*/
url.searchParams.get('name')
// jake
url.searchParams.get('age')
// 34
url.searchParams.get('gender')
// null
Run Code Online (Sandbox Code Playgroud)
Bha*_*att 16
您可以使用以下方法获取带有哈希标记的当前URL位置:
JavaScript的:
// Using href
var URL = window.location.href;
// Using path
var URL = window.location.pathname;
Run Code Online (Sandbox Code Playgroud)
jQuery:
$(location).attr('href');
Run Code Online (Sandbox Code Playgroud)
Roh*_*til 10
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
currentPageUrlIs = this.href.toString().toLowerCase();
}else{
currentPageUrlIs = document.location.toString().toLowerCase();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码也可以帮助某人
添加结果以便快速参考
window.location的;
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ, …}
Run Code Online (Sandbox Code Playgroud)
document.location
Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
ancestorOrigins: DOMStringList,
origin: "https://stackoverflow.com",
replace: ƒ, assign: ƒ
, …}
Run Code Online (Sandbox Code Playgroud)
window.location.pathname
"/questions/1034621/get-the-current-url-with-javascript"
Run Code Online (Sandbox Code Playgroud)
window.location.href
"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"
Run Code Online (Sandbox Code Playgroud)
location.hostname
"stackoverflow.com"
Run Code Online (Sandbox Code Playgroud)
对于包含查询字符串的完整URL:
document.location.toString().toLowerCase();
Run Code Online (Sandbox Code Playgroud)
对于主机URL:
window.location
Run Code Online (Sandbox Code Playgroud)
对于那些想要一个实际 URL 对象的人,可能是一个将 URL 作为参数的实用程序:
const url = new URL(window.location.href)
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en-US/docs/Web/API/URL
Nikhil Agrawal 的回答很棒,只需在此处添加一个小示例,您就可以在控制台中查看不同组件的运行情况:
如果您希望基本 URL 没有路径或查询参数(例如,针对开发/登台和生产服务器执行 AJAX 请求),window.location.origin
最好是因为它保留了协议以及可选端口(在 Django 开发中,您有时有一个非标准端口,如果你只使用主机名等,它会破坏它。)
小智 7
您有多种方法可以做到这一点。
1:
location.href;
Run Code Online (Sandbox Code Playgroud)
2:
document.URL;
Run Code Online (Sandbox Code Playgroud)
3:
document.documentURI;
Run Code Online (Sandbox Code Playgroud)
location.origin+location.pathname+location.search+location.hash;
Run Code Online (Sandbox Code Playgroud)
和
location.href
Run Code Online (Sandbox Code Playgroud)
做同样的事情。
获取当前位置对象的方法是window.location
.
将此与 比较document.location
,后者最初仅以字符串形式返回当前 URL。可能是为了避免混淆,document.location
被替换为document.URL
.
而且,所有现代浏览器都映射document.location
到window.location
.
实际上,为了跨浏览器的安全,您应该使用window.location
而不是document.location
.
在 jstl 中,我们可以使用pageContext.request.contextPath
. 如果要进行 Ajax 调用,请使用以下 URL。
url = "${pageContext.request.contextPath}" + "/controller/path"
Run Code Online (Sandbox Code Playgroud)
示例:对于页面,http://stackoverflow.com/posts/36577223
这将给出http://stackoverflow.com/controller/path
.