Home > Web Front-end > JS Tutorial > How to jump to a new page in js?

How to jump to a new page in js?

青灯夜游
Release: 2020-07-18 15:04:52
Original
17412 people have browsed it

JS method to jump to a new page: 1. Use location.href="new page URL" to jump; 2. Use location.replace("new page URL") to jump; 3. Use location.assign("new page URL") to jump.

How to jump to a new page in js?

Method 1: Using location.href

Syntax:

location.href="URL"
Copy after login

Example

<!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>
Copy after login

How to jump to a new page in js?

Method 2: Use location.replace()

Syntax:

location.replace("URL")
Copy after login

Example : Use location.replace() method to jump to other web pages

<!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>
Copy after login

How to jump to a new page in js?

Method 3: Use location.assign()

Syntax:

location.assign("URL")
Copy after login

Example: Use the location.assign() method to jump to other web pages

<!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>
Copy after login

How to jump to a new page in js?

Recommended tutorial: "JavaScript Video Tutorial

The above is the detailed content of How to jump to a new page in js?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template