我需要在首页执行一段jQuery代码。
URL的概率如下:
http://www.example.com
http://www.example.com/
http://www.example.com/default.aspx
如果我想运行代码,那么如果是以上任何一种URL,我可以使用什么方法?
var currenturl = window.location
但是每次将我的代码移动到服务器上时,我都必须更改此内容,因为在本地主机上,我的URL类似于
http://localhost:90/virtualDir/default.aspx
在ASP.NET中,我们可以使用各种方法获取IT。其中包括:
HttpContext.Current.Request.Url.AbsolutePath
或HttpContext.Current.Request.ApplicationPath
。我不确定在jQuery中有什么等效的方法。参考ASP.NET 示例。更新:
由于找不到更简单的方法,我采用了简单的方法来解决这个问题。
var _href = $(location).attr('href').toLowerCase()
var _option1 = 'http://localhost:51407/virtualDir/Default.aspx';
var _option2 = 'http://www.example.com/Default.aspx';
var _option3 = 'http://www.example.com/';
if (_href == _option1.toLowerCase() || _href == _option2.toLowerCase() || _href == _option3.toLowerCase()) {
$(".bar-height").css("min-height", "689px");
// alert('aa');
}
else
{ //alert('bb'); }
location.pathname
。 - dfsq