我正在使用jQuery。如何获取当前URL的路径并将其分配给变量?
示例URL:
http://localhost/menuname.de?foo=bar&number=0
我正在使用jQuery。如何获取当前URL的路径并将其分配给变量?
示例URL:
http://localhost/menuname.de?foo=bar&number=0
当前回答
只需在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));
}
我希望它对你有用。
其他回答
这是一个比许多人想象的更复杂的问题。一些浏览器支持内置JavaScript位置对象和通过window.location或document.location访问的相关参数/方法。然而,不同风格的Internet Explorer(6,7)不以相同的方式支持这些方法,(window.location.href?window.location.replace()不受支持),因此您必须通过始终编写条件代码来以不同的方式访问它们,以手持Internet Explorer。
因此,如果您有jQuery可用并已加载,那么您最好使用jQuery(location),正如其他人提到的那样,因为它解决了这些问题。然而,如果您正在通过JavaScript(即使用Google Maps API和位置对象方法)进行一些客户端地理位置重定向,那么您可能不想加载整个jQuery库并编写检查每个版本的InternetExplorer/Firefox/等的条件代码。
InternetExplorer让前端编码猫不高兴,但jQuery是一盘牛奶。
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>
在jstl中,我们可以使用pageContext.request.contextPath访问当前的url路径,
url = "${pageContext.request.contextPath}" + "/controller/path"
示例:在页面中http://stackoverflow.com/questions/406192这将给http://stackoverflow.com/controller/path
java脚本提供了许多方法来检索显示在浏览器地址栏中的当前URL。
测试URL:
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«Web浏览器通过遵循WebHosted应用程序和Web客户端(浏览器)之间的一些通信规则来使用Internet协议。(http=80,https(SSL)=443,ftp=21等)
示例:使用默认端口号
<protocol>//<hostname>:<port>/<pathname><search><hash>
https://en.wikipedia.org:443/wiki/Pretty_Good_Privacy
http://stackoverflow.com:80/
(//)«主机是Internet上的端点(资源所在的机器)的名称。www.stackeoverflow.com-应用程序的DNS IP地址(OR)localhost:8080-localhost
域名是您根据域名系统(DNS)树的规则和过程注册的。使用IP地址管理您的域以进行寻址的人的DNS服务器。在DNS服务器层次结构中stackoverlfow.com的根名称是com。
gTLDs - com « stackoverflow (OR) in « co « google
本地系统必须维护主机文件中非PUBLIC的域。localhost.yash.com«localhsot-子域(web服务器),yash.com-主域(代理服务器)。myLocalApplication.com 172.89.23.777
(/)«路径提供有关Web客户端希望访问的主机内特定资源的信息(?)«一个可选查询是传递一系列属性值对,由分隔符(&)分隔。(#)«可选片段通常是特定元素的id属性,web浏览器会将该元素滚动到视图中。
如果参数有Epoch?日期=1467708674,然后使用。
var epochDate = 1467708674; var date = new Date( epochDate );
URL
使用用户名:密码的身份验证url,如果usernaem/password包含@符号如:
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 (-_.!~*')
*/
如果您需要URL中的哈希参数,window.location.href可能是更好的选择。
window.location.pathname
=> /search
window.location.href
=> www.website.com/search#race_type=1