Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing JavaScript from the address bar causes the browser to exit the page?

I'm testing a webpage, and I want to execute some JavaScript code from the address bar to change some content (e.g. changing the content of a div) I loaded the webpage and typed in the address bar the following:

javascript:document.getElementById("message").innerHTML="anotherthing"

And, after executing the above code, the browser changes the content of the div, but inmediatly it exits the page.

How can I avoid that behavior?

Thank you.

like image 244
fergaral Avatar asked May 06 '15 13:05

fergaral


Video Answer


3 Answers

Add a void(0); to the end and it will fix it.

like image 128
potatopeelings Avatar answered Oct 19 '22 17:10

potatopeelings


When you copy and paste the javascript: label into some browsers URL bars it automatically removes it. This is a security measure following certain attacks which have occurred in the past whereby people were asked to copy exploitative JavaScript into their URL bars in order for attackers to steal data.

If you want to execute JavaScript like this, you may find that you need to manually type out the javascript: part, otherwise your browser will assume that you're wanting to instead search for document.getElementById(...)....

Example

Copy the following into your address bar:

javascript:alert('hi');

For me, in Chrome on Windows, the javascript: part is removed and instead it assumes I want to search for alert('hi'):

Example

A better solution

Depending on what you're trying to achieve, you'd probably be better off executing JavaScript through your browser's JavaScript console. If you're unsure how to do that, check this out: How to open the JavaScript console in different browsers?

like image 26
James Donnelly Avatar answered Oct 19 '22 18:10

James Donnelly


One more solution:

javascript: (function(){ /* Your Javascript code goes here */ })();
like image 27
Venkatesh Achanta Avatar answered Oct 19 '22 16:10

Venkatesh Achanta