Solved

How do I debug JavaScript

Posted on 2015-02-20
11
100 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
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 …
Suggested Courses

626 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