Using the window.open() Function
window.open(url, windowName, [windowFeatures]);
The function takes three parameters:
- url: The URL of the page to open in the new window.
- windowName: A name for the new window. This can be used as a reference to manipulate the window later.
- windowFeatures (optional): A string that specifies additional features for the new window (e.g., width, height, and scrollbars).
To open a new tab, we just need to call the window.open() function with the desired URL and an empty string as the windowName parameter. For example:
Opening a New Tab in jQuery
In this example, we use the jQuery $(document).ready() function to ensure that our code is executed once the DOM is fully loaded. We then select the element with the ID open-new-tab and attach a click event handler to it. Inside the event handler, we call the window.open() function as we did earlier.
To see this in action, add the following HTML to your page:<button id="open-new-tab">Open New Tab</button>
Now, when you click the “Open New Tab” button, a new tab will open with the specified URL.
In this blog post, we’ve explored how to open a new tab in jQuery by attaching a click event handler to an element and calling the window.open() function. This technique can be easily integrated into your existing jQuery codebase and provides a simple way to open new tabs as needed.