0

I'm using jQuery to put text into a div as follows:

$("#commentview").html(rowData.Text);

If, for example, rowData.Text is "I haven't seen this.", in IE the results show as:

"I haven't seen this."

However, in Chrome the ' shows properly.

"I haven't seen this."

Any ideas why this would be and if there is a work-around?

Thanks!

UPDATE

I was fooled by Chrome web developer tools. I had added a watch to rowData.Text and it was showing me "I haven't seen this.", so I thought that was what I was working with. But when I looked at the actual JSON response that populates rowData it had "I haven't seen this."

Paul
  • 3,725
  • 12
  • 50
  • 86
  • Both of your examples are identical... not sure what you're talking about. – Sparky Feb 23 '12 at 23:52
  • I don't see a different in the two sentences, can you clarify? – Charlie G Feb 23 '12 at 23:53
  • 1
    there is an `'` there instead of a single quote character. – Misha Reyzlin Feb 23 '12 at 23:54
  • yes, in IE the result shows `'` instead of a single quote character. – Paul Feb 23 '12 at 23:56
  • When you say "the results show" do you mean as a user viewing the page you actually see `'` in the div, or if viewing it with some debugging tool? When I tried it in IE7 it worked fine: http://jsfiddle.net/LxQ8F/. You could try the `.text()` method instead to see if it makes any difference... – nnnnnn Feb 23 '12 at 23:57

2 Answers2

2

My guess would be that your data actually contains that ' and that Chrome turns it into a single quote automatically and IE doesn't. So sanitize your text response on the server or normalize it on client before appending.

As we can see here: http://james.padolsey.com/jquery/#v=1.6.2&fn=jQuery.fn.html jQuery doesn't mess with escaping single quotes or something like that. In the end .html() uses .innerHTML so that is what you should look into to simplify the solution of your problem.

Try console.logging your rowData.Text and see if it indeed contains ' and then do something like the following:

$("#commentview").html(rowData.replace(/'/g, ""));

But what would be better is to solve this on the server side, as ' is not a valid HTML entity: https://stackoverflow.com/a/2083770/236135

Community
  • 1
  • 1
Misha Reyzlin
  • 13,736
  • 4
  • 54
  • 63
  • Yes, I'm pretty sure `'` wasn't supported by older versions of IE because it isn't (or wasn't) a valid html entity, it was introduced as an xml entity and thus worked in xhtml only though as always happens with these things some browsers happily support it anyway. You can use `'` instead of `'` and it will work in IE. – nnnnnn Feb 24 '12 at 00:04
  • You're right. See my update. So now the question is: What is the best way to normalize this on the client? – Paul Feb 24 '12 at 00:09
  • If you have to do it on the client that simple regex should do the trick. – Misha Reyzlin Feb 24 '12 at 00:10
  • I might have to do it on the client. I'm using ASP.NET MVC's built in Json serialization on the server (thanks MS for baking in invalid HTML entities). I'm now wondering if there are any other entities I'd need to look out for. – Paul Feb 24 '12 at 00:12
  • 1
    seems like a trivial replace on the .net side as well: http://www.telerik.com/community/forums/aspnet/editor/amp-apos-is-not-converted-to-in-internet-explorer.aspx `Content.Replace("'","'");` – Misha Reyzlin Feb 24 '12 at 00:15
0

You can used this cross browser script that I wrote for that:

x="abc'<Hello 'World' in Tag>";
for (var i=0,y='';i<x.length;i++) y+='&#' + x.charCodeAt(i) + ';'
b.innerHTML=y
b=document.createElement('div')

This is my First Answer here, so please vote, or feedback, it's exciting me!

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
  • To replace any ' with '. And to replace any string to the right char Code. So you will never have other problems. – Aminadav Glickshtein Feb 24 '12 at 01:44
  • I like the idea, but in my specific case the source data contains `'`, not `'`, so your code doesn't help convert that to something IE can work with. – Paul Feb 24 '12 at 16:17