Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 189461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:10:28+00:00 2026-05-11T16:10:28+00:00

If you open a text file (.txt, .js, .css, …) in your browser, it

  • 0

If you open a text file (.txt, .js, .css, …) in your browser, it will get wrapped up in a nice DOM tree.

For example, open this .txt file and enter

javascript:alert(document.documentElement.innerHTML);

into your address bar. Nice… every major browser supports DOM manipulation on this wrapped text files, which is a great thing for writing powerful bookmarklets or user scripts.

However, Firefox fails on assigning any element’s innerHTML. For example,

javascript: document.body.innerHTML = document.body.innerHTML.replace(/(\d+\s+\w+(?=\s+\d+))/g, '<span style="color:red">$1</span>'); void 0;

will work in every browser but Firefox.

Is there a trick to work around this issue?

(No, I don’t want to parse the innerHTML string manually, and no, it doesn’t work with jQuery either.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:10:29+00:00Added an answer on May 11, 2026 at 4:10 pm

    I think I found a working solution. First of all, let me give some more details on the question.

    The problem is: Firefox creates something like

    [some wrapper]
    +---document
        +---<html>[=documentElement]
            +---<body>
                +---<head/>
                +---<pre>
                    +---[actual plain text contents]
    

    but the wrapped document object does not support setting innerHTML properly. So, the basic idea is, create a new document object with full innerHTML support. Here’s how it works:

    var setInnerHTML = function(el, string) {
        if (typeof window.supportsInnerHTML == 'undefined') {
            var testParent = document.createElement('div');
            testParent.innerHTML = '<br/>';
            window.supportsInnerHTML = (testParent.firstChild.nodeType == 1);
        }
        if (window.supportsInnerHTML) {
            el.innerHTML = string;
        } else {
            if (!window.cleanDocumentObject) {
                /* this is where we get a 'clean' document object */
                var f = document.createElement('iframe');
                f.style.setProperty('display', 'none', 'important');
                f.src = 'data:text/html,<!DOCTYPE html><html><title></title></html>';
                document.body.appendChild(f); /* <- this is where FF creates f.contentDocument */
                window.cleanDocumentObject = f.contentDocument;
                document.body.removeChild(f);
            }
    
            /* let browser do the parsing */
            var div = window.cleanDocumentObject.createElement('div');
            div.innerHTML = string; /* this does work */
    
            /* copy childNodes */
            while(el.firstChild) {
                el.removeChild(el.firstChild); /* cleanup */
            }
            for (var i = 0; i < div.childNodes.length; i++) {
                el.appendChild(div.childNodes[i].cloneNode(true));
            }
            delete div;
        }
    }
    

    edit:

    This version is better and faster; using XSLTProcessor instead of iFrame.

    var setInnerHTML = function(el, string) {
        // element.innerHTML does not work on plain text files in FF; this restriction is similar to
        // http://groups.google.com/group/mozilla.dev.extensions/t/55662db3ea44a198
        var self = arguments.callee;
        if (typeof self.supportsInnerHTML == 'undefined') {
            var testParent = document.createElement('div');
            testParent.innerHTML = '<p/>';
            self.supportsInnerHTML = (testParent.firstChild.nodeType == 1);
        }
        if (self.supportsInnerHTML) {
            el.innerHTML = string;
            return el;
        } else if (typeof XSLTProcessor == 'undefined') {
            return undefined;
        } else {
            if (typeof self.cleanDocument == 'undefined')
                self.cleanDocument = createHTMLDocument();
    
            if (el.parentNode) {
                var cleanEl = self.cleanDocument.importNode(el, false);
                cleanEl.innerHTML = string;
                el.parentNode.replaceChild(document.adoptNode(cleanEl), el);
            } else {
                var cleanEl = self.cleanDocument.adoptNode(el);
                cleanEl.innerHTML = string;
                el = document.adoptNode(cleanEl);
            }
    
            return el;
        }
    
        function createHTMLDocument() {
            // Firefox does not support document.implementation.createHTMLDocument()
            // cf. http://www.quirksmode.org/dom/w3c_html.html#t12
            // the following is taken from http://gist.github.com/49453
            var xmlDoc = document.implementation.createDocument('', 'fooblar', null);
            var templ = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">'
                    + '<xsl:output method="html"/><xsl:template match="/">'
                    + '<html><title/><body/></html>'
                    + '</xsl:template></xsl:stylesheet>';
            var proc = new XSLTProcessor();
            proc.importStylesheet(new DOMParser().parseFromString(templ,'text/xml'));
            return proc.transformToDocument(xmlDoc);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 92k
  • Answers 92k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In both cases, you're not handling any disposal issues of… May 11, 2026 at 6:28 pm
  • Editorial Team
    Editorial Team added an answer A redirect, as others suggest, does have some advantage, but… May 11, 2026 at 6:28 pm
  • Editorial Team
    Editorial Team added an answer It's bad idea to dump indexer properties. It's similar to… May 11, 2026 at 6:28 pm

Related Questions

I'm developing an application targeting .NET Framework 2.0 using C# for which I need
My current knowledge: If you are trying to write text files in vbscript /
In our application, we receive text files ( .txt , .csv , etc.) from
C#: How do you handle/parse messages for your applications like drag and dropping an
I wanna stop the reading of my text input file when the word synonyms

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.