Solved

Javascript: Build dynamic URL

Posted on 2009-05-08
5
1,800 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

747 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

15 Experts available now in Live!

Get 1:1 Help Now