0

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
3
  • You know that the "else" part will not only never be executed, but it also does the same thing as the other, right? Commented Aug 22, 2009 at 13:10
  • I just fixed my code. The 'inputid' has been corrected. Commented Aug 22, 2009 at 13:13
  • 1
    I'm surprised at the calmness of the answers, but perhaps because it's JavaScript and not something like C++. Formatting/code style issues can be a bit of a religious issue for programmers, some of whom would rather gouge their eyes out than put { on a new line. Not I though. ;) Commented Aug 22, 2009 at 14:38

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
Gotchya, makes perfect sense. =)
2

Try to avoid multiple calls to document.getElementByID:

var objInputId = document.getElementById( 'inputid' );
objInputId.focus();
objInputId.value = "val";

2 Comments

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: Yes, that code could effectively replace the entire body of the formfocus function. See my answer (currently below) for full context.
1

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.

Comments

0

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
;
}

2 Comments

Newlines can matter in JavaScript, when you forget a semicolon. Not something to rely on, obviously...
Yes, I read that same thing regarding newlines: en.wikipedia.org/wiki/…
0

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.)

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.