Forum Moderators: open

Message Too Old, No Replies

how to extract parameter from URL

using JS

         

Tastatura

11:08 pm on May 5, 2007 (gmt 0)

10+ Year Member



Hi all,
JS is not my strongest side and this might be easy for some of you, but I am having trouble extracting parameter from URL (after page loads)

Looking at:
http://example.com?a=123&b=456&c=789#111

I am trying to get vale for "c" and everything behind it, ie c=789#111 .

using document.write(window.location.search)

produces everything but #111 (and I really want that part as well)

Any help is greatly appriciated

whoisgregg

1:15 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a thread [webmasterworld.com] from waaay back that has a good solution for parsing the query string. Here's another thread [webmasterworld.com] with a different way of solving the problem. :)

Tastatura

4:25 pm on May 7, 2007 (gmt 0)

10+ Year Member



Thanks for the info whoisgregg.
I don't think that the two examples will work for me because they don't capture fragment (values after ' # '). Maybe I didn't explain my situation well - after page loads I want to extract full URI from navigation bar using code on that same page. I am able to extract any parameters I want form the URI (see code example below) except for '#'.

I did try using indexOf('#') but it didn't work, and that is what I want.

Following script looks at search string, and returns value of target parameter (in the example below that would be ' q ') and then exits. It could be easily modified to return all search parameters.


<script type="text/javascript">
function get_search_string()
{
var fullurl=window.location.search
var param=(fullurl.slice(1))

var pairs = param.split("&")
var targetvar = "q"

for(var i = 0; i < pairs.length; i++)
{
var pos = pairs[i].indexOf('=')
if (pos == -1) continue
var argname = pairs[i].substring(0,pos)
var value = pairs[i].substring(pos+1)
if (argname == targetvar)
{
var searchstring = value
{break}
}
}

return searchstring
}

Little_G

4:35 pm on May 7, 2007 (gmt 0)

10+ Year Member



Hi,

You can get the #111 with window.location.hash [developer.mozilla.org]

Andrew

whoisgregg

5:01 pm on May 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, Tastatura. I missed the part about you needing the hash. :/ Little_G has the right information for you. :)