示例URL:
http://localhost/menuname.de?foo=bar&number=0
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。
window.location
。 - Haralan Dobrev.attr()
。 (1) 它不是一个元素,所以$(location)
充其量也只是模糊的,而且(2) 即使它工作了,你也应该使用.prop()
来获取属性。.attr()
是用于HTML属性的。” - cHaohttp://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>
/index.php
而不是 index.php
。 - Andreawindow.location.href
可能是更好的选择。window.location.pathname
=> /search
window.location.href
=> www.website.com/search#race_type=1
window.location.hash
。 - ptim您需要使用JavaScript内置的window.location
对象。
window.location.pathname
无法获取问号后面的内容。 - worc在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));
}
希望它能对你有用。
var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/'));
。 - cogdogwindow.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.origin
是什么? - Ali Sheikhpour这是一个比许多人想象的更加复杂的问题。几个浏览器支持内置的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库并编写检查每个版本的Internet Explorer / Firefox等的条件代码。
Internet Explorer使前端编码猫不高兴,但jQuery是一大块奶酪。
仅使用主机名,可以使用:
window.location.hostname
测试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>//<hostname>:<port>/<pathname><search><hash>
https://en.wikipedia.org:443/wiki/Pretty_Good_Privacy
http://stackoverflow.com:80/
域名是根据域名系统(DNS)树的规则和程序注册的。管理您的域名的人使用DNS服务器来进行寻址。在DNS服务器层级结构中,stackoverlfow.com的根名称是com。
gTLDs - com « stackoverflow (OR) in « co « google
在本地系统中,您需要维护Host文件中不公开的域名。
localhost.yash.com
是子域名(web-server
),yash.com
是主域名(Proxy-Server
)。
myLocalApplication.com
的IP地址为172.89.23.777。
如果参数具有Epoch ?date=1467708674
,则使用。
var epochDate = 1467708674; var date = new Date( epochDate );
带有用户名和密码的身份验证URL,如果用户名/密码包含@符号
例如:
Username = `my_email@gmail`
Password = `Yash@777`
@
编码为%40
。 参考链接1
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 (-_.!~*')
*/