Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I debug JavaScript

Posted on 2015-02-20
11
Medium Priority
?
108 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 84

Accepted Solution

by:
Dave Baldwin earned 668 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 668 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 84

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 664 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 84

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

782 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