11

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.

fergaral
  • 2,077
  • 6
  • 17
  • 34
  • Try a different JavaScript function to see if the page still closes: `javascript:document.body.contentEditable%20=%20'true';%20document.designMode='on';%20void%200` – Stu1986C May 06 '15 at 13:10
  • 1
    For debugging and executing script on your page, familiarise yourself with the developer tools of your chosen browser, it will make your life much easier :) – Mathew Thompson May 06 '15 at 13:11
  • 1
    Almost every browser (I suspect IE does too) have some built-in developers tools. Either firebug in firefox or chrome developers tools or whatever are extremely comfortable, there really is no reason anymore to execute some javascript through the address bar ;). – briosheje May 06 '15 at 13:14

4 Answers4

11

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

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
9

One more solution:

javascript: (function(){ /* Your Javascript code goes here */ })();
Venkatesh Achanta
  • 624
  • 1
  • 14
  • 31
3

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?

Community
  • 1
  • 1
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

Alternative solution: If you don't particularly need it to be the JavaScript that changes the content, if you just need the content of the div changed, then you can use the developer tools in your browser to do that.

In Firefox: Developer Tools > Inspector > right click the HTML and choose "Edit as HTML".

In Chrome: Tools > More Tools > Developer Tools > Elements > right click the HTML and choose "Edit as HTML".

In IE: Developer Tools > HTML > Edit Icon (looks like a piece of paper with a pencil)

BSMP
  • 4,596
  • 8
  • 33
  • 44