function getUrl(that) { return "www.whateveryouwant.com"; }
UPDATE
I assume that you want to use the getUrl() method to set the href attribute, because perhaps the specified pointer is not static (therefore, it can change at any time by e-mail to the getUrl () URL.)
In any case, you can assign the href attribute when the user clicks on the link in this way.
function changeHref(aElem) { aElem.href = getUrl(); }
Following the full code:
<a href="#" onclick="changeHref(this);">click me!</a> <script> function getUrl(that) { return "www.whateveryouwant.com"; } function changeHref(aElem) { aElem.href = getUrl(); } </script>
One more thing. You should avoid using javascript: pseudo-protocol .
This snippet will explain to you why:
A pseudo-protocol is a non-standard approach to this idea. The javascript: pseudo-protocol is assumed to be used to invoke JavaScript from a link. Here's how to use javascript: a pseudo protocol to call the popUp function:
<a href="javascript:popUp('http://www.example.com/');">Example</a>
This will work just fine in browsers that understand javascript: pseudo-protocol. However, older browsers will try to follow the link and fail. Even in browsers that understand the pseudo-protocol, the link becomes useless if JavaScript is disabled. In short, using the javascript pseudo protocol: usually a very poor way to reference JavaScript from your markup.
DOM Scripting: JavaScript Web Design and Document Object Model: Second Edition
source share