简体   繁体   中英

link running a javascript that open another page. how to make it do it in new tab if user uses middle click or right click menu

I have this link in my left navigation: dashboard

That javascript opens a link based on the passed parameters.

All works fine, but I would like to be able to use the browser capabilities of opening the links in a tab (when user is using middle click or selects 'Open link in new tag' from right click menu). Though, this is not working for links handled with javascript code.

There are many reasons why this is not the default behaviour of the browser (eg javascript function might only do some validation and stay in the page... browser can't know what the js might do or if a new window/dialog will result from that action so would make no sense to open new tag as a result of a middle click...). But hopefully there is a workaround for the default behaviour.

Any idea how this could be done?

Cheers, Stef.

Javascript links execute in context of the page where they are called. If you "open" the link in a new tab/window, the javascript code will be executed in the new window, ie, empty, and will most probably fail.

A browser could try to add the feature you are asking for by cloning the page which contains the link, and executing the javascript code in the context of the cloned page. But this would most likely break some critical sites (imagine for example that your online banking site works with javascript, so when you open a link in a new tab/window, cloning the original window might lead to a duplicate transaction).

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