1

Possible Duplicate:
Selecting HTML Comments with jQuery
Ajax functions like .load() strip out comments from the HTML. How can I keep the comments?

We are doing an AJAX call which is result is coming via

$('#page_section').html(result);

The problem is that the result is being stripped from all the HTML comments we have in there. We need the HTML comments to stay there since they contain JSON objects which we need later by our CMS. Any idea way to prevent HTML comments from being stripped with using .html().

Thanks!

Community
  • 1
  • 1
Joe Saad
  • 1,940
  • 3
  • 22
  • 32
  • 1
    For this kind of needs I use hidden textareas with the data in it. HTML comments are not intended for such use at all. – Ulflander Dec 04 '12 at 18:17
  • If you use an ID, why not just use vanilla-javascript? `document.getElementById("page_section").innerHTML = result;`. I just read an answer on SO that says that .innerHTML() doesn't strip comments, bu I never tested it. – 11684 Dec 04 '12 at 18:17
  • 3
    [See this question for the answer to what you've asked specifically](http://stackoverflow.com/questions/1623734/selecting-html-comments-with-jquery), but also, jQuery seamlessly integrates with `data-` attributes on HTML elements, allowing you to call the `.data()` method on any jQuery object to get and set values. That might be a cleaner, saner way to handle passing data with the HTML, rather than trying to put the JSON in comments and then parse it out. See [http://api.jquery.com/jQuery.data/](http://api.jquery.com/jQuery.data/) and [http://api.jquery.com/data/](http://api.jquery.com/data/) – Michael Schuller Dec 04 '12 at 18:19
  • 1
    You're aware that malicious users can tamper with the markup in their browsers, right? Relying on important data getting back to your server this way is a dangerous assumption. Same goes for hidden fields. – wsanville Dec 04 '12 at 18:19

4 Answers4

3

The html() method does not strip comments: http://jsfiddle.net/WEQW9/

It's the ajax method, checkout Ajax functions like .load() strip out comments from the HTML. How can I keep the comments?

Community
  • 1
  • 1
Ulflander
  • 648
  • 5
  • 15
0

Could you not use the .append() or .prepend()?

Example:

$('#page_section').append(result);
$('#page_section').prepend(result);

This will append or prepend the result to the matched element.

http://api.jquery.com/append/ http://api.jquery.com/prepend/

Yoda
  • 121
  • 1
  • 12
0

One way is to use plain Javascript:

$(selector)[0].innerHTML = 'String with comments JSON and BlackJack';

Second and the best one - you can use element attributes for json passing (even class attr.).

Michael Malinovskij
  • 1,422
  • 8
  • 22
0

If you are using an ID (you are in the question) you could just use vanilla javascript:

document.getElementById("page_section").innerHTML = result;

As stated (implicitly) by this answer .innerHTML() does create comment nodes.

I must say I agree with Michael C Schuller, use the data attribute for this, it is made for this type of matters.

Community
  • 1
  • 1
11684
  • 7,356
  • 12
  • 48
  • 71