javaScript重定向页面

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huiweizuotiandeni/article/details/70121628

Js文件:

window.onload = initAll;
function initAll() {
document.getElementById("redirect").onclick =initRedirect;
}
function initRedirect() {
window.location = "jswelcome.html";
return false;
}

网页文件:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to our site</title>
<script src="script07.js"></script>
</head>
<body>
<h2 class="centered">
<a href="script04.html"id="redirect"> Welcome to our site... c'mon in!</a>
</h2>
</body>
</html>

如果你以前见过函数,那么可能会认为js文件 中的initAll引用应该是initAll()。

这种想法是不对的,因为这是两种不同的东西:带圆括号的函数名意味着正在调用这个函数;

如果没有圆括号(就像这里的情况),就是将它赋值给事件处理程序,以便在此事件发生时运行它。


window.location= "jswelcome.html";

window.location(即浏览器中显示的页面)设置为一个新页面

可以监听<a>标签的href属性,当用户点击时更改显示的网页。return

false 表示停止对用户单击的处理,这样就不会加载href 指向的页面。


猜你喜欢

转载自blog.csdn.net/huiweizuotiandeni/article/details/70121628
今日推荐