Object Expected Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • malcster2
    New Member
    • Jun 2008
    • 14

    Object Expected Error

    HELLO, I AM VERY NEW TO JAVA SCRIPT PROGRAMMING.
    I AM PUTTING TOGETHER A PAGE FOR A FRIEND. THAT LOOK DEPENDS VERY MUCH ON SCREEN RESOLUTION, SO I AM TRY TO DETECT IT, AND CHANGE FONTS ACCORDINGLY. MY CODE SO FAR IS:

    Code:
    <head>
    <script type="text/javascript">
    
    function chkwidth()
    {
     alert(window.screen.width);
    
     x = window.screen.width;
     
     if x == 1280
      {
       
       document.write("<style type='text/css'> <!-- .style1 {font-size: 18px} --> </style>");
      }
      
     else
     {
       document.write("<style type='text/css'> <!-- .style1 {font-size: 10px} --> </style>");
      }
    } 
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <body onload="chkwidth()">
    <span class="style1">test text</span>
    </body>
    </html>
    HOWEVER, WHEN I RUN THE PAGE, I ALWAYS GET THE ERROR OBJECT EXPECTED. WHEN I RUN THE FUNCTION WITH JUST 'ALERT(WINDOW.S CREEN.WIDTH) IT WORKS FINE, SO I CAN'T UNDERSTAND WHERE THE PROBLEM IS.

    CAN ANYONE HELP?
    Last edited by acoder; Jun 30 '08, 11:05 AM. Reason: Added [code] tags
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Shouldn't the if be in parenthesis? i.e. if(statement)

    Also, why are you typing in caps?

    Comment

    • malcster2
      New Member
      • Jun 2008
      • 14

      #3
      am typing in caps to differentiate between me and the code, sorry if it seems i'm shouting.

      the if statement is in the curly brackets of the function.

      how would you write it?

      Comment

      • malcster2
        New Member
        • Jun 2008
        • 14

        #4
        sorry, see what you mean, should be if(condition).. ..

        anyway, i don't get the error any more, but the style font is not being set, i.e

        document.write( "<style type='text/css'> <!-- .style1 {font-size: 18px} --> </style>");

        is not being written to page. i know this because the text 'hello' is not being set the sz 18.

        any ideas

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by malcster2
          am typing in caps to differentiate between me and the code, sorry if it seems i'm shouting.
          To differentiate between the text and the code, use code tags - see How to Ask a Question.

          PS. welcome to Bytes!

          Comment

          • malcster2
            New Member
            • Jun 2008
            • 14

            #6
            thanks

            any idea on how to create my style with javascript?

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              To change styles using JavaScript, use the Style object. JavaScript uses camel case, so font-size would be fontSize.

              Comment

              • malcster2
                New Member
                • Jun 2008
                • 14

                #8
                sorry to sound thick, but how would i implement this? at the moment, it's a very basic page, with no div's. so my code is:

                Code:
                function chkwidth()
                {
                 
                
                 if (window.screen.width == 1280)
                  {
                     document.getElementById("style1").style.text.fontSize="28";
                  }
                  
                 
                } 
                ....
                
                <body onload="chkwidth()">
                
                <span class="style1">test text</span>
                
                </body>
                as i say, i'm very new to this, so what should my code look like?
                Last edited by acoder; Jun 30 '08, 09:23 PM. Reason: Added [code] tags

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  document.getEle mentById() accesses an element by its ID, so your span or div should have an id of "style1", e.g.
                  [code=html]<div id="style1">... </div>[/code]
                  It should be style.fontSize, not style.text.font Size. See this link on how to use style.fontSize.

                  One other point to note: what if the screen is wider than 1280?

                  Finally, you shouldn't need to adjust the font with screen size. The user could do it themselves using the browser menu or via keyboard shortcuts as long you use relative measurements, e.g. "em", "px", etc. You could provide this in the browser using links/buttons.

                  Comment

                  • malcster2
                    New Member
                    • Jun 2008
                    • 14

                    #10
                    i've done all my measurements using percentages rather than pixels.
                    but i find that when viewing in lower resolutions, font sizes, if not reset, looks dispraportionat e.

                    i've put my the 1280 check in to start with, but will run checks for other widths.

                    using the code i've already given you, what do you think the best way to write it is?

                    also, to clarify, what exactly is wrong with my code i have written in the previous post?

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Code:
                      document.getElementById("style1").style.text.fontSize="28";
                      should be
                      Code:
                      document.getElementById("style1").style.fontSize="28px";
                      and
                      Code:
                      <span class="style1">test text</span>
                      should be
                      Code:
                      <span id="style1">test text</span>

                      Comment

                      • malcster2
                        New Member
                        • Jun 2008
                        • 14

                        #12
                        thanks, works fine. hope to speak again

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          No problem. Since you're new to JavaScript, why not take a look at this tutorial to get you up and running.

                          Comment

                          Working...