Skip to content

Instantly share code, notes, and snippets.

@examinedliving
Created August 22, 2013 16:23
Show Gist options
  • Save examinedliving/6309483 to your computer and use it in GitHub Desktop.
Save examinedliving/6309483 to your computer and use it in GitHub Desktop.
Extract as an html string the doctype tag and html open tag for parsing out doctypes and xmlns when altering html documents.
function getDocTypeStr(htmlStr){
var getByXMLNS,docTypeStr, index=htmlStr.indexOf('<head>');
getByXMLNS=function(){
if(!htmlStr.match(/xmlns/i)){
return false;
} else {
var index_tmp,docTypeStr,newIndex,index=htmlStr.indexOf('xmlns'),
sub_tmp=htmlStr.substr(index);
index_tmp=sub_tmp.indexOf('>');
newIndex=index_tmp + index;
docTypeStr=htmlStr.substr(0,newIndex) + '>';}
return docTypeStr;
}
if(index===-1){
docTypeStr=getByXMLNS() || '<html>';
} else {
docTypeStr=htmlStr.substr(0,index);}
return docTypeStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment