使用jQuery获取当前URL?

我正在使用jQuery。 如何获取当前URL的路径并将其分配给变量?

示例url:

http://localhost/menuname.de?foo=bar&number=0 

要获取路径,您可以使用:

 var pathname = window.location.pathname; // Returns path only var url = window.location.href; // Returns full URL var origin = window.location.origin; // Returns base URL 

在纯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(''); 

这只有在你有jQuery的情况下才有效。 例如:

   $(location).attr('href'); // http://www.refulz.com:8082/index.php#tab2 $(location).attr('pathname'); // index.php   

如果您需要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.locationdocument.location访问的内置JavaScript位置对象和相关参数/方法。 但是,不同风格的Internet Explorer(6,7)不支持这些方法,(不支持window.location.hrefwindow.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-script提供了许多方法来检索当前URL,该URL显示在浏览器的地址栏中。

测试url: http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in/32942762?rq=1&page=2&tab=active&answertab=votes#32942762http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in/32942762?rq=1&page=2&tab=active&answertab=votes#32942762

 resourceAddress.hash(); console.log('URL Object ', webAddress); console.log('Parameters ', param_values); 

function:

 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(); } 
  • 协议« Web浏览器通过遵循WebHosted应用程序和Web客户端(浏览器)之间的通信规则来使用Internet协议。 (http = 80 ,https(SSL)= 443 ,ftp = 21等)

EX:使用默认端口号

 //:/ https://en.wikipedia.org:443/wiki/Pretty_Good_Privacy http://stackoverflow.com:80/ 
  • (//)«主机是因特网上给终点(资源所在的机器)的名称。 http://www.stackoverflow.com - 应用程序(OR)localhost的DNS IP地址:8080 - localhost

域名是您通过域名系统(DNS)树的规则和过程注册的。 使用IP地址管理您的域以进行寻址的人的DNS服务器。 在DNS服务器层次结构中,stackoverlfow.com的根名称为com。

 gTLDs - com « stackoverflow (OR) in « co « google 

本地系统您必须维护主机文件中不是PUBLIC的域。 localhost.yash.com « localhsot - subdomain( web-server ), yash.com - maindomain( Proxy-Server ). myLocalApplication.com 172.89.23.777 ). myLocalApplication.com 172.89.23.777

  • (/)«该路径提供有关Web客户端想要访问的主机内特定资源的信息
  • (?)«可选查询是传递由分隔符(&)分隔的一系列属性 - 值对。
  • (#)«可选片段通常是特定元素的id属性,Web浏览器会将此元素滚动到视图中。

如果参数有一个Epoch ?date=1467708674那么使用。

 var epochDate = 1467708674; var date = new Date( epochDate ); 

url 在此处输入图像描述


使用用户名:password的身份validationURL,如果usernaem / password包含@符号
喜欢:

 Username = `my_email@gmail` password = `Yash@777` 

那么你需要对@ as %40进行URL编码。 参考...

 http://my_email%40gmail.com:Yash%40777@www.my_site.com 

您可以登录window.location并查看所有选项,仅供URL使用:

 window.location.origin 

对于整个路径使用:

 window.location.href 

还有位置。 _ _

 .host .hostname .protocol .pathname 

这将使用JavaScript / jQuery返回当前页面的绝对URL 。

  • document.URL

  • $("*").context.baseURI

  • location.href

如果有人想要连接URL和哈希标记,请组合两个函数:

 var pathname = window.location.pathname + document.location.hash; 

我有这个去除GET变量。

 var loc = window.location; var currentURL = loc.protocol + '//' + loc.host + loc.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); }); 

以下是可以使用的有用代码片段的示例 – 一些示例使用标准JavaScript函数,并不是特定于jQuery:

请参阅8个有用的jQuery Snippets,用于URL和Querystrings

使用window.location.href 。 这将为您提供完整的URL 。

您可以使用js本身简单地获取路径, window.locationlocation将为您提供当前URL的对象

 console.log("Origin - ",location.origin); console.log("Entire URL - ",location.href); console.log("Path Beyond URL - ",location.pathname); 

window.location将为您提供当前的URL ,您可以从中提取任何内容…

如果要获取根站点的路径,请使用以下命令:

 $(location).attr('href').replace($(location).attr('pathname'),''); 

var path = location.pathname返回jQuery中当前URL的路径。 没有必要使用window

见purl.js。 这将非常有用,也可以使用,具体取决于jQuery。 像这样用它:

 $.url().param("yourparam"); 

非常常用的前3个是

 1. window.location.hostname 2. window.location.href 3. window.location.pathname 
 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

所有浏览器都支持Javascript窗口对象。 它定义了浏览器的窗口。

全局对象和函数自动成为窗口对象的一部分。

所有全局变量都是窗口对象属性,所有全局函数都是其方法。

整个HTML文档也是一个窗口属性。

因此,您可以使用window.location对象来获取所有与url相关的属性。

使用Javascript

 console.log(window.location.host); //returns host console.log(window.location.hostname); //returns hostname console.log(window.location.path); //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