Monday, April 4, 2011

Monday April 4th 2011


<script type="text/javascript">
//initialize theBeatles array and store in a variable
var theBeatles = new Array();
//set the values using keys rather than numbers
theBeatles["drummer"]= "Ringo";
theBeatles["RhythmGuitar"] = "John";
theBeatles["BassGuitar"] = "Paul";
theBeatles["LeadGuitar"] = "George";
var indexKey;
//write out each indexKey and the value for that indexKey from the array
for (indexKey in theBeatles) {
document.write("indexKey is " + indexKey + "<br>");
document.write("item value is " + theBeatles[indexKey] + "<br><br>");
}
</script>


here is another tutorial



<script type="text/javascript">
var userNumbers = new Array();
var userInput = 0;
var arrayIndex = 0;
var message = '';
var total = 0;
// loop for as long as the user doesn't input 99
while (userInput != 99) {
userInput = prompt("Enter a number, or 99 to exit", "99");
userNumbers[arrayIndex] = userInput;
arrayIndex++;
}

message += "You entered the following:\n";
for (var i = 0; i < arrayIndex-1; i++) {
message += userNumbers[i] + '\n';
total += Number(userNumbers[i]);
}
message += 'Total: ' + total + '\n';
alert(message);
</script>


Another one:


<script type="text/javascript">
var userNumbers = new Array();
var userInput;
var arrayIndex = 0;
do {
userInput = Number(prompt("Enter a number, or 99 to exit", "99"));
//check that user input is a valid number,
//and if not, break with error msg
if (isNaN(userInput)){
document.write("Invalid data entered: please enter a number between 0 and 99 in numerals");
break;
}
//If break has been activated, code will continue from here
userNumbers[arrayIndex] = userInput;
arrayIndex++;
} while (userInput != 99)
</script>


Here is a simple unobtrusive javascript form validation


<script type="text/javascript">
function checkSearch()
{
if(!document.getElementById || !document.createTextNode)
{
return;
}
if(!document.getElementById('search'))
{
return;
}

var searchValue=document.getElementById('search').value;
if(searchValue=='')
{
alert('Please enter a search term');
return false;
}
else
{
return true;
}
}
</script>

No comments: