Search ( Min 3 characters )
Search ( Min 3 characters )
| Tech | Title | Updated |
![]() ![]() | Setting the Page Title in Javascript | 2012-04-10 10:30:41 |
To change the title of you page, use document.title = "new page title."; | ||
![]() | How to get Client IP Address | 2012-04-14 18:11:31 |
Using JQuery we can get the IP Address of the client using the following. This also requires a PHP file on the server that actually gets the IP address and returns the IP as JSON Data. - JavaScript - <script> $.get("getip.php", function(data) { alert("Your IP Address is : "+data); }); </script> The PHP File "getip.php" <?php header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 1 Jan 1990 00:00:00 GMT'); header('Content-type: application/json'); $ip="unknown"; if ( !empty($_SERVER['HTTP_CLIENT_IP'])) { $ip=$_SERVER['HTTP_CLIENT_IP']; } elseif ( !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip=$_SERVER['REMOTE_ADDR']; } echo $ip; ?> | ||
![]() | Get MS time from January 1st 1970 in Javascript | 2012-04-14 18:05:30 |
Getting the current javascript date and time as milliseconds since midnight of January 1, 1970. var milliseconds = new Date().getTime(); | ||
![]() | Javascript - Array Key Values pairs example | 2012-04-14 18:06:57 |
using arrays in Javascript is simple. You can even use keys or text as identifiers. This is known as an associative array for example: var myarray = {}; // populate the array with data myarray['bob']={}; myarray['bob']['age']=10; myarray['bob']['location']="london"; // create another person in the array. myarray['fred']={}; myarray['fred']['age']=13; myarray['fred']['location']="spain'; you can then access the variables using: var age = myarray['fred']['age']; | ||
![]() | Get String Length | 2012-03-29 12:10:03 |
To get the length of a string in javascript use: var mystring = "test string"; var string_length = mystring.length; | ||
![]() | Handling KEY Events in Javascript | 2012-04-14 18:04:56 |
The easiest way to register for handling keyevents is to do it in the <body> <body onkeypress = "keyHandler(event)"> When a key is pressed , the event is sent to the function keyHandler.. <script> function keyHandler( evt ) { if ( evt!=null ) { alert("you pressed key : "+evt.keyCode); } } </script> | ||
![]() | A 2D JSON Data Array | 2012-03-29 14:38:20 |
The format of a 2D array in JSON with Javascript var data = { "array1": { "value1":100 , "value2":50 } , "array2": { "value1":500 , "value2":500 } , "array3":{ "value1":124 , "value2":353 } }; Remember with any JSON data structure or array type like this, alway exclude the final comma , if not you will end up with one extra array. | ||
![]() | Get a TextField or TextArea Value | 2012-03-29 14:41:10 |
Example of how to get a TextField or TextArea value in Javascript <html> <input id="textid" type="text" /> <textarea id="tarea"></textarea> <script> var textfieldvalue= document.getElementById("textid").value; var textareavalue= document.getElementById("tarea").value; alert(textfieldvalue); alert(textareavalue); </script> </html> | ||
![]() | Random Numbers | 2012-04-04 08:29:27 |
To get a random number you have to you the Math Library. you dont have to download or install anything its already built in to javascript.. var rand = Math.random() This will return a float random number between 0.0 and 1.0 If you want to have a random number from say 0..100 you would use the following. var rand = ( Math.random()*100 ) | ||
![]() | Javascript Timers | 2012-03-29 14:44:55 |
<script> function mytimer() { alert("hello"); setTimeout("mytimer()",10000); } mytimer(); </script> The above is a simple javascript timer. The call to the method is invoked when the page loads, so calls the timer. After you have finished processing whatever you need to do in the timer. you can either finish there, or tell the timer to kick off again. In the above example, the timer is restarted, and will be called again after 10 seconds. The delay is always in milliseconds. so 10 seconds = 10 * 1000 = 10000 Last Edit: 11 months, 3 weeks ago by admin. | ||
![]() | How to dynamically create a DIV with Javascript | 2012-03-29 14:45:24 |
Sometimes you need to create a DIV on the fly, maybe when a user clicks a button , and you want to display a message box.. here is the code to create a DIV and add it to another object, in this instance the "parent" div object var container = document.getElementById("parent"); var divv = document.createElement("div"); divv.className = "classname"; divv.id = "idofdiv"; divv.style.position= "absolute"; divv.style.top = "0px"; divv.style.left = "100px"; divv.style.width = "100px"; divv.innerText = "Hello"; container.appendChild(divv); | ||
![]() | get the Date and Time using Javascript | 2012-03-29 14:46:02 |
This will break the Date object into its components. var dte = new Date(); var hours= dte.getHours(); var minutes = dte.getMinutes(); var seconds = dte.getSeconds(); var year=1900+dte.getYear(); var month=dte.getMonth(); var day=dte.getDate(); | ||
![]() | Javascript Popup Prompt | 2012-03-29 14:47:06 |
Javascript can allow create a dialog that pops up on the screen, very much like the confirm dialog, but can also allow the user to enter information as well, like a user name <script> var name=prompt("Please enter your name","enter name"); if (name!=null && name!="") { // a name has been entered // you can now user the name in your code. } </script> the prompt takes 2 parameters, the first is the message on the dialog, the second is the text that is populated in the text field. This can be blank, or if you are editing this can be populated with the text that is to be changed. | ||
![]() | Javascript Confirm Prompt | 2012-03-29 14:47:38 |
Sometimes you need to ask the user a question that can be ok, or cancel. For example. "Are you sure you want to erase the hard drive...?" Javascript has a nice function called confirm for this.. <script> var result=confirm("Do you want to delete?"); if (result==true) { alert("You pressed OK!"); } else { alert("You pressed Cancel!"); } </script> | ||
![]() | Javascript Alert Box | 2012-03-29 14:48:10 |
If you want your page to pop up an alert box, here is the code for that.. <script> function doPopup(message) { alert(message); } </script> <input type="button" onclick="doPopup('Hello');" value="press me" /> pressing the button will cause the popup to appear. | ||
![]() | Using Javascript Reflection | 2012-04-04 08:24:17 |
Reflection in Javascript is very basic. If you have a class with properties, these are all accessable as follows. myClass['propertyName'] With this you can set and get values, and even call the methods myClass['proprtyName']=50; var i = myClass['propertyName']; var myFunction = myClass['aFunction']; myFunction(); | ||
![]() | Iterate a JavaScript associative Array / Key Pair | 2012-04-04 09:08:15 |
to iterate through an associative array: var myList { "a" : 1, "b" : 2, "c" : 3, "d" : 4 }; for(var idx in myList) { document.write( idx + " : " + myList[index] + "<br />"); } And the output a : 1 b : 2 c : 3 d : 4 | ||
![]() | How to set Fixed decimal places with a JavaScript Float | 2012-04-25 11:30:11 |
to set the number of decimal places use toFixed(places) var f = 4.5234234 var g = f.toFixed(3) // g = 4.523 | ||
![]() | Javascript reflection, using reflection to list members and variables | 2012-05-16 11:08:45 |
In java script we can use reflection to list all the members and variables of a class, The concept is the same as iterating through an array for ( var methods in object ) { var meth = methods; var value = object[methods]; } A complete example of listing methods and variables using reflection <script> bob = function() { this.a = 1; this.b = 2; this.meth = function(test) { var c=a+b; } } var bb = new bob(); for ( var methods in bb ) { document.write("name: "+methods+"<br>"); document.write("value: "+bb[methods]+"<br><br>"); } </script> the output from the above code name: a value: 1 name: b value: 2 name: meth value: function (test) { var c=a+b; } | ||
![]() | Javascript Stack trace in firefox | 2012-05-16 12:49:05 |
A very simple way to produce a stack trace in firefox is: function printStackTrace() { try { noneExistantVariable+=0; //forces a catch with the stack trace } catch(e) { print(e.stack); } } | ||
![]() | return JSON String | 2012-04-14 18:28:23 |
To return a JSON string in PHP, we use the following <?php $arr = array ('Key1'=>1,'Key2'=>2,'Key3'=>3,'Key4'=>4); echo json_encode($arr); ?> we can then echo the encoded value for the JavaScript or other application to receive. | ||



