setting innerHTML with a script inside [duplicate]
Asked Answered
H

4

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?

Halliehallman answered 27/7, 2012 at 20:7 Comment(6)
@11684: That's not needed in HTML5.Bargain
@Rocket You don't know which browser he is using!Hett
Oh, wait. Firebug... That means Mozilla...Hett
Does this link help? #1198075Fluorinate
I reckon this is trying to win the "how many questionable javascript practices can you fit into a single line of code" competition.Philippines
Thank you Larry, it helps. Yes Spudley, I upvoted your comment because this is a questionable practice but I need to understand if such a questionable practice would potentially corrupt my web app.Halliehallman
R
6

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>.

Redding answered 27/7, 2012 at 20:26 Comment(1)
Nice alternative to eval().Nicias
T
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/

Tubercle answered 16/6, 2016 at 16:27 Comment(0)
P
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!

Pavkovic answered 27/7, 2012 at 20:17 Comment(0)
O
-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");

Olva answered 27/7, 2012 at 20:23 Comment(1)
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?).Redding

© 2022 - 2024 — McMap. All rights reserved.