Solved

Javascript: Build dynamic URL

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

623 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