题
我使用的!我怎么得到的道路目前的网址,并将其分配给可变?
例URL:
http://localhost/menuname.de?foo=bar&number=0
解决方案
要获取路径,您可以使用:
var pathname = window.location.pathname; // Returns path only (/path/example.html)
var url = window.location.href; // Returns full URL (https://example.com/path/example.html)
var origin = window.location.origin; // Returns base URL (https://example.com)
其他提示
纯粹的jQuery风格:
$(location).attr('href');
location对象还有其他属性,如host,hash,protocol和pathname。
http://www.refulz.com:8082/index.php#tab2?foo=789
Property Result
------------------------------------------
host www.refulz.com:8082
hostname www.refulz.com
port 8082
protocol http:
pathname index.php
href http://www.refulz.com:8082/index.php#tab2
hash #tab2
search ?foo=789
var x = $(location).attr('<property>');
只有拥有jQuery才能使用。例如:
<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script>
$(location).attr('href'); // http://www.refulz.com:8082/index.php#tab2
$(location).attr('pathname'); // index.php
</script>
</html>
如果您需要URL中存在哈希参数, window.location.href
可能是更好的选择。
window.location.pathname
=> /search
window.location.href
=> www.website.com/search#race_type=1
您需要使用JavaScript的内置 window.location
对象。
只需在JavaScript中添加此函数,它将返回当前路径的绝对路径。
function getAbsolutePath() {
var loc = window.location;
var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}
我希望它适合你。
window.location是javascript中的一个对象。它返回以下数据
window.location.host #returns host
window.location.hostname #returns hostname
window.location.path #return path
window.location.href #returns full current url
window.location.port #returns the port
window.location.protocol #returns the protocol
在jquery中你可以使用
$(location).attr('host'); #returns host
$(location).attr('hostname'); #returns hostname
$(location).attr('path'); #returns path
$(location).attr('href'); #returns href
$(location).attr('port'); #returns port
$(location).attr('protocol'); #returns protocol
这是一个比许多人想象的更复杂的问题。一些浏览器支持通过 window.location
或 document.location
访问的内置JavaScript位置对象和相关参数/方法。但是,不同风格的Internet Explorer(6,7)不支持这些方法,( window.location.href
? window.location.replace()
不支持)所以你必须通过不断编写条件代码来手持Internet Explorer来以不同的方式访问它们。
所以,如果你有jQuery可用并加载,你也可以使用jQuery(location),因为它解决了这些问题。但是,如果您正在做一个示例 - 通过JavaScript进行一些客户端地理位置重定向(即使用Google Maps API和位置对象方法),那么您可能不想加载整个jQuery库并编写条件代码检查每个版本的Internet Explorer / Firefox /等。
Internet Explorer让前端编码猫不高兴,但jQuery是一盘牛奶。
仅限主机名,请使用:
window.location.hostname
这也有效:
var currentURL = window.location.href;
java-脚本提供了许多方法检索目前的网址,这是显示在浏览器的地址栏。
试验网址:
http://
stackoverflow.com/questions/5515310/get-current-url-with-jquery/32942762
?
rq=1&page=2&tab=active&answertab=votes
#
32942762
resourceAddress.hash();
console.log('URL Object ', webAddress);
console.log('Parameters ', param_values);
功能:
var webAddress = {};
var param_values = {};
var protocol = '';
var resourceAddress = {
fullAddress : function () {
var addressBar = window.location.href;
if ( addressBar != '' && addressBar != 'undefined') {
webAddress[ 'href' ] = addressBar;
}
},
protocol_identifier : function () { resourceAddress.fullAddress();
protocol = window.location.protocol.replace(':', '');
if ( protocol != '' && protocol != 'undefined') {
webAddress[ 'protocol' ] = protocol;
}
},
domain : function () { resourceAddress.protocol_identifier();
var domain = window.location.hostname;
if ( domain != '' && domain != 'undefined' && typeOfVar(domain) === 'string') {
webAddress[ 'domain' ] = domain;
var port = window.location.port;
if ( (port == '' || port == 'undefined') && typeOfVar(port) === 'string') {
if(protocol == 'http') port = '80';
if(protocol == 'https') port = '443';
}
webAddress[ 'port' ] = port;
}
},
pathname : function () { resourceAddress.domain();
var resourcePath = window.location.pathname;
if ( resourcePath != '' && resourcePath != 'undefined') {
webAddress[ 'resourcePath' ] = resourcePath;
}
},
params : function () { resourceAddress.pathname();
var v_args = location.search.substring(1).split("&");
if ( v_args != '' && v_args != 'undefined')
for (var i = 0; i < v_args.length; i++) {
var pair = v_args[i].split("=");
if ( typeOfVar( pair ) === 'array' ) {
param_values[ decodeURIComponent( pair[0] ) ] = decodeURIComponent( pair[1] );
}
}
webAddress[ 'params' ] = param_values;
},
hash : function () { resourceAddress.params();
var fragment = window.location.hash.substring(1);
if ( fragment != '' && fragment != 'undefined')
webAddress[ 'hash' ] = fragment;
}
};
function typeOfVar (obj) {
return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
}
例如:与默认的口号
<protocol>//<hostname>:<port>/<pathname><search><hash>
https://en.wikipedia.org:443/wiki/Pretty_Good_Privacy
http://stackoverflow.com:80/
- (//)"主是给定名称的结束点(计算机资源的生活),在互联网上。www.stackoverflow.com - DNS IP地址的应用程序(或)localhost:8080-localhost
域名是您登记册的规则和程序的域名系统(DNS)树。DNS服务的人管理领域与IP地址为解决目的。在DNS server层次 根姓名的一个stackoverlfow.com 是com.
gTLDs - com « stackoverflow (OR) in « co « google
当地的系统你必须保持域的不公众在主文件。localhost.yash.com « localhsot - subdomain(
web-server
), yash.com - maindomain(
Proxy-Server
).
myLocalApplication.com 172.89.23.777
- (/)"路径提供信息有关的具体资源在主机网客户的要求访问
- (?) "任选的查询是通过一种序列的属性值对分离的分隔(&).
- (#)"的一个可选的片段往往是一个id属性的一个具体元件,网络浏览器将滚动这元。
如果有一个参数 的时代 ?date=1467708674
然后使用。
var epochDate = 1467708674; var date = new Date( epochDate );
认证网址的用户名:密码,如果usernaem/密码包含@符号
如:
Username = `my_email@gmail`
Password = `Yash@777`
然后你需要的URL编码 @
作为 %40
. 指...
http://my_email%40gmail.com:Yash%40777@www.my_site.com
encodeURI()
(vs) encodeURIComponent()
例
var testURL = "http:my_email@gmail:Yash777@//stackoverflow.com?tab=active&page=1#32942762";
var Uri = "/:@?&=,#", UriComponent = "$;+", Unescaped = "(-_.!~*')"; // Fixed
var encodeURI_Str = encodeURI(Uri) +' '+ encodeURI( UriComponent ) +' '+ encodeURI(Unescaped);
var encodeURIComponent_Str = encodeURIComponent( Uri ) +' '+ encodeURIComponent( UriComponent ) +' '+ encodeURIComponent( Unescaped );
console.log(encodeURI_Str, '\n', encodeURIComponent_Str);
/*
/:@?&=,# +$; (-_.!~*')
%2F%3A%40%3F%26%3D%2C%23 %2B%24%3B (-_.!~*')
*/
您可以记录window.location并查看所有选项,仅供URL使用:
window.location.origin
用于整个路径:
window.location.href
还有位置。 _ _
.host
.hostname
.protocol
.pathname
如果有人想要连接网址和哈希标记,请合并两个功能:
var pathname = window.location.pathname + document.location.hash;
我有这个去除GET变量。
var loc = window.location;
var currentURL = loc.protocol + '//' + loc.host + loc.pathname;
您可以使用js本身简单地获取路径, window.location
或 location
将为您提供当前URL的对象
console.log("Origin - ",location.origin);
console.log("Entire URL - ",location.href);
console.log("Path Beyond URL - ",location.pathname);
var currenturl = jQuery(location).attr('href');
从iframe中获取父窗口的URL:
$(window.parent.location).attr('href');
注意:仅适用于同一个域
以下是使用jQuery和JavaScript获取当前URL的示例:
$(document).ready(function() {
//jQuery
$(location).attr('href');
//Pure JavaScript
var pathname = window.location.pathname;
// To show it in an alert window
alert(window.location);
});
$.getJSON("idcheck.php?callback=?", { url:$(location).attr('href')}, function(json){
//alert(json.message);
});
以下是可以使用的有用代码片段的示例&#8211;一些示例使用标准JavaScript函数,并不特定于jQuery:
使用 window.location.href 。这将为您提供完整的网址。
window.location 会为您提供当前的网址,您可以从中提取任何内容......
如果您想获取根网站的路径,请使用以下命令:
$(location).attr('href').replace($(location).attr('pathname'),'');
请参阅 purl.js 。这将非常有用,也可以使用,具体取决于jQuery。像这样使用它:
$.url().param("yourparam");
var path = location.pathname
返回当前URL的路径(不需要jQuery)。 window.location
的使用是可选的。
非常常用的前三名
1. window.location.hostname
2. window.location.href
3. window.location.pathname
所有浏览器都支持Javascript窗口对象。它定义了浏览器的窗口。
全局对象和函数自动成为窗口对象的一部分。
所有全局变量都是窗口对象属性,所有全局函数都是它的方法。
整个HTML文档也是一个窗口属性。
因此,您可以使用window.location对象获取所有与url相关的属性。
<强>的Javascript 强>
console.log(window.location.host); //returns host
console.log(window.location.hostname); //returns hostname
console.log(window.location.pathname); //return path
console.log(window.location.href); //returns full current url
console.log(window.location.port); //returns the port
console.log(window.location.protocol) //returns the protocol
<强> JQuery的强>
console.log("host = "+$(location).attr('host'));
console.log("hostname = "+$(location).attr('hostname'));
console.log("pathname = "+$(location).attr('pathname'));
console.log("href = "+$(location).attr('href'));
console.log("port = "+$(location).attr('port'));
console.log("protocol = "+$(location).attr('protocol'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
// get current URL
$(location).attr('href');
var pathname = window.location.pathname;
alert(window.location);
在jstl中,我们可以使用 pageContext.request.contextPath
访问当前的url路径,如果你想进行ajax调用,
url = "${pageContext.request.contextPath}" + "/controller/path"
例如:在页面 http://stackoverflow.com/questions/406192
中,这将提供 http://stackoverflow.com/controller/path