Casting window.location as string

Discussion in 'Javascript' started by Thomas Allen, Apr 3, 2009.

  1. Thomas Allen

    Thomas Allen Guest

    I was writing a little script today that would add a given class to
    anchors that linked to the current page. It does this by checking the
    last element of window.location split by '/' (of course this would
    typically be accomplished by a server-side language, but some
    designers I work with have to build Dreamweaver templates in straight
    HTML).

    I cast the string for splitting like so because, well, it works:

    urlTokens = (window.location + '').split('/');

    Is there a more elegant way to do this? Or is this the standard way to
    cast objects like window.location as a string?

    Thomas
    Thomas Allen, Apr 3, 2009
    #1
    1. Advertising

  2. Thomas Allen wrote:
    > I was writing a little script today that would add a given class to
    > anchors that linked to the current page. It does this by checking the
    > last element of window.location split by '/' (of course this would
    > typically be accomplished by a server-side language, but some
    > designers I work with have to build Dreamweaver templates in straight
    > HTML).
    >
    > I cast the string for splitting like so because, well, it works:
    >
    > urlTokens = (window.location + '').split('/');
    >
    > Is there a more elegant way to do this? Or is this the standard way to
    > cast objects like window.location as a string?
    >


    window.location is an object. It has several properties.

    https://developer.mozilla.org/En/DOM/Window.location#section_5

    MSDN documentation is not as good here:
    http://msdn.microsoft.com/en-us/library/ms535866(VS.85).aspx

    In your case, window.location.href would work.

    > Thomas


    Garrett
    Garrett Smith, Apr 3, 2009
    #2
    1. Advertising

  3. Thomas Allen

    Thomas Allen Guest

    On Apr 2, 10:33 pm, Garrett Smith <> wrote:
    > Thomas Allen wrote:
    > > I was writing a little script today that would add a given class to
    > > anchors that linked to the current page. It does this by checking the
    > > last element of window.location split by '/' (of course this would
    > > typically be accomplished by a server-side language, but some
    > > designers I work with have to build Dreamweaver templates in straight
    > > HTML).

    >
    > > I cast the string for splitting like so because, well, it works:

    >
    > > urlTokens = (window.location + '').split('/');

    >
    > > Is there a more elegant way to do this? Or is this the standard way to
    > > cast objects like window.location as a string?

    >
    > window.location is an object. It has several properties.
    >
    > https://developer.mozilla.org/En/DOM/Window.location#section_5
    >
    > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/library/ms535866(VS.85).aspx
    >
    > In your case, window.location.href would work.
    >
    > > Thomas

    >
    > Garrett


    Oh, I know, and I already have the script working:

    (forgive me, jQuery haters)

    /**
    * jQuery - Current Class
    *
    * Applies a class name to all anchors within a target element
    * that match the current page file (or other final token from path)
    *
    * Usage: $(target element).currentClass(class name);
    */
    (function($) {
    $.fn.currentClass = function(class) {
    // We need '' to cast window.location as a string
    urlTokens = (window.location + '').split('/');
    finalToken = urlTokens[urlTokens.length - 1];
    if(finalToken.length > 1){
    $(this).find('a[href$=' + finalToken + ']').addClass
    (class);
    }
    };
    }) (jQuery);

    I just was wondering if there is a more elegant/clear way to cast the
    object as a string.

    Thomas
    Thomas Allen, Apr 3, 2009
    #3
  4. Thomas Allen

    Thomas Allen Guest

    On Apr 2, 10:48 pm, Thomas Allen <> wrote:
    > On Apr 2, 10:33 pm, Garrett Smith <> wrote:
    >
    >
    >
    > > Thomas Allen wrote:
    > > > I was writing a little script today that would add a given class to
    > > > anchors that linked to the current page. It does this by checking the
    > > > last element of window.location split by '/' (of course this would
    > > > typically be accomplished by a server-side language, but some
    > > > designers I work with have to build Dreamweaver templates in straight
    > > > HTML).

    >
    > > > I cast the string for splitting like so because, well, it works:

    >
    > > > urlTokens = (window.location + '').split('/');

    >
    > > > Is there a more elegant way to do this? Or is this the standard way to
    > > > cast objects like window.location as a string?

    >
    > > window.location is an object. It has several properties.

    >
    > >https://developer.mozilla.org/En/DOM/Window.location#section_5

    >
    > > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/library/ms535866(VS.85).aspx

    >
    > > In your case, window.location.href would work.

    >
    > > > Thomas

    >
    > > Garrett

    >
    > Oh, I know, and I already have the script working:
    >
    > (forgive me, jQuery haters)
    >
    > /**
    >  * jQuery - Current Class
    >  *
    >  * Applies a class name to all anchors within a target element
    >  * that match the current page file (or other final token from path)
    >  *
    >  * Usage: $(target element).currentClass(class name);
    >  */
    > (function($) {
    >     $.fn.currentClass = function(class) {
    >         // We need '' to cast window.location as a string
    >         urlTokens = (window.location + '').split('/');
    >         finalToken = urlTokens[urlTokens.length - 1];
    >         if(finalToken.length > 1){
    >             $(this).find('a[href$=' + finalToken + ']').addClass
    > (class);
    >         }
    >     };
    >
    > }) (jQuery);
    >
    > I just was wondering if there is a more elegant/clear way to cast the
    > object as a string.
    >
    > Thomas


    Oh, and don't mind those globals behind the curtain, they were there
    for easy console debugging (rather than dropping into Firebug's
    debugger).

    Thomas
    Thomas Allen, Apr 3, 2009
    #4
  5. Thomas Allen

    RobG Guest

    On Apr 3, 12:48 pm, Thomas Allen <> wrote:
    > On Apr 2, 10:33 pm, Garrett Smith <> wrote:
    >
    >
    >
    > > Thomas Allen wrote:
    > > > I was writing a little script today that would add a given class to
    > > > anchors that linked to the current page. It does this by checking the
    > > > last element of window.location split by '/' (of course this would
    > > > typically be accomplished by a server-side language, but some
    > > > designers I work with have to build Dreamweaver templates in straight
    > > > HTML).

    >
    > > > I cast the string for splitting like so because, well, it works:

    >
    > > > urlTokens = (window.location + '').split('/');

    >
    > > > Is there a more elegant way to do this? Or is this the standard way to
    > > > cast objects like window.location as a string?

    >
    > > window.location is an object. It has several properties.

    >
    > >https://developer.mozilla.org/En/DOM/Window.location#section_5

    >
    > > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/library/ms535866(VS.85).aspx

    >
    > > In your case, window.location.href would work.


    [...]

    > Oh, I know, and I already have the script working:


    [...]

    >         urlTokens = (window.location + '').split('/');


    You've been told: window.location is an object that is not covered by
    any specification (it falls under DOM 0). The line above depends on
    its toString method returning the property you want. Why would you
    leave it to chance when you know the property you want explicitly?

    urlTokens = window.location.href.split('/');

    [...]

    > I just was wondering if there is a more elegant/clear way to cast the
    > object as a string.


    A clearer, more elegant method to get the property value has been
    demonstrated. There is no need to "cast" it as a string, the property
    you seek *is* a string.


    --
    Rob
    RobG, Apr 3, 2009
    #5
  6. Thomas Allen

    Thomas Allen Guest

    On Apr 2, 11:40 pm, RobG <> wrote:
    > On Apr 3, 12:48 pm, Thomas Allen <> wrote:
    >
    >
    >
    > > On Apr 2, 10:33 pm, Garrett Smith <> wrote:

    >
    > > > Thomas Allen wrote:
    > > > > I was writing a little script today that would add a given class to
    > > > > anchors that linked to the current page. It does this by checking the
    > > > > last element of window.location split by '/' (of course this would
    > > > > typically be accomplished by a server-side language, but some
    > > > > designers I work with have to build Dreamweaver templates in straight
    > > > > HTML).

    >
    > > > > I cast the string for splitting like so because, well, it works:

    >
    > > > > urlTokens = (window.location + '').split('/');

    >
    > > > > Is there a more elegant way to do this? Or is this the standard wayto
    > > > > cast objects like window.location as a string?

    >
    > > > window.location is an object. It has several properties.

    >
    > > >https://developer.mozilla.org/En/DOM/Window.location#section_5

    >
    > > > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/library/ms535866(VS.85).aspx

    >
    > > > In your case, window.location.href would work.

    >
    > [...]
    >
    > > Oh, I know, and I already have the script working:

    >
    > [...]
    >
    > >         urlTokens = (window.location + '').split('/');

    >
    > You've been told: window.location is an object that is not covered by
    > any specification (it falls under DOM 0).  The line above depends on
    > its toString method returning the property you want.  Why would you
    > leave it to chance when you know the property you want explicitly?
    >
    >   urlTokens = window.location.href.split('/');
    >
    > [...]
    >
    > > I just was wondering if there is a more elegant/clear way to cast the
    > > object as a string.

    >
    > A clearer, more elegant method to get the property value has been
    > demonstrated. There is no need to "cast" it as a string, the property
    > you seek *is* a string.
    >
    > --
    > Rob


    Thanks, I wasn't aware of the href attribute of window.location.
    That's much better.

    Thomas
    Thomas Allen, Apr 3, 2009
    #6
  7. Thomas Allen <> writes:

    > I cast the string for splitting like so because, well, it works:
    >
    > urlTokens = (window.location + '').split('/');
    >
    > Is there a more elegant way to do this? Or is this the standard way to
    > cast objects like window.location as a string?


    Like others have said, location.href is a string to begin with, so it
    is better for this particular case.

    In the general case, the this is one of the simpler ways to convert a
    value to a string. The best alternative is using the String function,
    e.g., "String(window.location)".

    /L
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Apr 3, 2009
    #7
  8. Thomas Allen wrote:
    > On Apr 2, 11:40 pm, RobG <> wrote:
    >>>> [window.location.href]
    >>> I just was wondering if there is a more elegant/clear way to cast the
    >>> object as a string.

    >> A clearer, more elegant method to get the property value has been
    >> demonstrated. There is no need to "cast" it as a string, the property
    >> you seek *is* a string.
    >> [snipped 50+ lines of irrelevance]


    For goodness (and your own) sake, learn to quote!

    <http://jibbering.com/faq/#posting>
    <http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Post> pp.

    > Thanks, I wasn't aware of the href attribute of window.location.
    > That's much better.


    You surely "actually do know [your] way around JavaScript fairly well".[1]

    Hm, hm.


    SCNR

    PointedEars
    ___________
    [1] <news:>
    Thomas 'PointedEars' Lahn, Apr 3, 2009
    #8
  9. Thomas Allen

    Thomas Allen Guest

    On Apr 3, 3:29 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > Thomas Allen wrote:
    > > On Apr 2, 11:40 pm, RobG <> wrote:
    > >>>> [window.location.href]
    > >>> I just was wondering if there is a more elegant/clear way to cast the
    > >>> object as a string.
    > >> A clearer, more elegant method to get the property value has been
    > >> demonstrated. There is no need to "cast" it as a string, the property
    > >> you seek *is* a string.
    > >> [snipped 50+ lines of irrelevance]

    >
    > For goodness (and your own) sake, learn to quote!
    >
    > <http://jibbering.com/faq/#posting>
    > <http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Post> pp.
    >
    > > Thanks, I wasn't aware of the href attribute of window.location.
    > > That's much better.

    >
    > You surely "actually do know [your] way around JavaScript fairly well".[1]
    >
    > Hm, hm.
    >
    > SCNR


    JavaScript != the DOM.

    Thomas
    Thomas Allen, Apr 3, 2009
    #9
  10. Thomas Allen wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> Thomas Allen wrote:
    >>> On Apr 2, 11:40 pm, RobG <> wrote:
    >>>>>> [window.location.href]
    >>>>> I just was wondering if there is a more elegant/clear way to cast the
    >>>>> object as a string.
    >>>> A clearer, more elegant method to get the property value has been
    >>>> demonstrated. There is no need to "cast" it as a string, the property
    >>>> you seek *is* a string.
    >>>> [snipped 50+ lines of irrelevance]

    >> For goodness (and your own) sake, learn to quote!
    >>
    >> <http://jibbering.com/faq/#posting>
    >> <http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Post> pp.
    >>
    >>> Thanks, I wasn't aware of the href attribute of window.location.
    >>> That's much better.

    >> You surely "actually do know [your] way around JavaScript fairly well".[1]
    >>
    >> Hm, hm.
    >>
    >> SCNR


    At least you show promise as you did not quote the signature. Now you only
    need to find out how trim quotes to the relevant minimum.

    > JavaScript != the DOM.


    LOL. You're preaching to the choir; Google is your friend. [psf 6.1]

    However, if you knew your JavaScript 101 (and you have provided another
    proof that you don't by using the term "attribute" for what is a property,
    despite correction), you would know that window.location and
    window.location.href are features that have been introduced with JavaScript
    1.0, and were moved to the Gecko DOM only after JavaScript 1.5 emerged.

    Of course, JScript and other ECMAScript implementations never had them,
    Microsoft started later than Netscape (in fact, they made a JavaScript
    copycat and called it differently to avoid lawsuits and license fees) and
    did that right for a change.

    I have said it so many times here and elsewhere before that I lost count.
    But to you everything already said and done a hundred times is brand-new
    despite the archives, isn't?

    And, let that be clear, you don't know your way around the DOMs either,
    otherwise you wouldn't be using/recommending jQuery (Resig himself hasn't
    got a minimum clue about it to begin with.)


    PointedEars
    Thomas 'PointedEars' Lahn, Apr 3, 2009
    #10
  11. Thomas Allen

    Thomas Allen Guest

    On Apr 3, 9:05 am, Thomas 'PointedEars' Lahn <>
    wrote:
    > Thomas Allen wrote:
    > > Thomas 'PointedEars' Lahn wrote:
    > >> Thomas Allen wrote:
    > >>> On Apr 2, 11:40 pm, RobG <> wrote:
    > >>>>>> [window.location.href]
    > >>>>> I just was wondering if there is a more elegant/clear way to cast the
    > >>>>> object as a string.
    > >>>> A clearer, more elegant method to get the property value has been
    > >>>> demonstrated. There is no need to "cast" it as a string, the property
    > >>>> you seek *is* a string.
    > >>>> [snipped 50+ lines of irrelevance]
    > >> For goodness (and your own) sake, learn to quote!

    >
    > >> <http://jibbering.com/faq/#posting>
    > >> <http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Post> pp.

    >
    > >>> Thanks, I wasn't aware of the href attribute of window.location.
    > >>> That's much better.
    > >> You surely "actually do know [your] way around JavaScript fairly well"..[1]

    >
    > >> Hm, hm.

    >
    > >> SCNR

    >
    > At least you show promise as you did not quote the signature.  Now you only
    > need to find out how trim quotes to the relevant minimum.
    >
    > > JavaScript != the DOM.

    >
    > And, let that be clear, you don't know your way around the DOMs either,


    Er, I just admitted that I don't know the DOM very well. My point was
    that I know the language itself reasonably well but typically use a
    library to further abstract the DOM.

    Thomas
    Thomas Allen, Apr 3, 2009
    #11
  12. Thomas Allen wrote:
    > Thomas 'PointedEars' Lahn wrote:
    >> Thomas Allen wrote:
    >>> Thomas 'PointedEars' Lahn wrote:
    >>>> Thomas Allen wrote:
    >>>>> On Apr 2, 11:40 pm, RobG <> wrote:
    >>>>>>>> [window.location.href]
    >>>>>>> I just was wondering if there is a more elegant/clear way to cast the
    >>>>>>> object as a string.
    >>>>>> A clearer, more elegant method to get the property value has been
    >>>>>> demonstrated. There is no need to "cast" it as a string, the property

    ^^^^^^^^
    >>>>>> you seek *is* a string.
    >>>>>> [...]
    >>>>> Thanks, I wasn't aware of the href attribute of window.location.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>>>> That's much better.
    >>>> You surely "actually do know [your] way around JavaScript fairly well"..[1]
    >>>> Hm, hm.
    >>>> SCNR

    >> At least you show promise as you did not quote the signature. Now you only
    >> need to find out how trim quotes to the relevant minimum.
    >>
    >>> JavaScript != the DOM.


    The relevant part that you snipped was:

    >> However, if you knew your JavaScript 101 (and you have provided another
    >> proof that you don't by using the term "attribute" for what is a
    >> property, despite correction), you would know that window.location and
    >> window.location.href are features that have been introduced with
    >> JavaScript 1.0, and were moved to the Gecko DOM only after JavaScript 1.5
    >> emerged.


    Who are you trying to fool here?

    >> And, let that be clear, you don't know your way around the DOMs either,

    >
    > Er, I just admitted that I don't know the DOM very well. My point was
    > that I know the language itself reasonably well but typically use a
    > library to further abstract the DOM.


    Can you read? *Evidently* (see above) you don't know your way around either
    one. And if that wording is too confusing for your little mind: you do not
    know your way around both.


    PointedEars
    Thomas 'PointedEars' Lahn, Apr 3, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,781
    Andrew McDonagh
    Jan 8, 2005
  2. Wally Barnes
    Replies:
    3
    Views:
    507
    Wally Barnes
    Nov 20, 2008
  3. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    547
    James Kanze
    Dec 20, 2009
  4. Bernd Liebermann
    Replies:
    3
    Views:
    145
    Erwin Moller
    Feb 11, 2004
  5. saiho.yuen
    Replies:
    3
    Views:
    401
    kaeli
    Sep 14, 2004
Loading...

Share This Page