Created
October 10, 2011 15:40
-
-
Save dave1010/1275643 to your computer and use it in GitHub Desktop.
jQuery plugin: set the HTML of an element to be the contents of its first comment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global $ */ | |
/** | |
* Replace an element's HTML with the content of its first comment | |
*/ | |
$.fn.commentToHTML = function() { | |
var d = 'extracted-comments'; | |
return this.each(function(i, el) { | |
var $el = $(this); | |
if ($el.data(d)) { | |
// comments have already been extracted | |
// no need to do it again | |
return; | |
} | |
var node = el.firstChild; | |
while (node) { | |
// found a HTML comment | |
if (node.nodeType === 8) { | |
$el | |
.html(node.nodeValue) | |
.data(d, true); | |
return; | |
} | |
node = node.nextSibling; | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question: Why is this useful?
Answer: http://calendar.perfplanet.com/2010/html-lazy-load/