View Full Version : Script functions until I add doctype

07-13-2012, 02:35 AM
I have a script that I got from W3Schoolsh that lets me show/hide my menu. It works fine until I add the doctype, then it ceases to function. I realize I could redo it with jQuery, but I'm more interested in WHY the addition of a doctype would cause it to stop working. Here is the code:

<script type="text/javascript">
var i = -135;
var c = 0;
var intHide;
var navSpeed = 5;

function choosePic() {
if (window.innerWidth < 600) {
document.getElementById('nav').onclick = showHideNav;
document.getElementById("logo").src = "images/grot_white.png";
document.getElementById("tagline").src = "images/tagline_phone.png";
document.body.style.backgroundColor = "#194964";

function showHideNav() {
if (c == 0) {
c = 1;
intShow = setInterval("showNav()",10);
else {
c = 0;
intHide = setInterval("hideNav()",10);
function showNav() {
if (i < -12) {
i = i + navSpeed;
function hideNav() {
if (i > -150) {
i = i - navSpeed;

The original script had a simple <html> and no doctype.

07-13-2012, 02:38 AM
I've had this before too
Mine worked with certain doctypes but not others
I'm curious about the answer also

07-13-2012, 03:44 AM
What is the doctype you added and what is the browser and operating system you are using? Those are majorly important questions.

07-13-2012, 03:53 AM
This line shouldn't do anything, without units a style left is meaningless:


Although maybe some browser with a quirky doctype would set the left to '0pt' as a default when given garbage in.

Old Pedant
07-13-2012, 10:18 PM
MSIE in "quirks mode" (which is usually what you get when you don't use a DOCTYPE) *does* support doing


without the "px", so I'd bet that she was using MSIE.

MSIE 8 and 9, at the least, do pay attention to the DOCTYPE enough that indeed they need to see

document.getElementById('nav').style.left = i + "px";

Not sure about MSIE 7.