Specifying a DOCTYPE breaks my JavaScript....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newsteve1
    New Member
    • Aug 2007
    • 12

    #1

    Specifying a DOCTYPE breaks my JavaScript....

    hi,

    im fairly new to javascript.

    im designing a web page that gives random background colors to some divs. everything works great until i put a DTD (document type definition) at the top of my code. im looking for reasons as to why this is...? I would like to use the DTD to avoid other problems (like quirks mode IE6 etc...)

    my page (as I want it to look - without the DTD) can be found at:
    http://www.stevenewber ry.com/experimental/index3.html

    my page (with the correct DTD - but it wont display coloured divs) is at:
    http://www.stevenewber ry.com/experimental/index2.html

    any insight would be awesome!
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Steve. Welcome to TSDN!

    I notice that you are not quoting the div IDs in allColorPick():
    [code=javascript]
    function allColorPick(){

    colorpick(div1) ;
    colorpick(div2) ;
    colorpick(div3) ;
    colorpick(div4) ;
    colorpick(div5) ;
    colorpick(div6) ;
    }
    [/code]

    Perhaps it should look like this:
    [code=javascript]
    function allColorPick(){

    colorpick('div1 ');
    colorpick('div2 ');
    colorpick('div3 ');
    colorpick('div4 ');
    colorpick('div5 ');
    colorpick('div6 ');
    }
    [/code]

    I don't care what anybody else says; this is why I actually *like* the fact that PHP (oh, and Perl) force you to put a '$' in front of variable names.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Changed thread title to better describe the problem.

      Comment

      • Logician
        New Member
        • Feb 2007
        • 210

        #4
        Originally posted by pbmods
        Perhaps it should look like this:
        [code=javascript]
        function allColorPick(){

        colorpick('div1 ');
        colorpick('div2 ');
        colorpick('div3 ');
        colorpick('div4 ');
        colorpick('div5 ');
        colorpick('div6 ');
        }
        [/code]
        Yes but the colorpick function is still addressing the div directly.

        Code:
        function allColorPick()
        {
         colorpick('div1');
         colorpick('div2');
         colorpick('div3');
         colorpick('div4');
         colorpick('div5');
         colorpick('div6');
        }
        
        function colorpick(myFoo)
        { 
         var choices=["#CFAA00","#826A00","#2F799C","#80D7FF","#008AA7","#00CAF2","#A65300","#F25100","#1BCC4D","#76FC00"];
        
         document.getElementById(myFoo).style.backgroundColor=choices[ Math.floor(Math.random()*choices.length) ];
        }
        
        /* PLEASE REMOVE THE GAP IN THE LINE ABOVE, WHICH WAS INSERTED BY A LONG-TERM BUG ON THIS FORUM */
        
        /* AVOID PASTING THIS CODE WITH MOZILLA, AS IT WILL CAPTURE THE LINE NUMBERS DUE TO ANOTHER LONG-TERM BUG.*/
        
        /* I MAY NOT NOTICE YOUR REPLY IMMEDIATELY, BECAUSE THE EMAIL NOTIFIER IS LONG-TERM BROKEN */
        I don't care what anybody else says; this is why I actually *like* the fact that PHP (oh, and Perl) force you to put a '$' in front of variable names.
        Proper use of the error console is all that's needed, not hideous syntax.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Originally posted by Logician
          /* PLEASE REMOVE THE GAP IN THE LINE ABOVE, WHICH WAS INSERTED BY A LONG-TERM BUG ON THIS FORUM */

          /* AVOID PASTING THIS CODE WITH MOZILLA, AS IT WILL CAPTURE THE LINE NUMBERS DUE TO ANOTHER LONG-TERM BUG.*/

          /* I MAY NOT NOTICE YOUR REPLY IMMEDIATELY, BECAUSE THE EMAIL NOTIFIER IS LONG-TERM BROKEN */
          Not that you're bitter or anything :P

          Good point about the colorpick() function, though.

          Steve, just wanted to let you know that your code is working in Safari, which I find curious. But it works, at any rate.

          Comment

          • newsteve1
            New Member
            • Aug 2007
            • 12

            #6
            Thanks for the input. The changes worked, and thank you Logician for shortening my code by about 47 lines... this is what learning is all about. Speaking of... while I am grateful that it works now, Im still curious as to why my original code worked in safari and worked in firefox if I DIDNT put a doctype, but didnt work in firefox with a doctype... any thoughts?

            Cheers

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Try running the old code (with the DOCTYPE declared) through the W3C Validator. I bet that would give some ideas.

              Comment

              • drhowarddrfine
                Recognized Expert Expert
                • Sep 2006
                • 7434

                #8
                A doctype is, essentially, the set of rules you tell the browser you are following. This is why the doctype is the very first thing you add to any web document and never change it after that. If you change the doctype, you are changing the rules and that is why a page may act differently.

                Comment

                Working...