Jump to content

Javascript errors


Darkranger85

Recommended Posts

Hey guys,

 

I'm trying to add a little bit of JavaScript to my toolbox of knowledge and I'm finding that, for me, it's a lot harder to pick up than PHP was.

 

The main reason for this being that when there is an error in my code, it doesn't seem to tell me anything about the error. It just "doesn't work."

 

This is extremely frustrating because it makes my progress very very slow because I have to sit there and search my entire code trying to find where the error is and considering I just started learning JS, that can take a long time cause I'm not as familiar with the syntax.

 

Is there some kind of "error reporting" that I have to turn on or something?

 

Thanks!

Link to comment
Share on other sites

I also am a fairly new JS developer/user and know what you mean.  For me, doing my testing in IE, I find that the little yellow icon in the lower left corner provides a handy pointer to the offending line of code and usually clues me into which line needs my attention.  Double click it and see what it says next time you see it appear.  Also if you are testing your work on an iphone or ipad, turn on the debug console in Safari.  Similar info is provided there.

Link to comment
Share on other sites

Thanks, that's good to know!

 

I guess a lot of my problem comes from not knowing the syntax all that well. But, thinking back, I guess it was the same when I started learning PHP. Getting errors in just about every single test run, normally ending up being something stupid like missing a semi colon and such lol.

 

But I just can't figure this out.

 

        function RegValidate(){
            var error = new Array();
            //Setup error array and validation variables//
            var _user = document.forms['register'].username.value;
            var _pass = document.forms['register'].password.value;
            var _pass2 = document.forms['register'].password2.value;         
            var _email = document.forms['register'].email.value;
            var _email2 = document.forms['register'].email2.value;	                       
                       
            if(empty(_user)){
                error[] = 'Please enter a username.';
                alert('I hate JavaScript!'); 
            }

        }

 

The code stopped working after I tried to add string to the error array. I looked up arrays and how to insert information into them and it seems right to me. :(

Link to comment
Share on other sites

IE sucks for developing JavaScript as it's developer tools leave a lot to be desired. Firefox has an extension called firebug which is awesome, and chrome has a built in console. Both of these tools help a great deal.

Link to comment
Share on other sites

Javascript doesn't have a method called empty. You would have to compare the value to empty string (e.g. val == ''). To add content to an array, you would use the push method (e.g. my_arr.push('my value'); )

 

Sorry, I should have also included this:

 

        function empty(obj) {
                if (typeof obj == 'undefined' || obj === null || obj === '') return true;
                if (typeof obj == 'number' && isNaN(obj)) return true;
                if (obj instanceof Date && isNaN(Number(obj))) return true;
                return false;
            }

 

my own empty function! :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.