4

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

URL probability are

http://www.example.com
http://www.example.com/
http://www.example.com/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.net 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.net 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.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'); }
Community
  • 1
  • 1
Learning
  • 19,469
  • 39
  • 180
  • 373

2 Answers2

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.com/default.aspx and example.com/folder1/default.aspx.

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

Dave Salomon
  • 3,287
  • 1
  • 17
  • 29
0

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.net 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;
}
giammin
  • 18,620
  • 8
  • 71
  • 89
  • Thanks i could do same in asp.net code behind but i am looking for solution using jquery. This approach can be a way around – Learning Sep 02 '14 at 13:01
  • @KnowledgeSeeker i use this with jquery. I use asp.net only to know the exact page also in case that the urlrouting is used. – giammin Sep 02 '14 at 13:02