简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM