What's the method of javascript replacing URL?

09-06-2023

Most people don't quite understand the knowledge points of this article about how to replace the URL with javascript, so Xiaobian summed up the following contents, with detailed contents and clear steps, which has certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article about how to replace the URL with javascript.

Replacing a URL with JavaScript refers to replacing a part of a web page with another URL, which can be realized through the location object in JavaScript. The location object can access the URL information of the current document, and can extract various parts of the URL to set a new URL. Let's take a look at a code example of a basic JavaScript replacement URL:

function redirect() {   location.replace("http://www.baidu.com"); //Replace the URL } redirect(); //Call the function

In the above code, the location.replace () method is used to replace the current URL, and it points to "http://www.baidu.com". Finally, the redirect () function is used to call execution.

In addition to using the location.replace () method, you can also use the location.href and location.assign () methods to replace the URL. These two methods have the same effect, pointing the current URL to a new URL.

function redirect() {   location.href = "http://www.baidu.com"; //Replace the URL } redirect(); //Call the function function redirect() {   location.assign("http://www.baidu.com"); //Replace the URL } redirect(); //Call the function

In the above code, the location.href and location.assign () methods can also point the current URL to a new URL.

In practical application, we often need to make dynamic URL replacement according to different situations. For example, get the content input by the user through the input box, and then pass the input content as a parameter to the URL to realize dynamic replacement.

function redirect() {   var keyword = document.getElementById("searchInput").value; //Get the contents of the input box   var newUrl = "https://www.baidu.com/s? wd=" + keyword;   location.href = newUrl; //Replace the new URL }

In the above code, we first obtained the contents of the input box with the id "searchInput", then spliced it into a new URL with the plus sign, and finally replaced it with the current URL.

JavaScript alternative URLs can also be used to achieve page jumping and redirection. The following is an example code for realizing page jumping and redirection through JavaScript:

//Page jump function jumpToPage() {   var pageNum = document.getElementById("pageNumInput").value;   var newUrl = "http://www.example.com/page_" + pageNum + ".html";   location.href = newUrl; } //redirect function redirect() {   location.replace("http://www.example.com"); //Redirect to a new website } redirect(); //Call the function

In the above example code, we get the content input by the user and then splice it into the new URL as a parameter to realize the page jump and redirection.

The above is the content of this article about how to replace the URL with javascript. I believe everyone has a certain understanding. I hope that the content shared by Xiaobian will be helpful to everyone. If you want to know more about the relevant knowledge, please pay attention to the Yisu Cloud Industry Information Channel.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us