admin管理员组

文章数量:1289529

It appears that JavaScript auto-converts certain special characters into HTML entities when outputting content via the innerHTML() function. This is a problem, since I need to be able to output < and > without converting to gt; and lt;

Can this auto-conversion be prevented, reversed, or escaped? So far, no matter what I do, < and > are always automatically encoded into HTML entities.

Example code:

function DisplayQueries() {
    var IDs = ['AllOpenedINC','AllOpenedCRQ','AllClosedINC','AllClosedCRQ','SameDayINC','SameDayCRQ','NotSameDayINC','NotSameDayCRQ',
        'StillOpenINC','StillOpenCRQ','OpenOldINC','OpenOldCRQ','OtherQueuesINC','OtherQueuesCRQ']

    for (var i = 0; i < IDs.length; i++) {
        if (eval(IDs[i]))
            document.getElementById(IDs[i]).innerHTML = eval(IDs[i]);
    }
}

Example query variable:

AllOpenedINC = "('Company*+' = \"test\" OR 'Summary*' = \"%test%\") AND ('Submit Date' >= \"" + theDate +
    " 12:00:00 AM\" AND 'Submit Date' <= \"" + theDate + " 11:59:59 PM\")" + nameINC;

It appears that JavaScript auto-converts certain special characters into HTML entities when outputting content via the innerHTML() function. This is a problem, since I need to be able to output < and > without converting to gt; and lt;

Can this auto-conversion be prevented, reversed, or escaped? So far, no matter what I do, < and > are always automatically encoded into HTML entities.

Example code:

function DisplayQueries() {
    var IDs = ['AllOpenedINC','AllOpenedCRQ','AllClosedINC','AllClosedCRQ','SameDayINC','SameDayCRQ','NotSameDayINC','NotSameDayCRQ',
        'StillOpenINC','StillOpenCRQ','OpenOldINC','OpenOldCRQ','OtherQueuesINC','OtherQueuesCRQ']

    for (var i = 0; i < IDs.length; i++) {
        if (eval(IDs[i]))
            document.getElementById(IDs[i]).innerHTML = eval(IDs[i]);
    }
}

Example query variable:

AllOpenedINC = "('Company*+' = \"test\" OR 'Summary*' = \"%test%\") AND ('Submit Date' >= \"" + theDate +
    " 12:00:00 AM\" AND 'Submit Date' <= \"" + theDate + " 11:59:59 PM\")" + nameINC;
Share Improve this question edited Apr 6, 2011 at 18:02 CXL asked Apr 6, 2011 at 17:48 CXLCXL 1,1122 gold badges19 silver badges38 bronze badges 4
  • Can you post some sample code (and tell us which browser you're running it in)? – JW. Commented Apr 6, 2011 at 17:49
  • Do you want to insert tags (XML of some sort), then you can use .appendChild("<tag>");, but i have a feeling that is not what you want? – Guidhouse Commented Apr 6, 2011 at 17:50
  • More info on what it is, you want to acplish please – Guidhouse Commented Apr 6, 2011 at 17:57
  • Someone solved this here: stackoverflow./a/7394787/2057171 – Albert Renshaw Commented Apr 29, 2021 at 5:35
Add a ment  | 

4 Answers 4

Reset to default 4

You should focus on what you want to acplish as a result, rather than the way of doing it. innerHTML() does encode, innerText() and textContent() do encoding too. So you should decode your strings if you want them as < or > back.

You can use this unescapeHTML() function to get your results as you want them.

 function unescapeHTML() {
    return this.stripTags().replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
  }

I hope this helps. I've copied it from Prototype.

You can use it as

element.innerHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&')

I think your question is based on a false premise. Just make a very simple test:

document.getElementById("testdiv").innerHTML = '<h1><em>Hello</em></h1>';

if this works fine then the problem is not on the JS side, instead you use some other ponents in your system which HTML-encode your characters.

I figured out what's going on. There's no easy way to prevent innerHTML from converting special characters to HTML entities, but since the problem was surfacing when copying the content of a DIV to the clipboard (using IE-only JS, which works since this is in a government environment where everyone has to use IE), I just used the replace() function to re-convert the HTML entities back to < and >.

You can use jquery and .append()

本文标签: Javascriptusing innerHTML to output strings *WITHOUT* HTMLencoded special charactersStack Overflow