4

I have the following code which works properly in chome

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <script>
    //<![CDATA[
        !function (){
            window.stop();
            var html = '<!DOCTYPE html>\n<html>\n<head>\n    <meta charset="utf-8">\n</head>\n<body>\n  \<script>console.log("loaded");<\/script>\ntext\n</body>\n</html>';
            document.documentElement.innerHTML = html;
        }();
    //]]>
    </script>
</body>
</html>

It prints "loaded" in the console. The same code does not work by firefox, it does not run the script, just prints the text.

(If you are curious why I need this, you can find it here: https://stackoverflow.com/a/30933972/607033 )

I tried possible solutions like this: https://stackoverflow.com/a/20584396/607033 but they did not work. Any idea how to work this around?

Note: there are many scripts in the HTML, e.g. bootstrap, jquery, facebook, google, etc..., not just a single inline script.

Community
  • 1
  • 1
inf3rno
  • 24,976
  • 11
  • 115
  • 197
  • usually innerHtml works but sometimes it does this behaivour. If you can use jquery then use $.load(). Much easier and stable. – Harsh Jun 19 '15 at 09:09
  • @AngularHarsh I am not sure I can use jquery. I don't want to load the original webpage, and the script runs as an addon SDK attachment, so I am not sure whether it can access jquery or not. I'll give it a try, maybe. – inf3rno Jun 19 '15 at 09:10
  • Anyways even $.load uses innerHtml in backend. Then you can use $.getScript for scripts as a success callback for $.load. Just a thought. $( "#myId" ).load( "anything.html", function( ) {$.getScript(){}}); – Harsh Jun 19 '15 at 09:14
  • @AngularHarsh The attachment script cannot see jquery, and the `$("html").load(htmlSourceCode)` did not work by normal HTML, since it uses files from the server (I guess with ajax). – inf3rno Jun 19 '15 at 09:45
  • Have you tried `document.write()`? You can then call `document.close()`. – Thomas Roch Jun 19 '15 at 09:53
  • @ThomasRoch Did not work either. But I found a solution. – inf3rno Jun 19 '15 at 09:55
  • @inf3rno what was your solution? I am running into a similar issue – kayq Feb 20 '21 at 00:13
  • @kayq As far as I remember I gave up on Firefox addons. – inf3rno Feb 20 '21 at 05:02

1 Answers1

0

I think there is no way in firefox to replace the complete HTML document with javascript without leaving the actual page. A workaround to reuse the original document and replace only the head and body tags:

$('html').html(html);

does this automatically: it strips out the HTML tags, injects the head and the body and loads the scripts.

ref: https://stackoverflow.com/a/1236372/607033

Community
  • 1
  • 1
inf3rno
  • 24,976
  • 11
  • 115
  • 197