Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor with "computed" URL

I have a JS script that appends to the HTML page the pairs: input + anchor

Can I compute the URL before the redirect happens?

Now I have link that looks like this:

<a href="#" onclick="myFunct();return false;">link</a>

and myFunct uses window.location.href to redirect webpage. The problem with this approach is that I cannot (obviously) CTRL+click on the link for opening goal link in a new tab.

Details:

  • The link URL is known after obtaining the URL from the server - this operation is very expensive for me and I would like to do that only in case it is absolutely necessary.

  • The idea is: user chooses a link, he/she clicks it, url is obtained from server and user is redirected (in the same window or in a new tab if he/she uses CTRL+click)

Thanks!

like image 743
Martin Vseticka Avatar asked Mar 07 '12 08:03

Martin Vseticka


2 Answers

Im not entirely certain this is what you are looking for by try giving your anchor an id

<a href="#" id="mylink">link</a>

then just update the href attribute to change the new location (perhaps on change on an input ?

<input onchange="changeLink(this)" value="http://www.google.com/"/>

function changeLink(elem) {
    var mylink = document.getElementById('mylink');
    mylink.href = this.value;  // you could prepend your value with a domain
}

Every time the value in the input changes the href will be changed to match

like image 102
Manse Avatar answered Sep 27 '22 17:09

Manse


instead of navigating with the onmousedown, you can just rewrite the href

<a href='http://www.google.com' id='computed_link' onmousedown='handleClick()'>Computed Link!!</a>
<script type='text/javascript'>
function handleClick()
{
    document.getElementById('computed_link').href = 'http://www.yahoo.com';
}
</script>
like image 42
trex005 Avatar answered Sep 27 '22 18:09

trex005