Solved

How do I debug JavaScript

Posted on 2015-02-20
11
93 Views
Last Modified: 2015-02-21
Hi Experts,

Can anyone tell me how to properly debug JavaScript, as I feel browsers messages are often misleading.

For example, I keep getting
SyntaxError: missing ; before statement login:68


My source code is
 <!DOCTYPE html>
<html lang="en">

 <head>
	<meta charset="utf-8">
        <title>RS @ APD</title>
	<link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/main.css">
        <link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/buttons.css">
        <link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/forms.css">
        <link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/scroll-table.css">
	
</head>

    <body>

        <h1>RS @ APD</h1><br>
        <h2>Change Login Password</h2>
        
        <!--Content Start-->

    <!--content-->
    
    <!--Place where appropriate-->
    
 
    
            <!--Content Start-->
        <form name="frm" id="frm" autocomplete="off" method="post" action="?action=PW_Change"
              onSubmit="return checkRequired();">
            <fieldset>
                
            <input type="hidden" name="hidUserID"  id="hidUserID" value="1">
            
            <label for="txtUser" class="required" >User:</label>
            <input type="text" name="txtUser" id="txtUser" value="reservations"
                   readonly="readonly"><br><br>
            
            <label for="txtPWold" class="required">Current Password:</label>
            <input type="password" name="txtPWold" id="txtPWold" maxlength="255" class="required"><br><br>
                        
            <label for="txtPWnew1" class="required">New Password:</label>
            <input type="password" name="txtPWnew1" id="txtPWnew1" maxlength="255" class="required"><br><br>
            
            <label for="txtPWnew2" class="required">Retype Password:</label>
            <input type="password" name="txtPWnew2" id="txtPWnew2" maxlength="255" class="required"><br><br>
            
            <label>&nbsp;</label>
            <input type="submit" id="btnSave" value="Save Changes" class="button">
            <input type="button" id="btnCancel" value="Cancel Changes" onClick="cancelChanges();" class="button"><br>
            </fieldset>        
        </form> 
            
<script type="text/javascript">

    function cancelChanges(){
        if (confirm("Are you sure you want to cancel these changes?")) {
        self.location = "index.php?action=pw_cancel";
        }
    }
    
    function checkRequired(){
        var pwOld = frm.txtPWold.value();
        var pw1 = frm.txtPWnew1.value();
        var pw2 = frm.txtPWnew2.value();
    
        if (pwOld == ''){
            alert 'Must specify old password.';
            return false;
        }
        
        if (pw1.length < 5){
            alert 'The new password must be 5 characters or more.';
            return false;
        }
        
        if (pw1 != pw2){
            alert 'New Password and Retype Password must match.';
            return false;
        }
        
        //all pass
        return true;
    }
  
</script>

        <!--Content Finish-->

    </body>
 
 </html>

Open in new window


Thanks
0
Comment
Question by:APD_Toronto
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 167 total points
ID: 40622277
Sorry, the browsers are the final word on their version of javascript.  IE, Firefox, and Chrome all have slightly different versions of javascript.  And when you run into that slight difference, you will see an error on one and not the others.  IE in my experience has been the pickiest.
0
 
LVL 12

Assisted Solution

by:Jeff Darling
Jeff Darling earned 167 total points
ID: 40622280
have you ever used jsfiddle?

jsfiddle example using your code

One nice thing is the lint feature.  It helps find code errors.
0
 

Author Comment

by:APD_Toronto
ID: 40622284
But, how do I pinpoint, for example the above error?  Whats wrong with it?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40622288
'alert' text requires () around them like this:
 <!DOCTYPE html>
<html lang="en">

 <head>
	<meta charset="utf-8">
        <title>RS @ APD</title>
	<link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/main.css">
        <link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/buttons.css">
        <link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/forms.css">
        <link rel="stylesheet" href="https://localhost/CSS_Reservations/styles/scroll-table.css">
	
</head>

    <body>

        <h1>RS @ APD</h1><br>
        <h2>Change Login Password</h2>
        
        <!--Content Start-->

    <!--content-->
    
    <!--Place where appropriate-->
    
 
    
            <!--Content Start-->
        <form name="frm" id="frm" autocomplete="off" method="post" action="?action=PW_Change"
              onSubmit="return checkRequired();">
            <fieldset>
                
            <input type="hidden" name="hidUserID"  id="hidUserID" value="1">
            
            <label for="txtUser" class="required" >User:</label>
            <input type="text" name="txtUser" id="txtUser" value="reservations"
                   readonly="readonly"><br><br>
            
            <label for="txtPWold" class="required">Current Password:</label>
            <input type="password" name="txtPWold" id="txtPWold" maxlength="255" class="required"><br><br>
                        
            <label for="txtPWnew1" class="required">New Password:</label>
            <input type="password" name="txtPWnew1" id="txtPWnew1" maxlength="255" class="required"><br><br>
            
            <label for="txtPWnew2" class="required">Retype Password:</label>
            <input type="password" name="txtPWnew2" id="txtPWnew2" maxlength="255" class="required"><br><br>
            
            <label>&nbsp;</label>
            <input type="submit" id="btnSave" value="Save Changes" class="button">
            <input type="button" id="btnCancel" value="Cancel Changes" onClick="cancelChanges();" class="button"><br>
            </fieldset>        
        </form> 
            
<script type="text/javascript">

    function cancelChanges(){
        if (confirm("Are you sure you want to cancel these changes?")) {
        self.location = "index.php?action=pw_cancel";
        }
    	}
    
    function checkRequired(){
        var pwOld = frm.txtPWold.value();
        var pw1 = frm.txtPWnew1.value();
        var pw2 = frm.txtPWnew2.value();
    
        if (pwOld == ''){
            alert ('Must specify old password.');
            return false;
        }
        
        if (pw1.length < 5){
            alert ('The new password must be 5 characters or more.');
            return false;
        }
        
        if (pw1 != pw2){
            alert ('New Password and Retype Password must match.');
            return false;
        }
        
        //all pass
        return true;
    }
  
</script>

        <!--Content Finish-->

    </body>
 
 </html>

Open in new window

0
 

Author Comment

by:APD_Toronto
ID: 40622291
How does jsfiddle help?

When I click Save - I get "Forbidden", and Cancel does nothing.

I can't see that it outlines any errors?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:APD_Toronto
ID: 40622296
That fixed cancel, but Save still submits even if fields are blank
0
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 40622306
sorry, I forgot to mention that I fixed a couple of the errors.

specifically the alert statements.  


https://jsfiddle.net/jeffld/9xugf01w/3/

click the jshint button to see the lines that have a problem.
0
 
LVL 17

Assisted Solution

by:OriNetworks
OriNetworks earned 166 total points
ID: 40622309
For basic syntax checking, You could use an online syntax validator http://esprima.org/demo/validate.html
 Although most IDE such as visual studio will highlight errors for you. As for runtime errors you can use browser tools such as internet explorer F12 developer tools. The console tab will show errors as they occur or you can search for a tutorial to set breakpoints, view network traffic, inspect values in realtime. Tools also exist for firefox and chrome
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40622310
I don't see any () around the 'alert' texts.  ??  That is what the original error is.
0
 

Author Comment

by:APD_Toronto
ID: 40623184
In JSFiddle I added () in all alerts, and added 3 === on line 12. I also did the same changes in my local environment, but still "Save Changes" does not work. For example it submits when the current password is empty, but in this case JS should return False and cancel out the Submit as per line 14. Why is this not happening?

As well, why 3 ===, and my IDE (NetBeans) suggested !== on line 22, but why? Even with these changes, Save does not cancel submission.

Please advise
0
 

Author Closing Comment

by:APD_Toronto
ID: 40623396
Had to remove () after .value
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now