To open a URL in a new tab without being blocked by the browser, you need to use the window.open() method inside a function that must be triggered by the user.
If you call the open() method after loading the webpage, the browser will block it.
Example : Javascript open link in a new tab. #
<button onclick="myFunc()">Open url</button>
<script>
function myFunc(){
window.open('https://example.com')
}
</script>
Try it Yourself »
window.open() : here window is an object and the open() is a method of the window object. We don't need to specify the window object to call the open() method.
<button onclick="myFunc()">Open url</button>
<script>
function myFunc(){
open('https://example.com')
}
</script>
Try it Yourself »
Comments (0)