Archive for : July, 2011

Javascript print array elements and print object

Print array elements with javascript:


var output = '';
for(var i=0; i < arr.length; i++){ output += "arr[" + i + "] = " + arr[i] + ";"; } alert(output);

Print object with javascript:


var output = '';
for (property in object) {
output += property + ': ' + object[property] +'; ';
}
alert(output);

Javascript – open a popup window using window.open

The syntax of the window.open:


open (URL, windowName[, windowFeatures])

example:

window.open ("http://www.g31zone.com","G31zone");
window.open ("http://www.g31zone.com", "G31zone","status=1,toolbar=1");

Parameters:

status The status bar at the bottom of the window.
toolbar The standard browser toolbar, with buttons such as Back and Forward.
location The Location entry field where you enter the URL.
menubar The menu bar of the window
directories The standard browser directory buttons, such as What’s New and What’s Cool
resizable Allow/Disallow the user to resize the window.
scrollbars Enable the scrollbars if the document is bigger than the window
height Specifies the height of the window in pixels. (example: height=’350′)
width Specifies the width of the window in pixels.

Verify if a checkbox is selected (checked) with jQuery

Verify if a checkbox is selected (checked) with jQuery


<form name="my_form_name" id="my_check_box">
<input type="checkbox" name="my_check_box" id="my_check_box">
</form>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#my_check_box').click(function () {
if (jQuery(this).is(':checked'))
{
alert('My checkbox is checked');
}
}
}
</script>

jQuery – prevent form submitting

Method to prevent form submitting using jQuery:


<form method="post" name="my_form_name" id="my_form_id">
<input type="submit" value="Go">
</form>
<script type="text/javascript">
jQuery('#my_form_id').submit(function() {
alert('Prevent form submitting.');
return false;
});
</script>