homepage Welcome to WebmasterWorld Guest from 54.242.18.190
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
setting URL hash to '0'
sssweb




msg:4451363
 3:56 pm on May 9, 2012 (gmt 0)

I have a script that reads the URL hash & uses the var for slideshow processing. It glitches whenever there's an on-click event that adds a '#' to the URL w/o a number after it. I just need a line of code to set var index = 0 when this occurs. Here's the current code:

var index = 0, hash = window.location.hash;
if (hash) {
index = /\d+/.exec(hash)[0];
index = (parseInt(index) || 1) - 1;
}

I tried this, but it doesn't work:

if (!index) {index = 0;}

 

sssweb




msg:4451402
 4:51 pm on May 9, 2012 (gmt 0)

If it helps, here's the script error I get [on line: index = /\d+/.exec(hash)[0]]:

Unable to get value of the property '0': object is null or undefined

Fotiman




msg:4451444
 5:59 pm on May 9, 2012 (gmt 0)


Unable to get value of the property '0': object is null or undefined


You're trying to get the item at index 0 of something that returns null. So just add a bit more logic to handle that case.


var index = 0, hash = window.location.hash;
if (hash) {
index = /\d+/.exec(hash);
if (index && index.length > 0) {
index = (parseInt(index[0]) || 1) - 1;
}
else {
index = 0;
}
}

sssweb




msg:4451455
 6:35 pm on May 9, 2012 (gmt 0)

I see your logic, but index still comes out undefined for some reason; also, I may have misdirected you -- since the script error comes on the exec(hash) line, the added conditional should probably go with 'if (hash)'

(BTW, I think you got a typo in parseInt(index[0].... - no '[0]' in the orig)

Fotiman




msg:4451484
 7:26 pm on May 9, 2012 (gmt 0)

No, there is no typo. Here's the original:

index = /\d+/.exec(hash)[0];
index = (parseInt(index) || 1) - 1;

In my example, the [0] has moved from that first line to the second one. Here's what your code does:

/\d+/.exec(hash)
That will return a result array or null. In the case that a match is found, an array is returned, and you were then accessing the item at index 0:
/\d+/.exec(hash)[0]
However, in the case that no match is found, it returns null, therefore you receive the error when you try to access the item at index 0: "Unable to get value of the property '0': object is null or undefined".

My solution was to split these into 2 calls:
index = /\d+/.exec(hash);
// index now equals an array or null, so check to see which:
if (index && index.length > 0) {
// if index is null, then no results found and we'll never
// reach the next line (note, checking the length is not
// really needed, but I included it to make it more obvious
// that we're expecting an array
//index = (parseInt(index) || 1) - 1;
index = (parseInt(index[0]) || 1) - 1;
// Since index is an array, call parseInt on the first item
// in the array (index[0]). This is essentially the same as
// what your code did, we've just moved the checking of the
// array value to after a small validation that it's an array

sssweb




msg:4451779
 12:33 pm on May 10, 2012 (gmt 0)

Oh - I didn't notice that you dropped the '[0]' from the exec(hash) line. Works now. Thanks Foti -- you're the man! And again, thanks for taking the time to fully explain it.

Fotiman




msg:4451815
 1:47 pm on May 10, 2012 (gmt 0)

Glad to help. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved