Can anybody tell me how to parse window.location.href to get the page name.
e.g. thesite/thefolder/thesubfolder/thepage.htm
and I want to return "thepage"
I will not know in advance how deep the page name is.
Thanks
Can anybody tell me how to parse window.location.href to get the page name.
e.g. thesite/thefolder/thesubfolder/thepage.htm
and I want to return "thepage"
I will not know in advance how deep the page name is.
Thanks
Not foolproof but, good for normal page names without query strings* containing the '/' character.Code:<script type="text/javascript"> thePage=unescape(location.href) thePage=thePage.substr(thePage.lastIndexOf('/')+1) thePage=thePage.substr(0,thePage.indexOf('.')-1) alert(thePage) </script>
*query strings follow the page name with a '?' and data, ex:
thesite/thefolder/thesubfolder/thepage.htm?var1=password
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
This one will work even with dots ('.') in the filename and with query strings containing ('/'). There could be other considerations I've missed for other potential location.href values.Code:<script type="text/javascript"> thePage=unescape(location.href) thePage=thePage.indexOf('?')==-1? thePage.substr(thePage.lastIndexOf('/')+1) : thePage.substring(thePage.lastIndexOf('/')+1,thePage.indexOf('?')) thePage=thePage.substr(0,thePage.lastIndexOf('.')-1) alert(thePage) </script>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Just the trick. Thanks a lot, John
Billy
Actually, looking at it again, my second version will still fail on query strings containing ('/'). Use:
Code:<script type="text/javascript"> thePage=unescape(location.href) if(thePage.indexOf('?')!==-1) thePage=thePage.substring(0,thePage.indexOf('?')) thePage=thePage.substr(thePage.lastIndexOf('/')+1) thePage=thePage.substr(0,thePage.lastIndexOf('.')) alert(thePage) </script>
Last edited by jscheuer1; 08-25-2005 at 04:08 PM.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Or just use %2f instead of / in GET query strings, like you're meant to.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
After it is unescaped, what good would that do?
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
None... but who mentioned unescaping it?
/EDIT: Oh wait, you did. I see why; perhaps unescape it after getting the appropriate portion? Check for ., then if it's -1, use %2e instead.
Last edited by Twey; 08-25-2005 at 04:23 PM.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
I think unescaping it in the first place is the safest route, no telling what you are dealing with before that.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
You can't escape the path, only the page. Split the page down, then unescape it. You needn't worry about the path.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
Bookmarks