PDA

View Full Version : Javascript - Switching DIV's



learning2play
23 Jul 2010, 05:05 PM
Hi, just followed a tutorial which showed how to use javascript to switch between divs.

Script seems to work fine, but theres a part i dont understand.

function ShowContent(d,e) {
if (d.length < 1) {return;} - Not sure what this line here does?
document.getElementById(d).style.display = "block";
document.getElementById(e).style.display = "none";
}

<a
onclick="ShowContent('one','two'); return true;"
href="javascript:ShowContent('one')">
Page 1
</a>

Anyone care to enlighten me? ;)

TheMichael
24 Jul 2010, 05:16 AM
If the length of the string (which is the variable "d" in your example) being passed in is less than 1 character long (in other words empty), return.

"return" immediately stops the function it's inside of and returns the value after it. In the case of no value, it simply stops the function.

Example:


function ShowMessage() {
return;
alert('Hi There');
}
The above example will never alert anything when you call ShowMessage() because it returns before you reach the alert;


function ShowMessage() {
alert('Hi There');
return;
alert('I love Ice Cream');
}
The above example will alert "Hi There", but not "I love Ice Cream".

Additionally,

if (d.length < 1) {return;}

means the same thing as:

if (d.length < 1) {
return;
}

Hope this answers your question.