0

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 (e.g. 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.

Stef
  • 2,603
  • 2
  • 18
  • 28

1 Answers1

1

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, i.e., 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).

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • Thanks Vlad. I don't totally agree with that example. If duplicating the page would lead to duplicate transaction, this basically means that the same would happen if you would click on a link on the same page (not javascript) ... you would end up using the same session and committing the same data twice, unless there is no check on the server side (e.g. cloning the session and invalidating the one which potentially achieved it's purpose or getting user to place order). – Stef Jun 08 '11 at 06:38
  • @Stef: I meant the following example: imagine that you are on a page where you are going to commit a transaction. Now, the page is duplicated, and you finish the transaction in the new window, and close that window. The original window is open still at the state with uncommitted transaction, so you are perfectly inclined to commit the same transaction once more. There must be better examples, though. – Vlad Jun 08 '11 at 11:19
  • @Vlad: I'm talking about the same scenario. Imagine that you would not click on a link that has a javascript behind, but to a link to the same site (e.g. your current page is bla.com/checkout and you have a link on that page bla.com/books). Thus you now have 2 pages (books and checkout opened under the same http session). From your 'books' page you click on checkout, which takes you to the checkout page and then click on 'finish'. This will close the checkout transaction. But, you still have the other window open on the checkout page. If you go to that and click 'finish', you are in trouble. – Stef Jun 09 '11 at 04:14
  • This can relatively easy (and safe as far as I see it) be done, on the browser side. The browser could easily track if there was any window.open request from the javascript function, and open a new window or a new tab depending on the action that triggered that javascript call and thus the window.open call. This way browser would actually respect the user's choice. By default it doesn't do it. I was curious if there is a way to do such. – Stef Jun 09 '11 at 04:21
  • @Stef: the problem is when the user middle-clicks a javascript link, correct? What would you propose the browser to do? – Vlad Jun 09 '11 at 15:54
  • @Stef: Actually the browser guys are considering implementing your idea, though it seems to be quite complicated w.r.t. different possible actions which can be encoded into a js link. For Firefox, see [here](https://bugzilla.mozilla.org/show_bug.cgi?id=55696) and [here](https://bugzilla.mozilla.org/show_bug.cgi?id=251137). – Vlad Jun 09 '11 at 16:05