1

I'm trying to redirect after page + images load:

function redirect_on_load(){
   //called from script in body
   //wait until page loads and click first link
   $(window).load(
       function() {
        $('a').click();         // desired action. ineffective
        $('a')[0].click();      // kills script
        $('a').get(0).click();   // kills script
        $('a').hide();          // works
       }
   );
}

There's only one link on the page.

Why doesn't the click method work?

1
  • 3
    did you try $.trigger("click")? Commented Jul 29, 2011 at 17:26

2 Answers 2

7

click will fire any event handlers attached to the element on the click event. It will not simulate an actual user click and fire the action that goes along with it. This means that it will not take the user to the URL defined in href. You will instead need to use window.location = URL

Something like:

window.location = $("a").attr("href");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It looks like I've duplicated: stackoverflow.com/questions/2053598/…
2
  • $('a').click(); will attempt to click all the anchor elements
  • $('a')[0].click(); See Rocket's comment
  • $('a').get(0).click(); See Rocket's comment
  • $('a').hide(); simply hides all the anchors

I would suggest placing an id or class of some kind on your desired link or just redirecting with locaton.href = $('a:first').attr("href")

1 Comment

$('a')[0].click(), and $('a').get(0).click(); don't work because $('a')[0] and $('a').get(0) are DOM elements, not jQuery objects, so they don't have a click function.

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.