Welcome to WebmasterWorld Guest from 54.159.165.175

Forum Moderators: open

How do I de-bug my javascript? Pop-up windows not popping up!

Is there a web site or free software that helps debug JS?

   
7:34 pm on Jun 27, 2002 (gmt 0)

10+ Year Member



I really really don't know a lot at all about javascripts. I got a script to make pop-up windows for my photos. It's not working, though, and I can't figure out why.

Is there some sort of free software that will help me find errors?

Or...Ahem...Do any of you want to try to be my hero? :)

The link is in my profile.

7:39 pm on Jun 27, 2002 (gmt 0)

10+ Year Member



Mozilla 1.0 (http://www.mozilla.org/), besides being a way-cool free browser, is the web developer's friend. On the Tools > Web Development menu is the javascript debugger, which is way way waaayy better than IE's error messages.
7:48 pm on Jun 27, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I took a look and used my trusty moz js debugger and your page gives me "missing ;" as well as a bunch of undefined var errors. I looked at the functions and it looks like you are missing about 50 semicolons. If you start with that it might either work, or give you a more precise error message.
7:56 pm on Jun 27, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



check your stickymail
8:33 pm on Jun 27, 2002 (gmt 0)

10+ Year Member



it looks like you are missing about 50 semicolons

Well, since javascript implies a semicolon at the end of a line, this is not really the problem (although you are right, it would be good practice to terminate her statements with semicolons).

Still, the problem here is that she is not unescaping her quotes in her descriptions.

Anywhere you have something like:

desc = "And she said to me "woo hoo!" and jumped up and down"

you need to have it like:

desc = "And she said to me \"woo hoo!\" and jumped up and down"

Actually, this is a very verbose and repetitive script, and I can't help but think it could be done in a more elegant fashion. Maybe I could get a little time ...

8:47 pm on Jun 27, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I must be half asleep, I missed that.
9:50 pm on Jun 27, 2002 (gmt 0)

10+ Year Member



I must be half asleep, I missed that

Me too, then. It took me a little bit. The semicolon errors that Mozilla was throwing didn't make any sense, though.

Anyway, I had some time on my hands so I modified both the script and the html files.

The new function that replaces all of the repetitive newwin1, newwin2, etc. functions is:

function newwin(obj,width,height,captionHeight) {
// grabs the href from the link
fileName = obj.href;
// grabs the title from the link
desc = obj.title;
// define array
pic = new Array(fileName,width,height,captionHeight);
}

and it is called from the HTML by:

<a href="imageurl" onclick="newwin(this,380,380,0);
popupwin(); return false;" title="imagedescription">
<img src="thumbnailurl" hspace="6"></a>

Note that I am defining the url and the description inline on the link itself, and then calling values in the function. This has the added benefit of allowing visitors to see the image if they have javascript turned off, since the link will then be followed to the image in the main window. Using the title for the caption is icing on the cake (it will also show up on a thumbnail image as a tooltip).

Even so, I'm sure there is probably an even more elegant solution. But now I am out of time.

Lauren, check your StickyMail for the full files.

Oh, one other thing. You still have to escape your quotes in the title attribute, but there you do it the HTML way -- with the &quot; entity.

10:05 pm on Jun 27, 2002 (gmt 0)

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The semicolon errors that Mozilla was throwing didn't make any sense, though.

I think Moz uses "missing ;" for any missing 'punctuation', even a (.

10:09 pm on Jun 27, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I think I remember seeing that before as well tedster. Nice little rewrite moonbiter.
10:23 pm on Jun 27, 2002 (gmt 0)

10+ Year Member



Nice little rewrite moonbiter.

Thanks, that means a lot. I'm still fairly new to scripting myself (< 1 year), and hearing a compliment is encouraging.

Thanks too, tedster, for the tip on the Mozilla's javascript console. I didn't know that.

Now if I could only figure out Venkmann (the Mozilla debugger). ...

3:20 am on Jun 28, 2002 (gmt 0)

10+ Year Member



I just have to say, thank you all, once again, for fixing something that I couldn't. You all rock! Thanks!

Lauren

7:22 am on Jun 28, 2002 (gmt 0)

10+ Year Member



Missing ; means: "I think you meant to start a new statement here, but you don't have a linebreak or a semi-colon." The parser doesn't always guess correctly, though. It sees:

desc="And she said to me "

and thinks: "Hmm, no operators, so that must be the end of the statement. Now, where's the newline? Or the semicolon? Darn, it's missing!"

For relatively simple scripts, Netscape 4 is also useful. If the script doesn't run correctly, or at all, type javascript: into the address field. This will open a JavaScript console and show any JavaScript errors it has encountered during the current session.

About semicolons: No, in JavaScript, it is not bad practice to leave them out. JavaScript was specifically designed for an environment where potentially every single byte may count, so JavaScript statements are terminated either by a semicolon or a newline.

This is different from Java or C, which are compiled languages, and where the source code is designed to be human-readable (more or less). So comments in Java (for example) make no difference to the size of the compiled file, and Java source code is expected to be well-commented for ease of maintenance. In JavaScript, comments merely add bloat, so should be removed before releasing the script.

For long JavaScripts, I actually recommend removing not just comments, but unnecessary whitespace and, yes, semicolons. I've found you can make the file anything up to 50% smaller. I think of it as analogous to compiling a Java application: it's more important for it to be machine-readable than human-readable. I keep a human-readable version on file. (Although, if I find my scripts getting that long, I might decide to switch to a client-side script instead.)

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month