I'm trying to redirect a开发者_StackOverflowfter 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?
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");
$('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")
精彩评论