6
$\begingroup$

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.

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

2 Answers 2

7
+50
$\begingroup$

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 = java`awt`Desktop`getDesktop[]; 
   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.

$\endgroup$
9
  • $\begingroup$ 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? $\endgroup$
    – Kuba
    May 27, 2013 at 6:15
  • $\begingroup$ @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... $\endgroup$ May 27, 2013 at 9:08
  • $\begingroup$ @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. $\endgroup$ May 27, 2013 at 9:26
  • $\begingroup$ @halirutan +1 Yet another example that this list is not quite correct. JLink does work in FreeCDF. $\endgroup$ May 27, 2013 at 9:32
  • $\begingroup$ @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... $\endgroup$ May 27, 2013 at 11:57
1
$\begingroup$

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`"];
  JLink`InstallJava[];
  JLink`LoadJavaClass["java.awt.Desktop"];
  desktop = java`awt`Desktop`getDesktop[];
  uri = JLink`JavaNew["java.net.URI","http://mathematica.stackexchange.com"]
)]

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.