executeJavaScript & mailto

Dear people of Vaadin,

i got some issues last days:

method exectuteJavaScript works just fine mit e.g. “alert(hello);” for testing but when it comes to open a new window respectively executing a html-mailto-call like this…

MobileSomewhatApplication.getApp().getMainWindow().executeJavaScript(“location.href = "mailto: test@test.de "; location.href.click()”);

…the method works but the main application says it´s loosing connection. :-/

You guys ideas how to solve this?

It would be awesome to get a quick fix, cause i got a presentation for that on Wednesday. :open_mouth:

Best regards

Christoph

Hi Christoph,

The problem with your call to executeJavaScript is that you replace the location of the main window, which will cause all kinds of problems as the user effectively is navigated away from your application.

Would using the Link component be enough? See
Sampler
for an example of how to open an external link, which may be of any protocol, e.g. mailto: should work just fine. The issue with Link is that you cannot have it happen automatically as the result of some other interaction and it will always look like a normal link unless you style it.

Another possible solution is to open a popup window with your mailto link, see
Chapter 12.7 Printing
in Book of Vaadin for an example that you can modify for your use case.

HTH,
/Jonatan

Hey Jonatan,

thanks a lot for your fast reply. Appreciate that.

The Link alternative isn´t adaptable for my custom Component. There´s a LayoutListener which fires with that certain href-Code.
If can´t use just a Link-Component on the one hand, on the other i can´t work around that by just creating one and firing it with a non-given fire()-Method.
The custom Component is out of a Layout with its Listener and a TextField to have the possiblity to switch between Click- and EditMode including the use of setPropertyDataSource()-Method.

So i tried the other alternative you mentioned. This seemed to work but if i call “self.close();” right after my Code it seems as if the Browser hadn´t enough time ?! I dunno. If i delete the self.close()-Operation it works fine.

I tried to work-around that with a javaScript written sleep method but this doesnt seem to be the problem. any further ideas?

Oh - btw - kind of good to know. i use the addon touchkit and so the TouchKitWindow but this works as well.

that´s how my listener looks like:


new LayoutClickListener() {

				private static final long serialVersionUID = 5065719585277426034L;

				@Override
				public void layoutClick(LayoutClickEvent event) {

					// Create a window that contains what you want to print
					TouchKitWindow window = new TouchKitWindow();

					getApplication().addWindow(window);

					// Open it as a popup window with no decorations
					getWindow().open(new ExternalResource(window.getURL()), "_blank", 1, 1, // Width and height
							Window.BORDER_NONE); // No decorations

					String script = "location.href = mailto: test@test.de; location.href.click()";

					window.executeJavaScript(script);

					String sleepFunction = "function sleep(milliseconds) {" 
                                                        + "var start = new Date().getTime();"
							+ "for (var i = 0; i < 1e7; i++) {" + "if ((new Date().getTime() - start) > milliseconds){"
							+ "break;}}}";

					window.executeJavaScript(sleepFunction + "sleep(2000);self.close();");
				}
			};

Best regards,

Christoph

Hi again,

Sleeping like that in javascript will actually not release control to other parts of javascript running in the browser (everything runs in one thread). This means that your sleep function will keep the JS interpreter busy and it’ll get to run other code only once the function has returned and self.close() has been called, which is exactly the same as not running the sleep function at all. Try using the builtin setTimeout instead, e.g.

setTimeout(self.close(), 2000);

HTH,
/Jonatan

thanks so far.

i did position that issues to the background.

your proposal didnt work btw. funnywise the initial javascript code works on android with dolphin browser. perhaps does android manage that thread issue in another kind a way. iPhone probs arent solved so far.