robinton
robinton

Reputation: 75

How to set this.getAttribute('href') in this javascript?

I am trying to load a link after 4 seconds after click. Javascript and link are below

 <script>
 function myFunction()
 {
 setTimeout(function(){window.location.assign(this.getAttribute('href'))},4000);
 }
 </script>

<a href="http://www.foo.com" onclick="myFunction(); return false">

But its not working. How can i solve this. Thanks for your answers.

Upvotes: 0

Views: 5381

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to pass the clicked element reference to the click handler

<a href="http://www.foo.com" onclick="myFunction(this); return false">

then

function myFunction(el) {
    setTimeout(function () {
        window.location.assign(el.getAttribute('href'))
    }, 4000);
}

or using jQuery

function myFunction(el) {
    setTimeout(function () {
        window.location.assign($(el).attr('href'))
    }, 4000);
}

Upvotes: 7

Related Questions