javascript怎么实现页面跳转

青灯夜游
青灯夜游原创
2023-01-04 09:34:4850287浏览

js页面跳转方法:1、使用“location.href="URL"”;2、使用“location.replace("URL")”;3、使用“location.assign("URL")”;4、使用“window.open("URL")”。

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript实现页面跳转的方法:

1、使用location.href属性跳转到其他网页

语法:

location.href="URL";

示例:

<!DOCTYPE html>
<html>
    <head>
		<meta charset="UTF-8">
    </head> 
    <body> 
      <p>这是<i>location.href</i>方式的示例</p> 
      <button onclick="myFunc()">点击这里</button> 
        
      <!--重定向到其他网页的脚本-->   
      <script> 
         function myFunc() { 
           window.location.href="//m.sbmmt.com"; 
         } 
      </script> 
   </body> 
</html>

2、使用location.replace()方法跳转到其他网页

语法:

location.replace("URL");

示例:

<!DOCTYPE html>
<html>
    <head>
		<meta charset="UTF-8">
    </head> 
    <body> 
      <p>这是<i>location.replace()</i>方式的示例</p> 
      <button onclick="myFunc()">点击这里</button> 
        
      <!--重定向到其他网页的脚本-->   
      <script> 
         function myFunc() { 
          location.replace("//m.sbmmt.com"); 
         } 
      </script> 
   </body> 
</html>

【推荐学习:javascript高级教程

3、使用location.assign()方法跳转到其他网页

语法:

location.assign("URL")

示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    </head> 
    <body> 
      <p>这是<i>location.assign()</i>方式的示例</p> 
      <button onclick="myFunc()">点击这里</button> 
        
      <!--重定向到其他网页的脚本-->   
      <script> 
         function myFunc() { 
          location.assign("//m.sbmmt.com"); 
         } 
      </script> 
   </body> 
</html>

4、使用window.open()方法跳转到其他网页

语法:

window.open("URL");

示例:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    </head> 
    <body> 
      <p>这是<i>window.open()</i>方式的示例</p> 
      <button onclick="myFunc()">点击这里</button> 
        
      <!--重定向到其他网页的脚本-->   
      <script> 
         function myFunc() { 
          window.open("//m.sbmmt.com"); 
         } 
      </script> 
   </body> 
</html>

更多编程相关知识,请访问:编程视频!!

以上就是javascript怎么实现页面跳转的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。