Is there a javascript command that will read the url that was called...?

G

Guy Doucet

Hi,

If I have a link to a URL such as <a
href="http://www.mysite.com/somecode.htm?5">Link1</a>, and if I include
javascript in the somecode.htm file, is there a javascript command that I
can use which will read the argument 5?

For that matter, is there anyway to send an argument to the javascript code
of an html link you are calling. Server scripts are not an option for me.

For example, assuming that index.htm contains 50 links, I would like each
link to point to the same html file, but I want that javascript code in that
html file to execute something different depending on which link was
clicked.

My only other alternative would be to create 50 similar html files which to
me looks like amateur or bad coding.

Thanks for all info,

G Doucet
 
L

Lee

Guy Doucet said:
Hi,

If I have a link to a URL such as <a
href="http://www.mysite.com/somecode.htm?5">Link1</a>, and if I include
javascript in the somecode.htm file, is there a javascript command that I
can use which will read the argument 5?

The search attribute of the location object contains the "?" and
whatever follows it, if they exist. Strip off the "?" and you've
got the argument:

<script type="text/javascript">
arg=location.search.replace(/^\?/,"");
alert(arg)
</script>
 
M

Mick White

Guy said:
Hi,

If I have a link to a URL such as <a
href="http://www.mysite.com/somecode.htm?5">Link1</a>, and if I include
javascript in the somecode.htm file, is there a javascript command that I
can use which will read the argument 5?
[snip]

var myQuery=location.search.substring(1);

var myQuery=location.search.split("?")[1];

And more...
Mick
 
R

Randy Webb

Danny said:
Yes, location.search, it returns also the '?', so,
location.search.slice(1) works fine for me.

Nice to see that you have confirmed what was already known. Now, if you
will, please read the group FAQ with regards to posting style,
specifically with regards to top posting.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top