[英]auto click a link when page loads doesn't work
I have a page for redirect purpose, I want to redirect user to another page once it hits this page: 我有一个用于重定向的页面,当用户点击此页面时,我想将用户重定向到另一个页面:
<!DOCTYPE html>
<html>
<head>
<title>Open App</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
alert("test"); //this shows up.
$('#openApp').click(); //this button is not clicked.
});
</script>
</head>
<body>
<a href="http://www.google.com" id="openApp">redirect</a>
</body>
</html>
I want the redirect happen once this page is load. 我希望重定向在此页面加载后发生。 But the click function is not fired.
但是不会触发单击功能。 I have to click the link by myself to redirect to google.com.
我必须自己单击链接才能重定向到google.com。
It's not possible to trigger a click
event without any user interaction. 没有任何用户交互,就不可能触发
click
事件。 In other words, if the function being called wasn't called from a user interaction, the click event will not get triggered. 换句话说,如果未从用户交互中调用被调用的函数,则不会触发click事件。
In your case, simply update the window.location
attribute: 就您而言,只需更新
window.location
属性:
window.location = 'http://www.google.com';
I normally wouldn't give you an alternative as an answer, but looking at your comments, it looks like you're just trying to redirect. 我通常不会给您替代方法作为答案,但是看着您的评论,您似乎只是在尝试重定向。
You can use window.location.replace('http://www.google.com');
您可以使用
window.location.replace('http://www.google.com');
for that. 为了那个原因。
$(document).ready(function () {
window.location.replace('http://www.google.com');
});
See also, this answer . 另请参阅此答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.