View Full Version : help parsing window.location.href
wkenny
08-25-2005, 02:27 PM
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
jscheuer1
08-25-2005, 02:46 PM
<script type="text/javascript">
thePage=unescape(location.href)
thePage=thePage.substr(thePage.lastIndexOf('/')+1)
thePage=thePage.substr(0,thePage.indexOf('.')-1)
alert(thePage)
</script>Not foolproof but, good for normal page names without query strings* containing the '/' character.
*query strings follow the page name with a '?' and data, ex:
thesite/thefolder/thesubfolder/thepage.htm?var1=password
jscheuer1
08-25-2005, 03:17 PM
<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>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.
wkenny
08-25-2005, 03:35 PM
Just the trick. Thanks a lot, John
Billy
jscheuer1
08-25-2005, 03:40 PM
Actually, looking at it again, my second version will still fail on query strings containing ('/'). Use:
<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>
Or just use %2f instead of / in GET query strings, like you're meant to.
jscheuer1
08-25-2005, 04:16 PM
After it is unescaped, what good would that do?
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.
jscheuer1
08-25-2005, 04:24 PM
I think unescaping it in the first place is the safest route, no telling what you are dealing with before that.
You can't escape the path, only the page. Split the page down, then unescape it. You needn't worry about the path.
jscheuer1
08-25-2005, 04:57 PM
You are right in part but, going back to where you said:
Or just use %2f instead of / in GET query strings, like you're meant toI would, of course, always do that :rolleyes: . However, I have no control over what others have or may do. Still best to unescape first and deal with a known quantity than to hope everybody else is playing by the rules and basing your code on that.
Of course they'll follow it... it's a convention to follow the standard ;)
Due to it being a standard, I'd say it's safe to rely on it being followed. It might not work, but no-one will blame you. :)
jscheuer1
08-25-2005, 06:09 PM
Why is there never a cold haddock around when you need one? :confused: ;)
Pfft, the congor eel beats it every time. ;)
Massive long things, great range. And they keep wriggling after you gut them. There's nothing worse to be hit in the face with than a long, cold, wet, headless, wriggling eel. :p
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.