7

I have a simple Javascript function which builds a Url that I want to provide a link to.

However, I can't seem to get the anchor tag working with it. How do I assign the href of the anchor tag the results of the Javascript function?

Neither one of these work correctly:

<a href="getUrl();">Click here</a>
<a href="javascript:getUrl();">Click here</a>

This is what I want to accomplish.

6 Answers 6

13
<script type="text/javascript">
    function getUrl()
    {
        return "http://www.google.com";
    }
</script>
<a href="javascript:document.location.href=getUrl();">Click here</a>

-- update --

If I wanted to incorporate user278064s' comments, i would change the above into:

  <script type="text/javascript">
        function getUrl()
        {
            return "http://www.google.com";
        }
    </script>
    <a href="#" onClick="document.location.href=getUrl();">Click here</a>
Sign up to request clarification or add additional context in comments.

2 Comments

is one of these considered to be better practice?
@miss.serena: Neither would win a beauty contest, but the 2nd one would be considered better back in the days. Because it keeps your statusbar more clean, without resorting to onMouseOver/onMouseOut code which rewrites the status bar. But who uses the status bar anyway these days? ;)
1

None of the above solutions worked for me. A good way would be to create a new link in the function.

function fetchURL() {
  var title = "Download";
  var URL = title.link("https://www.google.com");
  document.getElementById("dynamicButton").innerHTML = URL;

}
<body onload="fetchURL()">
  <div id="dynamicButton">
    //empty
  </div>

Comments

0
<a onclick="getUrl();" href="#">Click here</a>

Click Here

1 Comment

This does absolutely nothing.
0

Give the link an id:

<a id="specialLink">Click Here</a>

Later on, set its href from JavaScript:

document.getElementById('specialLink').href = getUrl();

(You might want to include a placeholder href in the link which people with JavaScript disabled will see.)

2 Comments

What does 'later on' mean? When the page loads, I want this link's href to point to the correct location so that when the link is clicked it takes the user to where I want them to go. There really is no 'later on'.
@Mike “later on” just means “in any JavaScript which runs after the link on the page”. It could be a JavaScript file included at the end of the document, a function that runs when the page is ready, or a <script> block that you put just below the link.
0
function getUrl(that) {
   return "www.whateveryouwant.com";
}

// Point the a.href attribute at your url.
var a = document.getElementsByTagName('a')[0];
a.href = getUrl();

UPDATE

I assume that you want to use the getUrl() method to set the href attribute, because probably the pointed url is not static (so it could change at any moment e point to the getUrl() returned url.)

Anyway, you could assign the href attribute, when i.e. the user click on the link, in this way.

function changeHref(aElem) {
   aElem.href = getUrl();
}

Following the complete 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 other thing. You should avoid the use of javascript: pseudo-protocol.

This fragment will explain you why:

A pseudo-protocol is a nonstandard take on this idea. The javascript: pseudo-protocol is supposed to be used to invoke JavaScript from within a link. Here’s how the javascript: pseudo-protocol would be used to call the popUp function:

<a href="javascript:popUp('http://www.example.com/');">Example</a>

This will work just fine in browsers that understand the javascript: pseudo-protocol. Older browsers, however, will attempt to follow the link and fail. Even in browsers that understand the pseudoprotocol, the link becomes useless if JavaScript has been disabled. In short, using the javascript: pseudo-protocol is usually a very bad way of referencing JavaScript from within your markup.

DOM Scripting: Web Design with JavaScript and the Document Object Model: Second Edition

1 Comment

I don't understand where the function should be called from. When the page loads, I want the link to contain the proper address in the href. Where does the code which you have outside of the function definition go? How does the <a href=???> get assigned?
0

The following worked for me

<script type="text/javascript">
   $(function() {
        document.getElementById("aTag").href = getUrl();
   });
</script>

<a id="aTag">Click Here</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.