Date().getTime() Javascript stupidness

Status
Not open for further replies.

cexxy

Well-known member
125
Hello. I'm trying to shoehorn a webcam script I took off the internet (www.spotlife.com) into my own personal one. While i'm not familiar with Javascript, I am pretty comfortable with C++, so most of this is familiar to me.

The script isn't working. In IE it says nothing, and in fact, it opens up the folder that the HTML and script are in. In Netscape, it gives me this error:

code:<HR>
Code:
JavaScript Error: file:///C|/webthing v2/webcam6.0.js, line 16:<P>missing ) in parenthetical.<P>        var downloadStart = (new Date().getTime());
..............................^

I can include the whole script if you want to see the rest of it, but it seems like it should work, especially since i didn't change that part from what was in the original script. I understand about pointers in C++, and i assume that this is what the 'new' is all about in Javascript.

also, it doesn't like the new Date().getTime() in any of the lines it's in. I get the same error for all of them. Is there something I have to do to make it happy?

edit: silly formatting errors
 

HitScan

Ars Tribunus Militum
2,523
Subscriptor++
HitScan: No. There is no global Date object; he needs to instantiate a Date object (hence the "new") and requires to pass an argument list (in this case, an empty one).
Uh oops. :D

Cexxy: listen to stickboy or 0xfc00, either of their suggestions will work. I, on the other hand, need to pick up a book sometime so I'm not always guessing :D

HitScan
 

cexxy

Well-known member
125
0xfc00, I was thinking about your suggestion. I saw your method someplace, but I didn't implement it because in the code i *borrowed* it's done just like I said. Date().getTime() And the browser doesn't do any funny stuff when it's run, so why should it start retching when I do it?

Annoying!

I'll post the whole script if it comes to that, but that seems like poor forum manners. I'm not sure.
 

cexxy

Well-known member
125
Don't you need to put parentheses around the "new Date()" part? i.e.:

yay! That's exactly what I needed to do! Thank you.

Now instead of Javascript errors it just does a directory listing in BOTH BROWSERS! Joy.

I have two questions:

1. Why do I type " (new Date()).getTime()" and not "new (Date().getTime())" like I was thinking? is it just to somehow tie the 'new' to Date()? If so, then why exclude getTime() from the parenthesis?

2. Why is it listing the directory contents?
 

cexxy

Well-known member
125
oh, also, apparently my version of Netscape gives the same error on their script, too. I swear it worked before, but maybe I just looked at it in IE. Then again, I have Netscape 4.05, so maybe that's part of the reason.

I think I'll go upgrade since I've nothing better to do but bother everyone without doing my homework first. (but please help me if you can)
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by Cexxy:
1. Why do I type " (new Date()).getTime()" and not "new (Date().getTime())" like I was thinking? is it just to somehow tie the 'new' to Date()? If so, then why exclude getTime() from the parenthesis?

You need to brush up on object-oriented programming.

"new Date()" instantiates a new Date object. The "Date()" part calls the Date constructor with no arguments.

Each Date object has a getTime() method, but you need to create the object first before you can use it. Hence, you need to make sure that the "new Date()" portion occurs first, so you force precedence with parentheses.

I'm not sure why the "new" operator has lower precedence than the "." operator, though, since "new (Date().getTime())" makes no sense.

2. Why is it listing the directory contents?

No idea. Sounds like a server problem, because client-side scripting won't let you do arbitrary things like viewing directory contents on the server.
 

cexxy

Well-known member
125
Actually, stickboy, what I need to brush up on is Javascript. OOP I still remember pretty well. I just didn't understand that Javascript needed to be written that way. That's all.

Also, about the directory listing thing, on my computer it lists the directory contents, on the server it takes me back to the base directory and displays the index.htm file. If that makes any sense the way I said it...

ugh...what the hell is the matter with this thing?
 
Status
Not open for further replies.