如果网址是主页,则执行jquery

4

我需要在首页执行一段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.AbsolutePathHttpContext.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
请参见以下链接:https://dev59.com/6nRC5IYBdhLWcg3wG9To - Wilfredo P
2个回答

3

你能否只在需要的页面上包含脚本?例如:只在default.aspx中使用<script type="text/javascript" src="homepage.js"></script>

如果不行,那么请像dfsq建议的一样使用 window.location.pathname

var page = window.location.pathname;
if(page == '/' || page == '/default.aspx'){
    // -- do stuff
}

您可以仅获取最后斜杠后的部分,以考虑文件夹差异...
var page = window.location.toString();
page = page.substring(page.lastIndexOf('/'));

但是这对于 example.com/default.aspxexample.com/folder1/default.aspx 都是适用的。

请记住,这个Javascript是客户端的,因此没有类似于你链接的C#示例的等效方法。


0
你可以使用我的方法来精确地知道页面(包括URL路由)并在JavaScript中使用它:
我使用body id来识别页面。
JavaScript代码:
$(document).ready(function () {
    if (document.body.id.indexOf('defaultPage') == 0) {
        /*do something*/
    }
});

Asp.net 代码:

在母版页或页面(aspx)中:

...
<body id="<%=BodyId %>">
...

代码后台:

private string _bodyId;
public string BodyId
{
    get
    {
        if (string.IsNullOrWhiteSpace(_bodyId))
        {
            var path = GetRealPagePath().TrimStart('/','~');
            int index = path.LastIndexOf('.');
            if (index > -1)
            {
                path = path.Substring(0, index);
            }
            _bodyId = path.Replace("/", "_").ToLower();
        }
        return string.Concat(_bodyId,"Page");
    }
}
public string GetRealPagePath()
{
    string rtn = Request.Path;
    if (Page.RouteData != null && Page.RouteData.RouteHandler!= null)
    {
        try
        {
            if (Page.RouteData.RouteHandler.GetType() == typeof(PageRouteHandler))
            {
                 rtn=((PageRouteHandler)Page.RouteData.RouteHandler).VirtualPath;
            }
            else
            {
                 rtn = Page.Request.AppRelativeCurrentExecutionFilePath;
            }
        }
        catch (Exception ex)
        {              
            Logger.Error(string.Format("GetRealPagePath() Request.Path:{0} Page.Request.AppRelativeCurrentExecutionFilePath:{1}", Request.Path, rtn), ex);
        }
    } 
    return rtn;
}

谢谢,我可以在ASP.NET代码后端做同样的事情,但我正在寻找使用jQuery的解决方案。这种方法可能是一个解决办法。 - Learning
@KnowledgeSeeker 我使用jQuery来实现这个功能。我只使用ASP.NET来确定确切的页面,以及在URL路由被使用时的情况。 - giammin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接