javascript error: Object expected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • barbara09
    New Member
    • Jan 2009
    • 4

    javascript error: Object expected

    here's the code i am using:
    Code:
    <script language="JavaScript" type="text/javascript">
    <!-- begin
    
    function TimedPop() {
    url = "promotional-shirts.html";
    width = 320; // width of window in pixels
    height = 390; // height of window in pixels
    delay = 3; // time in seconds before popup opens
    timer = setTimeout("popup(url, width, height)", delay*1000);
    }
    
    // end -->
    </script>
    in the body:
    Code:
    <body onLoad="TimedPop()">
    here's the error:

    Line: 1
    Char: 1
    Error: Object expected
    Code: 0

    I have no other javascript within my page.

    thanks
    Last edited by gits; Jan 7 '09, 06:26 PM. Reason: added code tags
  • xNephilimx
    Recognized Expert New Member
    • Jun 2007
    • 213

    #2
    There's no javascript native "popup" function, you need to create it first, using window.open

    Comment

    • barbara09
      New Member
      • Jan 2009
      • 4

      #3
      i don't understand. i thought since it's a function, all i have to do is call it in the <body> tag using "onLoad="TimedP op()">" because TimedPop is the name of the function that was place in the <head> tag.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        yes, but in that function you call:

        [code=javascript]
        popup(url, width, height)
        [/code]
        and this function needs to be declared somewhere ... what is already mentioned. in case it is already defined then please post the code for that ... so that we might have a look at it.

        kind regards

        Comment

        • barbara09
          New Member
          • Jan 2009
          • 4

          #5
          i got the following in the <head> of my html page
          Code:
          <script language="JavaScript" type="text/javascript"> 
          <!-- begin 
            
          function TimedPop() { 
          url = "promotional-shirts.html"; 
          width = 320; // width of window in pixels 
          height = 390; // height of window in pixels 
          delay = 3; // time in seconds before popup opens 
          timer = setTimeout("popup(url, width, height)", delay*1000); 
          } 
            
          // end --> 
          </script>
          and in the
          Code:
          <body> tag; i got <body> tag using <body onLoad="TimedPop()">
          Last edited by gits; Jan 7 '09, 10:05 PM. Reason: added code tags

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by barbara09
            i don't understand. i thought since it's a function, all i have to do is call it in the <body> tag using "onLoad="TimedP op()">" because TimedPop is the name of the function that was place in the <head> tag.
            you are right in using this function (TimedPopup()) this way, but (as gits already mentioned) this is not the only function you use. you are using two other functions, namely setTimeout() (which is a built-in function) and popup() (which is not). so if you call popup(), Javascript looks for a function with this name and if it doesn't find such a function definition, it throws the error you received.

            Comment

            • barbara09
              New Member
              • Jan 2009
              • 4

              #7
              so how do i fix it? what do i need to do to get it to work?

              thanks

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by barbara09
                so how do i fix it? what do i need to do to get it to work?
                define the function popup() ← (simple answer)

                extended answer:
                a popup window is defined using the function window.open() (how to define the arguments, see window.open – MDC).
                after you have defined the popup() function, you can call it in setTimeout()

                sample:
                Code:
                function TimedPop() 
                {
                // you variables and computations
                  var popup = function() { 
                    window.open(...); // insert your values
                  }
                var timer = window.setTimeout(popup, time);
                }
                
                window.onload = TimedPop;
                this needs some explanation.

                unless there is a good reason for it, you should declare all variables you use with the "var" keyword (not globally accessible).

                line #10: instead of calling <body onload="..."> it is much more portable to call the onload in the script, so you don't need to put it in the HTML file (including the script does the job). the object "window" is so-to-speak representing the <body> for any event.
                note that the function TimedPop() is in this case not called with parentheses!

                line #4: a so-called "closure" (a more advanced javascript technique). the variable popup holds the function code defined in the curly brackets. the name "closure" derives from the fact, that any static variable value is preserved in it. you can call this function code like a normal function (i.e. popup()).

                line #7: calling setTimeout(), there are two ways calling a function in setTimeout()
                1. insert the code to execute (in quotation marks)
                2. call the function name (without parentheses)

                this is quite a lot of theory, but it will help you writing better javascript. If you have questions just ask.

                regards

                Comment

                Working...