Home >Web Front-end >Front-end Q&A >How to pop up a new page in javascript
In Web development, jumping to a page is a frequently used operation, and JavaScript can easily implement this function. Next we will introduce the operation of popping up a new page and realizing a jump through JavaScript.
1. Pop up a new page
We can use two methods to pop up a new page:
1. Through the window.open() method
window The .open() method will open a new window or tab in the browser. Its syntax is as follows:
window.open(url, name, specs, replace);
where
For example, we can use the following code to open a new window and let it jump to Baidu's homepage:
window.open("https://www.baidu.com", "baidu");
2. Through the window.location.href attribute
The window.location.href property can get or set the URL of the current page. When we set this attribute to a new URL, the browser will immediately jump to the URL, thus achieving the effect of popping up a new page.
For example, we can use the following code to let the browser jump to Baidu's homepage:
window.location.href = "https://www.baidu.com";
2. Implement page jump
After knowing how to pop up After the new page, we also need to know how to implement the jump.
1. Through the window.location.href attribute
We can achieve page jump by setting the window.location.href attribute. For example, the following code can jump the current page to Baidu's news page:
window.location.href = "https://news.baidu.com/";
2. Through the window.open() method
We can also use the window.open() method To achieve page jump. For example, the following code can open Baidu's news page in a new window or tab:
window.open("https://news.baidu.com", "baidu");
It should be noted that if we want to jump to a new page in the current page, we should use window. location.href property. If you want to open a new page in a new window or tab, you can use the window.open() method.
3. Summary
Through the above introduction, we have learned how to use JavaScript to pop up a new page and realize the jump operation. Whether you use the window.location.href attribute or the window.open() method, it is a very convenient way to jump to a page. The specific use can be selected according to specific business needs.
The above is the detailed content of How to pop up a new page in javascript. For more information, please follow other related articles on the PHP Chinese website!