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.

2

4 Answers 4

3

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');
Sign up to request clarification or add additional context in comments.

4 Comments

ok that is at least 1 step. thank you, but it still does not autoredirect. is there a way to test if the function actually does anything?
Yes, you can debug it. Put a break point on that line and see if the browser run it.
it does not for me it just sits there with the link in the screen. does it need some kind of jfidle extention or something like that??
it was the link i forgot. there was no : <script src="ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> in my code.
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.

Comments

1

Try this:-

 document.getElementById('yourLinkID').click();

Comments

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!

7 Comments

thank you im a total idiot on this one. i forgot the script src link......
Yeah, happens all the time. ;)
@B.Dionys Since this answer does not work, but Mosh Feu's does, and explains it well, I suggest you accept that one instead. Forgetting the actual link to jQuery is more a typo in this case, than an answer.
@LGSon, sorry it was my typo problem also, I copied his code and tried it on local. But forgot to copy my code here!
@B. Dionys- Yes go ahead, his answer was acceptable more than mine.
|

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.