Home  >  Article  >  Web Front-end  >  How to implement webpage jump in javascript (code example)

How to implement webpage jump in javascript (code example)

PHPz
PHPzOriginal
2023-04-25 09:13:212180browse

In JavaScript, there are many ways to jump to web pages. The following are common methods:

  1. Use window.location.href

window.location. href can directly jump the current page to the specified URL. The code is as follows:

window.location.href = "http://www.example.com";
  1. Use window.open

window.open to open a new window or tab. , and jump to the specified URL, the code is as follows:

window.open("http://www.example.com", "_blank");

Among them, "_blank" means opening the link in a new window or tab.

  1. Use location.replace

location.replace can jump to the specified URL, and can also replace the current page so that it no longer appears in the browser's history. The original page, the code is as follows:

location.replace("http://www.example.com");

Since the current page is replaced, it is recommended to confirm the user operation before performing the jump operation.

  1. Use form form submission

In HTML, you can use form submission to jump to the specified URL. The code is as follows:

<form action="http://www.example.com" method="get">
  <input type="submit" value="跳转">
</form>

Through the form The action attribute in specifies the jump URL, which automatically jumps when the form is submitted.

Summary

The above are several ways to jump to web pages in JavaScript. Which method to use needs to be chosen according to specific needs. Among them, window.location.href, window.open, and location.replace can be called directly in JavaScript, while the form needs to be used in HTML.

The above is the detailed content of How to implement webpage jump in javascript (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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