Solved

How do I debug JavaScript

Posted on 2015-02-20
11
92 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 82

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 82

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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 82

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

708 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

19 Experts available now in Live!

Get 1:1 Help Now