Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Casting window.location as string

Reply
Thread Tools

Casting window.location as string

 
 
Thomas Allen
Guest
Posts: n/a
 
      04-03-2009
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
 
Reply With Quote
 
 
 
 
Garrett Smith
Guest
Posts: n/a
 
      04-03-2009
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...tion#section_5

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

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

> Thomas


Garrett
 
Reply With Quote
 
 
 
 
Thomas Allen
Guest
Posts: n/a
 
      04-03-2009
On Apr 2, 10:33*pm, Garrett Smith <(E-Mail Removed)> 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...tion#section_5
>
> MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/libr...66(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
 
Reply With Quote
 
Thomas Allen
Guest
Posts: n/a
 
      04-03-2009
On Apr 2, 10:48*pm, Thomas Allen <(E-Mail Removed)> wrote:
> On Apr 2, 10:33*pm, Garrett Smith <(E-Mail Removed)> 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...tion#section_5

>
> > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/libr...66(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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      04-03-2009
On Apr 3, 12:48*pm, Thomas Allen <(E-Mail Removed)> wrote:
> On Apr 2, 10:33*pm, Garrett Smith <(E-Mail Removed)> 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...tion#section_5

>
> > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/libr...66(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
 
Reply With Quote
 
Thomas Allen
Guest
Posts: n/a
 
      04-03-2009
On Apr 2, 11:40*pm, RobG <(E-Mail Removed)> wrote:
> On Apr 3, 12:48*pm, Thomas Allen <(E-Mail Removed)> wrote:
>
>
>
> > On Apr 2, 10:33*pm, Garrett Smith <(E-Mail Removed)> 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...tion#section_5

>
> > > MSDN documentation is not as good here:http://msdn.microsoft.com/en-us/libr...66(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
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      04-03-2009
Thomas Allen <(E-Mail Removed)> 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'

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-03-2009
Thomas Allen wrote:
> On Apr 2, 11:40 pm, RobG <(E-Mail Removed)> 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:(E-Mail Removed)>
 
Reply With Quote
 
Thomas Allen
Guest
Posts: n/a
 
      04-03-2009
On Apr 3, 3:29*am, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> Thomas Allen wrote:
> > On Apr 2, 11:40 pm, RobG <(E-Mail Removed)> 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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      04-03-2009
Thomas Allen wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Thomas Allen wrote:
>>> On Apr 2, 11:40 pm, RobG <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
casting from 'const string' to a 'non-constant string' Rene Ivon Shamberger C++ 2 10-19-2012 06:42 PM
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
Casting Object into String[ ]? Nathan ASP .Net 1 10-23-2003 07:33 AM



Advertisments