Search code examples
javascripthtmlwindow.open

How to open a window with a custom link?


I have made an html website on my local storage. I also made a button, then a href attribute since I want to go to another link. What happens is I provide a link, then it just goes to an odd path and doesn't work. Check what I mean below:

<a href="#" target="_blank" onclick="window.open('https://www.example.com', '_blank', 'width=200, height=100, menubar=0,titlebar=0,status=0')" target="_blank">Click Me!</a>

It simply just opens another window and contains an odd path through my files. I have tried searching up how to fix this. With my wordings I couldn't find an answer. I even tried looking for questions similar to this, but no luck.


Solution

  • Using windows.open you don't have to use a tag and, it will automatically open the link in a new tab so you don't need to use target="_blank".

    Just do as following:

    <a onclick="window.open('https://www.example.com')" >Click Me!</a>
    

    or this without onclick:

    <a href="https://www.example.com" target="_blank">Click Me!</a>