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.

8
  • usually innerHtml works but sometimes it does this behaivour. If you can use jquery then use $.load(). Much easier and stable. Commented Jun 19, 2015 at 9: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. Commented Jun 19, 2015 at 9: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(){}}); Commented Jun 19, 2015 at 9: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). Commented Jun 19, 2015 at 9:45
  • Have you tried document.write()? You can then call document.close(). Commented Jun 19, 2015 at 9:53

1 Answer 1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.