tags:

views:

156

answers:

6

Good Saturday morning to the most helpful online community on the web! =)

I have a few hundred lines of JavaScript that I'd like to reformat to make it easier for me to read. I thought before I went wasting half of my day, I'd ask my question here first.

I am new to JavaScript and would like to know if my syntax below is correct?

My original code is this:

function formfocus() {
    if(document.getElementById('inputida')) {
     document.getElementById('inputida').focus();
     document.getElementById('inputida').value = '';
    }
    else if(document.getElementById('inputidb')) {
     document.getElementById('inputidb').focus();
     document.getElementById('inputidb').value = '';
    }
}

function popitup(url, name, width, height) {
    newwindow = window.open(url, name, 'width=' + width + ',height=' + height + ',scrollbars=yes');

    if(window.focus) {
     newwindow.focus();
    }
    return false;
}

... And I want to change the code into this (note the spacing):

function formfocus()
{
    if ( document.getElementById( 'inputida' ) )
    {
     document.getElementById( 'inputida' ).focus();
     document.getElementById( 'inputida' ).value = '';
    }
    else if ( document.getElementById( 'inputidb' ) )
    {
     document.getElementById( 'inputidb' ).focus();
     document.getElementById( 'inputidb' ).value = '';
    }
}

function popitup( url, name, width, height )
{
    newwindow = window.open( url, name, 'width=' + width + ', height=' + height + ', scrollbars=yes' );

    if ( window.focus )
    {
     newwindow.focus();
    }
    return false;
}

The differences being:

  1. spacing just after the 'if' and 'else if' statements
  2. spacing around the parentheses
  3. a line-feed before the opening curly braces
+2  A: 

Yes, your new syntax is valid, and equivalent to the old.

Apart from very obscure cases, newlines and spaces in JavaScript code are ignored, so you can lay it out however you like.

But the old syntax is how idiomatic JavaScript is written - an experienced JavaScript programmer looking at your new syntax would think it looked odd.

RichieHindle
"Idiomatic..." Going to have to google that one. Lol
Jeff
@Jeff: 8-) Read it as "JavaScript as written by experienced JavaScript coders". It's like English - native English speakers sometimes express things in ways that sound odd to people who are learning English as a second language.
RichieHindle
Gotchya, makes perfect sense. =)
Jeff
+1  A: 

Yes it is valid.

This increases readability.

But in your release version it would be better to minify your js file to reduce the bandwidth usage.

Jsmin is a javascript minifer.

JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files. It typically reduces filesize by half, resulting in faster downloads. It also encourages a more expressive programming style because it eliminates the download cost of clean, literate self-documentation.

rahul
A: 

First use JSLint to verify the accuracy of your code syntax: http://jslint.com/

Secondly use my Pretty Diff tool to beautify your code: http://mailmarkup.org/prettydiff/prettydiff.html

+2  A: 

Try to avoid multiple calls to document.getElementByID:

var objInputId = document.getElementById( 'inputid' );
objInputId.focus();
objInputId.value = "val";
James Wiseman
In a *big* way!
T.J. Crowder
Could you show me exactly what you mean? Does the example you show above replace something in my original code? If so, which lines? Very much appreciated!
Jeff
@Jeff: Yes, that code could effectively replace the entire body of the ``formfocus`` function. See my answer (currently below) for full context.
T.J. Crowder
A: 

Yes, Javascript is not line based, and most spacing is not required.

You can even write the code in riddiculously unreadable ways like this:

function formfocus(){if(document.getElementById(
'inputid')){document.getElementById('inputid').focus
();document.getElementById('inputid').value='';}else
if(document.getElementById('inputid')){document.
getElementById('inputid').focus();document.getElementById
('inputid').value='';}}function popitup(url,name,width,
height){newwindow=window.open(url,name,'width='+width+',
height='+height+',scrollbars=yes');if(window.focus
){newwindow.focus();}return false;}

Or this:

function
formfocus
(
)
{
if
(
document
.
getElementById
(
'inputid'
)
)
{
document
.
getElementById
(
'inputid'
)
.
focus
(
)
;
document
.
getElementById
(
'inputid'
)
.
value
=
''
;
}
else
if
(
document
.
getElementById
(
'inputid'
)
)
{
document
.
getElementById
(
'inputid'
)
.focus
(
)
;
document
.
getElementById
(
'inputid'
)
.
value
=
''
;
}
}
function
popitup
(
url
,
name
,
width
,
height
)
{
newwindow
=
window
.
open
(
url
,
name
,
'width='
+
width
+
',height='
+
height
+
',scrollbars=yes'
)
;
if
(
window
.
focus
)
{
newwindow
.
focus
(
)
;
}
return
false
;
}
Guffa
Newlines *can* matter in JavaScript, when you forget a semicolon. Not something to rely on, obviously...
bobince
Yes, I read that same thing regarding newlines: http://en.wikipedia.org/wiki/JavaScript_syntax#Whitespace_and_semicolons
Jeff
A: 

Your changes look valid, but there's a lot of cleanup to do in that code.

  • As James Wiseman said, you want to avoid calling document.getElementById repeatedly, it's inefficient and difficult to read.
  • In formfocus, the first two if statements are the same; the second is pointless.
  • In popitup, the code is creating an implicit global variable, which is a Bad Thing. It needs a var.

Thus:

function formfocus()
{
    var elm;

    elm = document.getElementById( 'inputid' );
    if ( elm )
    {
        elm.focus();
        elm.value = '';
    }
}

function popitup( url, name, width, height )
{
    var newwindow = window.open(
        url,
        name,
        'width=' + width + ', height=' + height + ', scrollbars=yes'
    );

    if ( window.focus )
    {
        newwindow.focus();
    }
    return false;
}

(For more about implicit global vars, see blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html; I've made that not a link because it's my own blog -- not that there's much there -- and I don't want to link spam.)

T.J. Crowder
Thanks! =) Regarding the formfocus and redundant getElementById ... The purpose of the second ELSE IF is so that the function can be used for more than one input ID. In other words, I will have several forms on my site that require focus, each using different IDs.
Jeff