Solved

How do I debug JavaScript

Posted on 2015-02-20
11
98 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 13

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

Author Comment

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

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSV export single column not inserting comma's in file 11 67
Save data in two Database, Asp 2 69
Error Handler 8 42
transfer content to new site design 7 49
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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