最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IE innerHTML chops sentence if the last word contains '&' (ampersand) - Stack Overflow

programmeradmin1浏览0评论

I am trying to populate a DOM element with ID 'myElement'. The content which I'm populating is a mix of text and HTML elements.

Assume following is the content I wish to populate in my DOM element.

var x = "<b>Success</b> is a matter of hard work &luck";

I tried using innerHTML as follows,

document.getElementById("myElement").innerHTML=x;

This resulted in chopping off of the last word in my sentence. Apparently, the problem is due to the '&' character present in the last word. I played around with the '&' and innerHTML and following are my observations.

  1. If the last word of the content is less than 10 characters and if it has a '&' character present in it, innerHTML chops off the sentence at '&'.
  2. This problem does not happen in firefox.
  3. If I use innerText the last word is in tact but then all the HTML tags which are part of the content bees plain text.

I tried populating through jQuery's #html method,

 $("#myElement").html(x);

This approach solves the problem in IE but not in chrome.

How can I insert a HTML content with a last word containing '&' without it being chopped off in all browsers?

Update : 1. I tried html encoding the content which I am trying to insert into the DOM. When I encode the content, the html tags which are part of the content bees plain string.

For the above mentioned content, I expect the result to be rendered as,

Success is a matter of hard work &luck

but when I encode what I actually get in the rendered page is,

<b>Success</b> is a matter of hard work &luck

I am trying to populate a DOM element with ID 'myElement'. The content which I'm populating is a mix of text and HTML elements.

Assume following is the content I wish to populate in my DOM element.

var x = "<b>Success</b> is a matter of hard work &luck";

I tried using innerHTML as follows,

document.getElementById("myElement").innerHTML=x;

This resulted in chopping off of the last word in my sentence. Apparently, the problem is due to the '&' character present in the last word. I played around with the '&' and innerHTML and following are my observations.

  1. If the last word of the content is less than 10 characters and if it has a '&' character present in it, innerHTML chops off the sentence at '&'.
  2. This problem does not happen in firefox.
  3. If I use innerText the last word is in tact but then all the HTML tags which are part of the content bees plain text.

I tried populating through jQuery's #html method,

 $("#myElement").html(x);

This approach solves the problem in IE but not in chrome.

How can I insert a HTML content with a last word containing '&' without it being chopped off in all browsers?

Update : 1. I tried html encoding the content which I am trying to insert into the DOM. When I encode the content, the html tags which are part of the content bees plain string.

For the above mentioned content, I expect the result to be rendered as,

Success is a matter of hard work &luck

but when I encode what I actually get in the rendered page is,

<b>Success</b> is a matter of hard work &luck

Share Improve this question edited Apr 1, 2010 at 12:54 munity wiki
2 revs
Mandai
Add a ment  | 

3 Answers 3

Reset to default 6

You should replace your & with &amp;.

The & (ampersand) character is used within HTML to represent various special characters. For example, &quot; = ", &lt; = <, etcetera. Now, &luck clearly is not a valid HTML entity (for one it is missing the semicolon). However, various browsers may, due to binations of error correcting (the semicolon), and the fact that it looks somewhat like an HTML entity (& followed by four characters) try to parse it as such.

Because &luck; is not a valid HTML entity, the original text is lost. Because of this, when using an ampersand in your HTML, always use &amp;.

Update: When this text is entered by a user, it is up to you to escape this character properly. In PHP for example, you would call htmlentities on the text before displaying it to the user. This has the added benefit of filtering out malicious user code such as <script> tags.

The ampersand is a special character in HTML that indicates the start of a character entity reference or numeric character reference, you need to escape it like so:

var x = "<b>Success</b> is a matter of hard work &amp;luck";

Try using this instead:

var x = "<b>Success</b> is a matter of hard work &amp;luck";

By HTML encoding the ampersand, you are ensuring that there is no ambiguity in what you mean when you write "&luck".

发布评论

评论列表(0)

  1. 暂无评论