Casting window.location as string

T

Thomas Allen

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
 
G

Garrett Smith

Thomas said:
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.

Garrett
 
T

Thomas Allen

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.


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
 
T

Thomas Allen

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

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.

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
 
R

RobG

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

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.
 
T

Thomas Allen

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.

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

Thomas
 
L

Lasse Reichstein Nielsen

Thomas Allen said:
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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[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>
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:29805adc-4ebe-49a8-8d0f-d966ecf680a2@y13g2000yqn.googlegroups.com>
 
T

Thomas Allen

Thomas said:
[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>
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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Thomas said:
Thomas said:
[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>
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
 
T

Thomas Allen

Thomas said:
Thomas said:
Thomas Allen 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
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Thomas said:
Thomas said:
Thomas 'PointedEars' Lahn wrote:
Thomas Allen 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:

Who are you trying to fool here?
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top