17

I'm trying to execute Javascript on my web application by executing this on the browser URL/address bar:

javascript:window.alert('test');void(0);

However, nothing happens and the alert box is not showing up? Could the reason be that the app is running in DevMode?

1
  • your question helped me with the void(0); part at the end without which the browser actually navigates away from the current page. Thanks Commented Oct 17, 2015 at 14:22

2 Answers 2

20

You can write your javascript code inside the block like this

javascript:{alert("ok");}
Sign up to request clarification or add additional context in comments.

4 Comments

Note, however, that it does not work on a blank page.
Thanks, wasn't sure why void() couldn't take multiple semicolon-delimited calls. Of course it expects one argument, but your syntax avoids void() and allows for multiple statements.
@Andrew thanks, that was the only thing I was missing
Still works as of Chrome 104.
13

I believe most browsers refuse to execute javascript: URLs from the URL bar as a safety measure (there's been messages on the web –esp. Facebook– telling people to copy-paste a javascript: URL to their URL bar that triggered an XSS). They didn't want to break bookmarklets though, so you can put that code in a bookmark; but it's much easier to just open the browser's JS console and type that command there.

Anyway, it's not due to GWT's DevMode.

5 Comments

Too bad, in old browsers I've used before it works fine, anyway, my goal is to simply invoke GWT methods exposed to javascript by GwtQuery for testing purposes...
Isn't it easier to use the browser's JS console? It gives you introspection of complex objects (much better than a window.alert), autocompletion, command history, etc. Only benefits, without any drawback.
Actually, I use JS console for logging. The reason I will use to execute JS methods is to trigger specific methods of my app
And so? What's preventing you from typing "window.alert('test')" (to reuse your example) in the JS console rather than in the URL bar?
Chrome strips the "javascript" part out from anything you paste into it, but you can still type it manually, and you can still tell people to copy something like this and just delete the period at the beginning: .javascript:while(1); // (fairly harmless)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.