0

I created a button that links to rederect.html and in there i created a link using <a href> that has to be auto-clicked to go to index.html.

So when you are on index and used the button to get there you go to the rederect.html and automatically go back to index.html because the a href has to auto-trigger. The only problem is that the <a href> does not auto-trigger.

This is what i got so far:

<a href="index.html" class="banan" id="banan">Succesvol uitgelogt return naar login.</a>

<script type="text/javascript">
  window.onload = function() {
    $(function(){
      window.location.href = $('.banan').attr('index.html');
    });
  }
</script>

I used the window.onload event to try and trigger the function in there to trigger when you get on the redirect page. but it does not autorederect/click the <a href> or give back any errors so i really don't know how to fix this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
B. Dionys
  • 916
  • 7
  • 34
  • Possible duplicate of [How do I programmatically click a link with javascript?](http://stackoverflow.com/questions/902713/how-do-i-programmatically-click-a-link-with-javascript) – BSMP Nov 03 '16 at 16:14
  • uitgelocht? Bijna goed man:P – Bob Brinks Nov 03 '16 at 16:20

4 Answers4

4

To get the attribute's value by using the function .attr you need to pass the attribute name which is href.

The .attr() method gets the attribute value for only the first element in the matched set

So:

window.location.href = $('.banan').attr('href');
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
1

If you're already using jQuery, just use $(location).attr('href',url);. window.location.href seems to have inconstant behavior in some browsers, in fact, it flat out doesn't work in my version of Firefox. I've heard tell of setting window.location directly not working in versions of IE.

Esdras Suy
  • 39
  • 1
  • 7
1

Try this:-

 document.getElementById('yourLinkID').click();
Razia sultana
  • 2,168
  • 3
  • 15
  • 20
1

How about trying something like this-

<a href="index.html" class="banan" id="banan">Succesvol uitgelocht return naar login.</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script type="text/javascript">
 window.onload = function() {
  window.location.href = $('.banan').attr('href');
});
}
</script>

This works for me!

voucher_wolves
  • 565
  • 10
  • 20