?
Solved

Javascript: Build dynamic URL

Posted on 2009-05-08
5
Medium Priority
?
1,810 Views
Last Modified: 2013-11-19
Lets say in the same page I have::

<a href="javascript:;" onclick="somefunction();">23</a>

<a href="page.asp?number=">Enter</a>


How do I manipulate the href string in javscript so that 23 is inserted and URL becomes:

<a href="page.asp?number=23">Enter</a>
0
Comment
Question by:fcqmax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
zacspam earned 2000 total points
ID: 24337785
Well, to make it easier, I am going to add some id's to your code so we can grabt hem faster.  Then, you can use the function document.getElementById() to get a reference to the Enter anchor tag.  After that, you can set the .href attribute to whatever you want.
 
In the code below, I pass this.innerHTML to somefunction.  That will send whatever is between the <a> and </a> tags, which in this case is 23.  You could also change it to just send 23 on its own without being dynamic.  
In somefunction(), I get areference to the link we want to change and then set the href property to the link you wanted plus what was passed in.
 
Hope that helps!

<html><body>
<a href="javascript:;" onclick="somefunction(this.innerHTML);">23</a>
<a href="page.asp?number=" id="link">Enter</a>
 
<script type="text/javascript">
function somefunction(s) {
	document.getElementById("link").href="page.asp?number=" + s;
}
</script>
 
</body></html>

Open in new window

0
 

Author Comment

by:fcqmax
ID: 24341759
How about just appending a string? Is it possible to do something like:

document.getElementById("link").href = document.getElementById("link").href + s;


0
 

Author Comment

by:fcqmax
ID: 24341802
nvm, that works also. thanks
0
 

Author Closing Comment

by:fcqmax
ID: 31579514
Thanks.
0
 

Expert Comment

by:RJHarvey72
ID: 24411730
this code works for some of my asp pages. but what about pages that requiere multiple parameters to be passed.

can you use

<a href="page.asp?number=" id="link">Enter</a>

technique to pass more than one parameter tot he asp page?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question