Sure, here’s how we can create the article:
Open URL in new window is a common requirement in web programming especially when we want to handle external links. It can be beneficial for the user experience and for SEO purposes, as it ensures that users can still access our page while browsing the linked content.
The window.open() function
- URL: This is the webpage that you want to open
- Name: This represents the target attribute or the name of the window
- Specs: This sets the window features such as its height, width, and whether the status, tool, menu, or scroll bars are turned on.
var newWindow = window.open('https://www.example.com', '_blank', 'height=600,width=800,status=yes,toolbar=no,menubar=no,location=no');
Step-By-Step Explanation of The Code
The first step is to call window.open() function where ‘window’ is a global object representing the browser’s window where scripts are executed. ‘open()’ is a function that creates a new window.
Secondly, we provide the parameters. The URL of the page we want to open is passed as the first parameter.
The ‘_blank’ target makes the browser open the URL in a new window. If you’d like to open the URL in a new tab instead, you could change ‘_blank’ to ‘_self’.
The third parameter is optional and is used for specifying the features of the new window. In this case, ‘height’, ‘width’ and ‘status’ are set, while ‘toolbar’, ‘menubar’, and ‘location’ are disabled. You can customize these options according to your requirement.
Finally, the reference to the new window is stored in the variable ‘newWindow’. This could be used later, for example to change the URL of the new window.
Additional Libraries or Functions
Remember, while opening URLs in new windows can be beneficial to keep users on your page, too many new windows or tabs can be annoying to some users. Always consider user experience when deciding when and where to use this feature.