9

If I run the following line in Firebug on any page:

document.documentElement.innerHTML="<script>alert(1)</script>";

why isn't the alert command executed?

6
  • @11684: That's not needed in HTML5. Commented Jul 27, 2012 at 20:11
  • @Rocket You don't know which browser he is using! Commented Jul 27, 2012 at 20:12
  • 3
    Oh, wait. Firebug... That means Mozilla... Commented Jul 27, 2012 at 20:12
  • 1
    Does this link help? stackoverflow.com/questions/1197575/… Commented Jul 27, 2012 at 20:13
  • 2
    I reckon this is trying to win the "how many questionable javascript practices can you fit into a single line of code" competition. Commented Jul 27, 2012 at 20:18

4 Answers 4

5

It looks like that your <script> tag is being added as you expect, but the code within it is not being executed. The same failure happens if you try using document.head (or any other DOM element, it seems). For whatever reason (possibly standards compliance, possible security), inline code inside of <script> blocks that are added via .innerHTML simply doesn't run.

However, I do have working code that produces similar functionality:

var script = document.createElement('script');
script[(script.innerText===undefined?"textContent":"innerText")] = 'alert(1);';
document.documentElement.appendChild(script);

Here, you add the <script> block with documentElement.appendChild and use textContent or innerText to set the content of the <script>.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice alternative to eval().
1

It is always best to create the elements and append them, rather than straight inserting any html using innerhtml.

You can use read more about it here: https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet

This fragment works:

var newScript = document.createElement( "script" );
newScript.type = 'text/javascript';
var scriptContent = document.createTextNode( "googletag.cmd.push( function() { googletag.display( '" + encodeURIComponent( divID ) + "' ); } );" ); 
newScript.appendChild( scriptContent ); 

Here is the example in action: https://jsfiddle.net/BrianLayman/4nu667c9/

Comments

0

Actually you can use eval but that's not a good practice for security issues. You can do something like this:

var scr = document.createElement('script');
scr.src = 'yourscriptsource';
document.body.appendChild(scr);

Hope it helps!

Comments

-5

You don't to do that. In Firebug go to the "Console" tab. You can enter code directly there. Next to the three blue angle brackets at the bottom of the console type this and then hit the enter key: alert("asdf");

1 Comment

The OP is trying to diagnose why this line of code does not work as expected. The OP almost certainly is already using the Firebug console (where else could (s)he run the following line in Firebug other than the console?).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.