jquery - Expand this code to check if HTML comment in head includes a string -
the <head>
contains:
<!-- foo 1.2.3 author bill --> <!-- foo 1.2.3 author joe -->
i can far code may wrong:
var hc = $('head')[0].childnodes; (var = 0; < hc.length; i++) { console.log(hc); if (hc[i].nodetype == 8) { // comments have nodetype of 8 // need here value , verify 1 of comments includes "bill" } }
my own take on problem create simple function, coupled use of contents()
retrieve child-nodes of given element. function:
function verifycomment(el, tofind) { return el.nodetype === 8 && el.nodevalue && el. nodevalue.indexof(tofind) > -1; }
and use (note i've used element other head
, js fiddle doesn't really/easily offer access head
element of document, changing selector should make work head
well):
$('#fakehead').contents().each(function(){ console.log(verifycomment(this, 'bill')); });
as alternative could, of course, extend prototype of comment
node:
comment.prototype.hascontent = function (needle) { return this.nodevalue.indexof(needle) > -1; }; $('#fakehead').contents().each(function(){ if (this.nodetype === 8 && this.hascontent('bill')){ console.log(this); } });
references:
Comments
Post a Comment