...

View Full Version : Can JavaScript change a link's href?



Bullschmidt
08-26-2003, 11:16 AM
Let's say I have a link that looks like this:

<a name="MyLink" href="otherpage1.htm">Other Page</a>

Is there any JavaScript code I could use (it would perhaps be triggered by the onchange event of a listbox) to change the link to be like this instead (i.e. href of otherpage2.htm)?:

<a name="MyLink" href="otherpage2.htm">Other Page</a>

ellisd5
08-26-2003, 11:24 AM
Dont know about being able to change the href of the "a" tag maybe its possible in IE6.

However you could store the url in a variable then change that variable depending on certain events. so......


<script>
...
var theURL = "www.google.com";
...
</script>

<a href="javascript:document.location=theURL" >Other Page</a>

<!-- other stuff that will change variable -->



The word javascript in the href should be 1 word, for some reason its getting formated and display as two words on this forum, stange!

Bullschmidt
08-26-2003, 11:41 AM
Thanks for the good answer Dale.

The reason I wanted it to be a modified anchor tag is so that the user would still have the option of right-clicking and saving the target as...

So do you know of any JavaScript that can do the same thing as right-clicking on a link and saving target as?

The reason for this is that the site is for a songwriter who wants to put .mp3 files on the Web and let users listen to the song or download the song. And there is a listbox because there are various song lengths involved and the user can choose one.

Kor
08-26-2003, 12:11 PM
try something based on

var link ="otherpage1.htm";
document.write('<a href = \"+link+'\" ...............>')

and whichever function to change the variable link on event.

fredmv
08-26-2003, 01:02 PM
This should work...



<script type="text/javascript">
function update()
{
document.getElementById("link").href = document.f.s.value;
return;
}
</script>

<form name="f">
<select name="s" onchange="update();">
<option value="1.mp3">1.mp3</option>
<option value="2.mp3">2.mp3</option>
<option value="3.mp3">3.mp3</option>
</select>

<a href="1.mp3" id="link">Right Click -> Save Target As...</a>
</form>


Hope that helps you out. :thumbsup:

Bullschmidt
08-27-2003, 12:29 AM
fredmv,

Thanks a lot for your great answer about having a listbox alter the href of a link! I tried it and it works!

fredmv
08-27-2003, 01:34 AM
Hey, no problem. Happy that helped you out. :thumbsup:

glenngv
08-27-2003, 04:14 AM
cross-browser solution:



<script type="text/javascript">
function update(objS)
{
document.links["link"].href = objS.options[objS.selectedIndex].value;
}
</script>
...
<form name="f">
<select name="s" onchange="update(this);">
<option value="1.mp3">1.mp3</option>
<option value="2.mp3">2.mp3</option>
<option value="3.mp3">3.mp3</option>
</select>

<a href="1.mp3" name="link">Right Click -> Save Target As...</a>
</form>

cheesebag
08-27-2003, 05:57 AM
Might try fiddling with this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function processclick(oLink) {
var oSelect = document.forms.songform.elements.songlist;
oLink.href = oSelect.options[oSelect.selectedIndex].value;
return true;
}

</script>
</head>
<body>
<form name="songform">
<select name="songlist">
<option value="BMs.mp3">Bite Me (short version)</option>
<option value="BMd.mp3">Bite Me (dance version)</option>
<option value="BMl.mp3">Bite Me (2 hrs.)</option>
</select>____<a href="BMs.mp3" onclick="return processclick(this)"><font face="comic sans ms">play me!</font></a>
</form>
</body>
</html>

glenngv
08-27-2003, 07:47 AM
using document.execCommand('SaveAs') will save the current document not a particular link.
So the code I posted is I think the better solution. You just have to inform the user that if he wants to save the file instead of running it, he should right-click the link and choose "Save Target As..."

cheesebag
08-27-2003, 06:01 PM
OK, wasn't aware of that. Modified the above, still prefer it to Glenn's. :) Don't think you should inform the user of a specific Internet Explorer procedure - there are alternative methods of saving links (and lots of musicians use Macs).

cordoval
05-03-2008, 07:39 AM
I would like to contribute how I used this discussion:

My goal was to change the popup window address upon an action in a webpage that was being generated by PHP interpreter:


I changed the code of the function like this:

//update links
function update(objS)
{
document.links["varlink"].href = objS.href; //options[objS.selectedIndex]
}

And I change the lines of html document like this:

<a name="varlink" ... >

<script language="javascript">
document.write('<?php echo '<a name="varlink" href="javascript:popupWindow ...>

<a href="javascript:popupWindow('<?php echo 'http://www.
; update(this); return false;"

Sorry if it is confusing. I could not paste the rest of the code.

Encouragements,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum