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!

4
  • 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. Commented Dec 4, 2012 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. Commented Dec 4, 2012 at 18:17
  • 3
    See this question for the answer to what you've asked specifically, 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/ and http://api.jquery.com/data/ Commented Dec 4, 2012 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. Commented Dec 4, 2012 at 18:19

4 Answers 4

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?

Sign up to request clarification or add additional context in comments.

Comments

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/

1 Comment

however it will lose comment nodes.
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.).

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.