Category : JavaScript

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

Read More →

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>

Javascript – limit the number of characters in a textarea

Javascript – limit the number of characters in a textarea: <script language=”javascript” type=”text/javascript”> function limitText(fieldLimit, countLimit, limitNum) { if (fieldLimit.value.length > limitNum) { fieldLimit.value = fieldLimit.value.substring(0, limitNum); } else { countLimit.value = limitNum – fieldLimit.value.length; } } </script> <form name=”form_name”> <textarea name=”sometext” onKeyDown=”limitText(this.form.sometext,this.form.counter,100);” onKeyUp=”limitText(this.form.sometext,this.form.counter,100);”> </textarea><br /> (Maximum characters: 100)<br /> You have <input readonly type=”text”

Read More →

Javascript – addslashes and stripslashes

A Javascript function for add slashes: function addslashes(str) { str=str.replace(/\\/g,’\\\\’); str=str.replace(/\’/g,’\\\”); str=str.replace(/\”/g,’\\”‘); str=str.replace(/\0/g,’\\0’); return str; } A Javascript function for strip slashes: function stripslashes(str) { str=str.replace(/\\’/g,’\”); str=str.replace(/\\”/g,'”‘); str=str.replace(/\\0/g,’\0′); str=str.replace(/\\\\/g,’\\’); return str; }

JavaScript redirect a page after 5 seconds

Javascript function to redirect to another page after 5 seconds: function redirect_after_5_seconds() { setTimeout(“location.href=’index.php/new_url_for_redirection'”, 5000); }

Javascript – compare two dates

Compare two dates in Javascript: <html> <head> <title>JavaScript – compare two dates</title> </head> <body> <script language=”Javascript” type=”text/javascript”> var date1 = new Date(“22/12/2010”); var date2 = new Date(“25/12/2010”); if (date1 <= date2) { alert(“Date 1 lower or equal than date 2!”); } else { alert(“Date 2 lower than date 1!”); } </script> </body> </html>

1 2 3