Forum Moderators: open

Message Too Old, No Replies

Grabbing part of the current page's URL

Includuing part, but not all, of the query string

         

MatthewHSE

2:43 am on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My site's CMS uses some pretty long query strings when viewing articles. Here's an example:

[mysite.com...]

I have no idea why the script programmers use such a long query string just to view a single article, partucilarly when the link will work with the entire query string omitted back to the itemid part. I guess they have their reasons, though.

The URL's are dynamically generated, technically, but each article will always have the same URL. Additionally, the various "aspects" of the query string (itemid, action, etc.) will be the same for every article. Only the categories and itemid's will be different.

Anyway, I've written a little script that needs to print a URL to the current page. But, for these purposes, I only need the "necessary" part of the URL. Like I said above, you can get to the same page without using the entire query string. Instead of the example above, the following will work:

[mysite.com...]

So is there any way to use JavaScript to get only that part of the current URL, and ignore the rest of the query string?

Thanks in advance,

Matthew

adni18

2:48 am on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try using substrings, or try using the split function.

adni18

2:55 am on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:


montage=window.location.href.split("?");
foo=montage[1].split("itemid=");
roman=foo[1].split("&")[0];
alert(montage[0]+"?itemid="+roman);

MatthewHSE

1:45 pm on Nov 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, thanks, that's just what I needed. I modified your suggestion a bit and came up with the following:

<script type="text/javascript">
fullurl = window.location.href.split("?");
itemid = fullurl[1].split("itemid=");
finalurl = itemid[1].split("&")[0];
link = fullurl[0]+"?itemid="+finalurl;
document.write(link);
</script>

Works great!

code_geek

4:44 am on Nov 17, 2004 (gmt 0)

10+ Year Member



wouldn't this be simpler and work the same way?
note: this script displays an alertbox with the "necessary" part of the current URL

alert(window.location.split("&")[0]);

untested, but seems to me that it shoud work with not problems