Solved

Javascript: Build dynamic URL

Posted on 2009-05-08
5
1,802 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
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
zacspam earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now