Jump to content

Correcting Javascript Code Errors


jflint
 Share

Recommended Posts

I am a Web design teacher and I teach JavaScript as part of my HTML course. Many times students have problems with an assigned project working correctly. When I review their JavaScript code it looks 100% correct. I have printed their code and my code and reviewed them line by line with a ruler and found no problems. If I cut and past the code I have into their page it works fine. It appears to me that when they use the backspace key on their key board to make corrections that this is causing them some problems. Can anyone help with this issue?

Link to comment
Share on other sites

What are you typing into? Are you creating an HTML text document? Is your file size exactly the same as the ones that don't work? What errors are occurring?

Link to comment
Share on other sites

If you can provide an example of some of their code, we can see exactly what's not working.Certain "features" only work in some browsers and other things. If the code examples they're using were given from an older textbook they may be specifically targeted at Internet Explorer 5.5 or 6, which is never a good thing.

Link to comment
Share on other sites

Don't know if you know this or not, but there is a few javascript debuggers out there.IE has one built-in: Internet Options > Advanced > Browsing > Uncheck "Disable script debugging (Internet Explorer)"(For those more knowledgeable than I, what does the "Disable script debugging (Other)" do?)I've heard some goods things about Firebug for Firefox, though I've never tried it.

Link to comment
Share on other sites

Guest FirefoxRocks
Don't know if you know this or not, but there is a few javascript debuggers out there.IE has one built-in: Internet Options > Advanced > Browsing > Uncheck "Disable script debugging (Internet Explorer)"(For those more knowledgeable than I, what does the "Disable script debugging (Other)" do?)I've heard some goods things about Firebug for Firefox, though I've never tried it.
Unless you are using Internet Explorer 8, it sucks. Firebug for Firefox and the Google Chrome/Safari debuggers are much better. I haven't tried Opera Dragonfly thoroughly yet, but it seems to be pretty good.And if you are a web design teacher who teachers JavaScript, perhaps you should teach to stricter standards? (no offense meant)
Link to comment
Share on other sites

How did this thread get onto debugging and standards? OP seemed very clear that these weren't the issue.I once had a similar problem as OP's, though not with JavaScript. I work on a Mac, and I find that some text editors produce plain text that Unix/Linux servers don't like at all. It's the old newline/carriage return thing. (No, I don't need recommendations for editors that do work, thanks all the same.)I wonder if that could be the issue here, somehow, and if the problem would disappear if a different editor were used. OP did not specify what editor the students were using. If it's something unusual, maybe it's the problem.Though, again, I've never had such problems with JavaScript or HTML. Browsers are written to expect the unexpected.

Link to comment
Share on other sites

There are actually two types of backspaces, ^H (backspace) and ^? (delete). Most modern systems, I believe, use the latter, however...What editor are they using?

Edited by Synook
Link to comment
Share on other sites

I was wondering what might happen if the new lines were created by "0D0A" and backspace only deleted "0A", leaving the "0D" invisible. It might eventually get shuffled into places where a newline isn't allowed. You'd only need one to trash a function.

Link to comment
Share on other sites

I have had a couple of problems getting scripts copied from books to work, and have scoured them character by character and found nothing amiss, yet the sample code from the accompanying disc works fine. I use ConTEXT...I wonder if it is inconcistent with the response you get from backspace. When I first delete in a document after opening the program the first time, hitting backspace makes the document tree on the left back up until there isn't anywhere to go, THEN it deletes characters. I must start using delete...

Link to comment
Share on other sites

I just realized the code I sent was the uncorrected codce. I found a number of errors in thsi code. I corrected them. I appreciate your comments and will use them.I really appreciate all your help with this below is my student's code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml1 transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>West Lake Landscaping</title><script type="text/javascript"><!--Hide from old browsersfunction scrollColor() { styleObject=document.getElementsByTagName('html')[0].style styleObject.scrollbarFaceColor="#006600" styleObject.scrollbarTrackColor="00aa00"}function loadinfo(myForm) { var menuSelect=myForm.Menu.selectedIndex var menuUrl=myForm.Menu.options[menuSelect].value+".html" window.location=menuUrl}//--></script><style type="text/css"><!--hr { color: #006633 } body { background-image: url(chapter09bkgrnd.jpg);}--></style></head><body onload="scrollColor()"><table width="800" border="0" align="center" cellpadding="2"> <tr> <td width="792"> <div align="center"> <p><img src="images/chapter09banner.jpg" alt="landscape banner" width="800" height="150" /></p> </div> </td> </tr> <tr> <td> <hr size="5" /> <p style="margin-left:10%"> <script type="text/javascript"> <!--Hide from old browsers var today = New Date() var dayofweek = today.toLocaleString() dayLocate = dayofweek.indexOf(" ") weekDay = dayofweek.substring(0, dayLocate) newDay = dayofweek.substring(dayLocate) dateLocate = newDay.indexOf(",") monthDate = newDay.substring(0, dateLocate+1) yearLocate = dayofweek.indexOf("2010") year = dayofweek.substr(yearLocate, 4) var springDate = new Date("March 21, 2010") var daysToGo = springDate.getTime()-today.getTime() var daysToSpring = Math.ceil(daysToGo/(1000*60*60*24)) document.write(<"p style='margin-left:10%; font-family:Arial, sans serif; font-weight:bold; font-size:14'>Today is "+weekDay+" "+monthDate+" "+year+", that leaves only "+ daysToSpring + " days until the start of spring.") document.write("<br />Spring is the time to prepare your landscape for new growth of flowers and lawns. ") document.write("<br />Call us now at (221) 555-9100 for a free consultation and a free estimate.</p>") //--> </script> </p> <hr size="5" /> </td> </tr> <tr> <td> </td> </tr> <tr> <td><p>West Lake Landscaping has been in business since 1980 and our employees have over 100 years of combined landscape experience and knowledge. </p> <p>West Lake’s mission is to provide you with a beautiful exterior to your home that matches your personality and extends a warm invitation to visitors. West Lake pledges that no task will be too small to merit our attention. </p> <p>West Lake is a full service landscape construction and maintenance company that can provide year round service in lawn, leaf, and snow removal. We can provide a plan to provide outdoor beauty to match the seasons, from spring flowers through fall fauna.</p> <p>Whether you are looking to add a new patio or deck, replace old bushes and shrubs, revitalize a backyard garden with a pond, or just add a new approach to your home with a brick walkway and flowers, West Lake can provide you with a landscape plan to fit your budget and your property size and style. </p> <p></p></td> </tr> <tr> <td> <form name="infoMenu" action=" "> <p align="left" style="font-weight:bolder"> Select an item from the list to get more information about using stones, flowers, or general design principles:<br /> <select name="Menu" onchange="loadInfo(this.form)"> <option value="chapter09westlake">Select an information item</option> <option value="chapter09stones">Rocks and Stones</option> <option value="chapter09flowers">Using Flowers</option> <option value="chapter09principles">Landscape Design Principles</option> </select> </p> </form> </td> </tr></table><p> </p><p> </p><script type="text/javascript"><!--Hide from old browsers document.write("<p style='margin-left:10%; font-family:Arial, sans-serif; font-size:xx-small; color:#006600'>) document.write("West Lake Landscaping, Copyright 2009-10010. ") document.write ("<br />This document was last modified "+document.lastModified+"</p>") //--> </script></body></html>We use Internet Explorer 7 and create the code in Notepad or Crimson Editor.As for the backspace being the issue I am just guessing at this point.

Edited by jflint
Link to comment
Share on other sites

There's an error here:document.write(<"p style='margin-left:10%; font-family:ArialI'm sure it's just a result of the copy and paste, but Javascript doesn't support multi-line strings, so make sure the string going to document.write is either all on one line, or is broken up where the first character on the subsequent lines is the plus sign.The main thing to watch out for with copying and pasting code is to make sure the quotes are all regular quotes. Things like Wordpress, for example, don't use regular quotes, they use some UTF character that looks like a quote but isn't. Those won't work in source code, they all need to be regular single or double quotes.Other than that, you can probably skip using HTML comments in the Javascript and CSS sections. I realize that people still do that to hide the code from older browsers, but I'm not aware of a single browser in use that doesn't understand script or style tags. You're using an XHTML doctype, for example, any browser that understands XHTML is going to know what to do with a script or style tag.

Edited by justsomeguy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...