# How to open hyperlinks (from web-embedded CDF) in a new tab

I have a problem with Hyperlink (in a web-embedded CDF) opening links in the same browser tab and I couldn't find an option to avoid that.

I have the following constraints:

1. CDF is web-embedded (created by me).
2. The hyperlink should open only after mouse clicking, no combinations with keyboard keys are allowed.

Failed attempts:

• I've tried to click it with Shift like it is written in Help but it does not work. It doesn't matter because of constraint #2 above. However if it is possible to mimic the Shift key being pressed (in some other way than pressing the key) maybe that will help?

• I've tried to replace Hyperlink by Button[ "link", SystemOpen["link"]] but there is no reaction.

I don't think this is important, but this is how I'm testing it:

• I create a CDF file using Mathematica
• Export as "web embeddable..."
• I paste script received in the end to a .txt file -> save -> switch to .html

It is quite important for me to find a solution to this, because the CDF resets every time the link opens in the same tab.

-
Can't you allow only the Shift key to be pressed and ignore key presses from all other keys? –  rm -rf May 26 '13 at 16:45
@rm -rf It is not the case. I do not want any keys to participate :) –  Kuba May 26 '13 at 16:56
Moreover, Shift does not work in web-embedded CDF! Only CRTL, but it is related to browser not due to CDF. –  Kuba May 26 '13 at 17:00

## 2 Answers

I probably have an idea how to turn a Button into want you require with the help of JLink. The trick includes to use the java methods to open a link in the system browser. The following is a small working example without catching any exceptions and testing whether opening an URL is even supported.

If I export this Manipulate as an embedded CDF, open it and click the button, on OSX a new tab is opened every time.

Manipulate[
Button["Browse Me", desktop@browse[uri]],
Initialization :> (Needs["JLink"]; InstallJava[];
LoadJavaClass["java.awt.Desktop"];
desktop = javaawtDesktopgetDesktop[];
uri = JavaNew["java.net.URI",
"http://mathematica.stackexchange.com"]),
SaveDefinitions -> True
]


## Comment to comments

That definitely works, thank You. I'm not very familiar with Java so let me ask You some questions. I've checked those procedures and it seems there is no direct question to Java about new tab. Is it then 'nature' of JavaNew[]?, or lucky coincidence?

The Mathematica function JavaNew has nothing to do with opening something in a new tab. It is just java code transformed into a Mathematica call. The uri = JavaNew[...] line is equivalent to the following java syntax

java.net.URI uri = new java.net.URI("http://mathematica.stackexchange.com");


which creates a new URI object which is then opened in the browser. Basically, I only transformed the following java function into Mathematica call with the help of JLink:

public void openUrl(String url) throws IOException, URISyntaxException {
if(java.awt.Desktop.isDesktopSupported() ) {
java.awt.Desktop desktop = java.awt.Desktop.getDesktop();

if(desktop.isSupported(java.awt.Desktop.Action.BROWSE) ) {
java.net.URI uri = new java.net.URI(url);
desktop.browse(uri);
}
}
}


To answer your question: desktop.browser(url) seems to open everything in a new tab and yes, this is a lucky coincidence to make this solution so short. But you should not completely ignore Alberts concerns; while this seems to work on many machines, there is the probability that on some machines it won't.

-
That definitely works, thank You. I'm not very familiar with Java so let me ask You some questions. I've checked those procedures and it seems there is no direct question to Java about new tab. Is it then 'nature' of JavaNew[]?, or lucky coincidence? –  Kuba May 27 '13 at 6:15
@Kuba: what this does it to make use of OS functionality accessed via Java (see e.g. awt dektop doc). It uses the default browser to open an URI. When the CDF runs in the default browser and the configuration allows it that might work as desired (open in a new tab). It didn't work for my setup (firefox on Windows) out of the box and whether it works will depend on many details of OS, browser and configuration. With all the security measures in todays browsers I think you can't expect this to work in general... –  Albert Retey May 27 '13 at 9:08
@AlbertRetey It works nicely for me on Windows even when the CDF is not run in the default browser, though it always opens a new tab in the default browser, but that should be acceptable. –  Rolf Mertig May 27 '13 at 9:26
@halirutan +1 Yet another example that this list is not quite correct. JLink does work in FreeCDF. –  Rolf Mertig May 27 '13 at 9:32
@RolfMertig: What I tried to make clear is that while it might work in many cases, you can't guarantee it will. An extreme case is users who might have uninstalled or deleted the executable that their OS still is configured to use as default browser. But I could well imagine that also the fact that browsers and plugins run in sandboxes these days might well result in this not working in some cases. As I said it didn't work for me but I'm sure I could get it working if I wanted by tweaking my setup... –  Albert Retey May 27 '13 at 11:57

halirutan have given me good answer. It works :)

My cdf is not based on Manipulate but DynamicModule so I had to add small thing to make it work :) That was fast thanks to Istvan Zachar work.

Following code is usefull since simple switching Manipulate to DynamicModule is not enough. The only differece is with using explicit names:

DynamicModule[{},
Button["Browse Me", desktop@browse[uri]],
Initialization :> (
Needs["JLink"];
JLinkInstallJava[];
JLinkLoadJavaClass["java.awt.Desktop"];
desktop = javaawtDesktopgetDesktop[];
uri = JLinkJavaNew["java.net.URI","http://mathematica.stackexchange.com"]
)]
`

From what I've checked this and halirutan versions works on WinXP and Win7 with Chrome.

-