user2641103
user2641103

Reputation: 702

Opening a new window with javascript doesn't work

I want to open a new window or tab pointing to a different url taken from a link by using javascript. This is the code:

<script>
        window.open($('#my_a_link_id').get(0).href);
</script>

However, nothing happens. What I am missing here? I have checked already that the $('#my_a_link_id').get(0).href expression has the correct value.

Upvotes: 0

Views: 1391

Answers (2)

XavierBrt
XavierBrt

Reputation: 1249

To open a link in a new tab, you have to use the keyword target on a a tag :

<a href="http://www.mywebsite.com/" target="_blank">My Website</a>

You can do it dynamically with js.

It will open a new tab (default) or a new window depending how the user configured his browser.

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

The browser won't let you open a window on your own, it's a feature in browsers since the early 2000s called "popup blockers".

If you must open a window, you have to do it as part of user interaction (user clicked the mouse or navigated to a link with the keyboard and interacted with it in that way, etc).

It's worth noting that opening new windows is considered bad UI and is generally not recommended. There's almost always a better solution.

Upvotes: 2

Related Questions