Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect Doctype
function(
a,b // Placeholders
){
with(document.doctype) // To save referencing it each time
return '<!DOCTYPE ' // Making an assumption that the doctype starts with this.
+ name // Usually 'html'
+ ((b=publicId,a=systemId) // Shorthand references, return value is systemId so we can test if there is a system definition
? // Then
(b // If this is a public standard
? // Then
' PUBLIC "'+b+'" ' // Write the type as "PUBLIC" and the ID
: // Else
' SYSTEM ' // Write the type as "SYSTEM"
)
+ '"'+a+'"' // Add on the system ID
: // Else
'' // Write nothing
)
+'>' // ...and done.
}
function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Simon Madine <http://thingsinjars.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "Detect Doctype",
"description": "Returns the doctype of the current page",
"keywords": [
"html",
"doctype"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>&lt;!DOCTYPE html&gt;</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var myFunction = function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'}
document.getElementById( "ret" ).innerHTML = new Option(myFunction()).innerHTML //Borrowed from @jed's escapeHTML (https://gist.github.com/964847)
</script>
@p01

This comment has been minimized.

Copy link

commented Dec 11, 2011

The version below is 2 bytes shorter and makes sure there is a space before the systemId

function(){with(document.doctype)return'<!DOCTYPE '+name+(publicId&&' PUBLIC '+publicId)+' '+systemId+'>'}
@thingsinjars

This comment has been minimized.

Copy link
Owner Author

commented Dec 11, 2011

I've just reshuffled it so that it includes the 'SYSTEM' type if it's an obscure custom doctype. If we want to skip that (admittedy, extremely rare) case, I'd go with @p01's. Although it does include an extra space if there isn't a systemid.

@subzey

This comment has been minimized.

Copy link

commented Dec 12, 2011

Does this code really need to be wrapped into function(){...}?

At the time any script is executed DOCTYPE is already known, is there any difference between defining new string variable and defining new function that returns that string virtually always?
(Doctype may be changed dynamically, but I had to do so only once and don't want to do it again :) )

Your opinions, please.

@subzey

This comment has been minimized.

Copy link

commented Dec 12, 2011

We can shorten long property names:

function(a,b){with(document.doctype)return'<!DOCTYPE '+name+((b=publicId,a=systemId)?(b?' PUBLIC "'+b+'" ':' SYSTEM ')+'"'+a+'"':'')+'>'}

@thingsinjars

This comment has been minimized.

Copy link
Owner Author

commented Dec 12, 2011

Of course. Before the reshuffle, there was nothing to be gained from shortening the variables but it makes sense now. I've updated and expanded in the annotated version.

As for the more general question of 'does this need to be wrapped in a function', I'm interested to know what others think. Keeping with the rules, yes; Keeping with the spirit, not necessarily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.