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:
- spacing just after the
'if'
and'else if'
statements - spacing around the
parentheses
- a line-feed before the opening
curly braces