javascript - Excute jquery if url is home page - Stack Overflow

I need to fire piece of jQuery code only if it is home page.URL probability are.aspxHow can i run cod

I need to fire piece of jQuery code only if it is home page.

URL probability are


/
.aspx

How can i run code if it is any of the above url i can use

var currenturl = window.location

but then i have to change this every time i move my code to server as on local host my url is like

http://localhost:90/virtualDir/default.aspx

in asp we can get the it using various

HttpContext.Current.Request.Url.AbsolutePath or HttpContext.Current.Request.ApplicationPath

I am not sure what are the equivalent in jQuery

reference of asp example

UPDATE:

I have taken a simple approach as i could not find other easy way of doing it

            var _href = $(location).attr('href').toLowerCase()
            var _option1 = 'http://localhost:51407/virtualDir/Default.aspx';
            var _option2 = '.aspx';
            var _option3 = '/';
            if (_href == _option1.toLowerCase() || _href == _option2.toLowerCase() || _href == _option3.toLowerCase()) {
                $(".bar-height").css("min-height", "689px");
               // alert('aa');
            }
            else
            { //alert('bb'); }

I need to fire piece of jQuery code only if it is home page.

URL probability are

http://www.example.
http://www.example./
http://www.example./default.aspx

How can i run code if it is any of the above url i can use

var currenturl = window.location

but then i have to change this every time i move my code to server as on local host my url is like

http://localhost:90/virtualDir/default.aspx

in asp we can get the it using various

HttpContext.Current.Request.Url.AbsolutePath or HttpContext.Current.Request.ApplicationPath

I am not sure what are the equivalent in jQuery

reference of asp example

UPDATE:

I have taken a simple approach as i could not find other easy way of doing it

            var _href = $(location).attr('href').toLowerCase()
            var _option1 = 'http://localhost:51407/virtualDir/Default.aspx';
            var _option2 = 'http://www.example./Default.aspx';
            var _option3 = 'http://www.example./';
            if (_href == _option1.toLowerCase() || _href == _option2.toLowerCase() || _href == _option3.toLowerCase()) {
                $(".bar-height").css("min-height", "689px");
               // alert('aa');
            }
            else
            { //alert('bb'); }
Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Sep 2, 2014 at 12:47 LearningLearning 20.1k44 gold badges192 silver badges396 bronze badges 2
  • Try location.pathname. – dfsq Commented Sep 2, 2014 at 12:49
  • See stackoverflow./questions/406192/… – Wilfredo P Commented Sep 2, 2014 at 12:49
Add a ment  | 

2 Answers 2

Reset to default 3

Could you only include the script on the page where it's needed? i.e. only use <script type="text/javascript" src="homepage.js"></script> from default.aspx ?

If not, then, as dfsq said - use window.location.pathname .

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

You could just get the part after the last slash, to account for folder differences...

var page = window.location.toString();
page = page.substring(page.lastIndexOf('/'));

... but this would be true for both example./default.aspx and example./folder1/default.aspx.

Remember, this Javascript is client-side, so there's no equivalent to the C# example you linked.

You could use my approch to know exactly the page (also with urlrouting) to use it in javascript:

I use the body id to identify the page.

javascript code:

$(document).ready(function () {
    if (document.body.id.indexOf('defaultPage') == 0) {
        /*do something*/
    }
});

Asp code:

in masterpage or page (aspx):

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

code behind:

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;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744858545a4597554.html

相关推荐

  • javascript - Excute jquery if url is home page - Stack Overflow

    I need to fire piece of jQuery code only if it is home page.URL probability are.aspxHow can i run cod

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信