By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,608 Members | 1,925 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,608 IT Pros & Developers. It's quick & easy.

javascript error: Object expected

P: 4
here's the code i am using:
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript">
  2. <!-- begin
  3.  
  4. function TimedPop() {
  5. url = "promotional-shirts.html";
  6. width = 320; // width of window in pixels
  7. height = 390; // height of window in pixels
  8. delay = 3; // time in seconds before popup opens
  9. timer = setTimeout("popup(url, width, height)", delay*1000);
  10. }
  11.  
  12. // end -->
  13. </script>
  14.  
in the body:
Expand|Select|Wrap|Line Numbers
  1. <body onLoad="TimedPop()">
  2.  
here's the error:

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

I have no other javascript within my page.

thanks
Jan 7 '09 #1
Share this Question
Share on Google+
7 Replies


xNephilimx
Expert 100+
P: 213
There's no javascript native "popup" function, you need to create it first, using window.open
Jan 7 '09 #2

P: 4
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="TimedPop()">" because TimedPop is the name of the function that was place in the <head> tag.
Jan 7 '09 #3

gits
Expert Mod 5K+
P: 5,390
yes, but in that function you call:

Expand|Select|Wrap|Line Numbers
  1. popup(url, width, height)
  2.  
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
Jan 7 '09 #4

P: 4
i got the following in the <head> of my html page
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript" type="text/javascript"> 
  2. <!-- begin 
  3.  
  4. function TimedPop() { 
  5. url = "promotional-shirts.html"; 
  6. width = 320; // width of window in pixels 
  7. height = 390; // height of window in pixels 
  8. delay = 3; // time in seconds before popup opens 
  9. timer = setTimeout("popup(url, width, height)", delay*1000); 
  10.  
  11. // end --> 
  12. </script> 
  13.  
and in the
Expand|Select|Wrap|Line Numbers
  1. <body> tag; i got <body> tag using <body onLoad="TimedPop()">
Jan 7 '09 #5

Dormilich
Expert Mod 5K+
P: 8,639
@barbara09
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.
Jan 7 '09 #6

P: 4
so how do i fix it? what do i need to do to get it to work?

thanks
Jan 8 '09 #7

Dormilich
Expert Mod 5K+
P: 8,639
@barbara09
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:
Expand|Select|Wrap|Line Numbers
  1. function TimedPop() 
  2. {
  3. // you variables and computations
  4.   var popup = function() { 
  5.     window.open(...); // insert your values
  6.   }
  7. var timer = window.setTimeout(popup, time);
  8. }
  9.  
  10. 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
Jan 8 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.