Summary of methods for opening pages in js


Preface

This article summarizes the method of opening a new page/window in JS

1.window.open

Open a new browser page or tab and set the parameters of the new page.

window.open(url,name,specs,replace)
  1. Parameter 1: url
    a. Required parameter, address of new page
  2. Parameter 2:name
    a. The name of the new window
  3. Parameter 3:specs
    a. Parameters of the new page, you can set the size, position, and toolbar
  4. Parameter 4:replace:Boolean
    a. true: replace the current page
    b. false: open a new page based on the current page< /span>
  5. Parameter 5: opener
    a. Indicates the window object that calls window.open. This window can reference the window object that calls it through window.opener

2.location.href / window.location.href

Modify the address of the current page to the address of the new page, but cannot set new page parameters.

location.href = url
window.location.href = url
  1. location.href is an attribute obtained from the location object, and its scope is within the current window or tab.
  2. window.location.href is to obtain the attributes of the location object through the window object, and its scope is the entire window
    If location.href is used within the function, it may not affect the global window. ,window.location.href always points to the global window

3.location.replace

The location.replace and location.href methods are similar
When location.replace is used, it will replace the current browser window, and the new page will replace the current page in the history. location without creating a new entry in the history or going back to the previous page

location.replace(url)

4.a label jump

The target attribute of the a tag can set the opening method of the link

<a href="window.baidu.com" target="_blank">百度</a>

a tag attribute

  1. target attribute
    a. target="_blank": open in a new window
    b. target="_parent": open the link in the parent window
    c. target="_self": Default: Jump on the current page
    d. target="_top": Open the link in the current window and replace the current The entire form (frame page)
  2. download attribute
    a. The download attribute is used to specify the download behavior of the link. When the download attribute is present, the specified file will be downloaded when the user clicks the link, rather than being navigated to the link's URL.
    b. The download attribute only applies to files linked to servers with the same source. If linked to resources from other sources, the browser may prevent downloading
<a href="example.text" download>下载</a>

Guess you like

Origin blog.csdn.net/cx18333/article/details/134481720