Home  >  Article  >  Web Front-end  >  How Can I Open URLs in New Windows with JavaScript?

How Can I Open URLs in New Windows with JavaScript?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-05 07:18:02639browse

How Can I Open URLs in New Windows with JavaScript?

Opening URLs in New Windows with JavaScript

When creating share buttons, it's often desirable to open shared links in new windows. JavaScript provides utilities for achieving this, but getting the syntax right can be challenging.

Solution Using window.open()

To create a link that opens the current page URL in a new window with specific dimensions, utilize the window.open() method:

<a onclick="window.open(document.URL, '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');">
  Share Page
</a>

Explanation

  • document.URL retrieves the current page URL.
  • '_blank' specifies that the link should open in a new window or tab.
  • The optional parameters define window attributes:

    • location=yes displays the address bar in the new window.
    • height=570 sets the window height to 570 pixels.
    • width=520 sets the window width to 520 pixels.
    • scrollbars=yes enables scrollbars in the new window.
    • status=yes displays the status bar in the new window.

The above is the detailed content of How Can I Open URLs in New Windows with JavaScript?. 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