• 1st, Aug 2010

Javascript Handbook and Tips

/**
* Fundamentals
*
**/
//Get Html Objects
document.getElementById('id')

//Timeout, delay...
setTimeout("alert('hi!')",3000);

//Change url, location, href
window.location = "/index.php";

//Replace texts dynamically
document.getElementById('FlashCode').innerHTML = flashcode

//Replace strings in vars
string.replace(findstring,newstring);

//Html Check Buttons
document.forms.form.license.checked='checked'; //set value
if(document.forms.form. license.checked) {} //check value

//Print html
document.write("");

//Visibility elements
document.getElementById('error2').style.visibility = 'visible';
document.getElementById('error2').style.visibility = 'hidden';	

//Radio buttons
0.12<input type="radio" class="text" name="ProfilePriceID" checked="checked" value="0.12">
0.56<input type="radio" class="text" name="ProfilePriceID" value="0.56">
//set value
document.forms.frmemail.ProfilePriceID_New[0].checked=true
//get value
document.forms.frmemail.ProfilePriceID[0].value -> 0.12
//if checked
document.forms.frmemail.ProfilePriceID[0].checked -> on
//PHP get by post
$_POST["ProfilePriceID"] -> 0.12 (the value of the radio button checked)

// Html Select - Drop Down Element
document.getElementById('giftfinderprice').selectedIndex=3;
//or
<option selected value='3'>three</option>
//select the option by value in a select box
<select id="filterBy">
	<option name="one">one</option>
	<option name="two">two</option>
	<option name="three">three</option>
</select>
<script>
	document.getElementById("filterBy").selectedIndex = document.getElementById("filterBy").options['two'].index;
</script>

//New Window
window.open('www.google.es',null,'width=400,height=200,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes')"
//To make IE compatible: don't change the null var and don't add spaces beween the properties.

//How to round with 2 decimals: save two decimals with times 100, round it, and divide by 100
Math.round(5.95 * 1.15 * 100) / 100

/**
* Cookies
*
**/
// Read
		var nameEQ = "played" + "="; //look for the value 'played'
		var ca = document.cookie.split(';');
		var value;
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) value=c.substring(nameEQ.length,c.length);
		}
// Write
		var expirationDate = new Date;
		expirationDate.setMonth(expirationDate.getMonth()+6);
		document.cookie = "played=1; expires="+ expirationDate.toGMTString(); // Create the cookie

/**
* Useful Snippets
*
**/

// Dynamic load source file
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", "http://localhost:8888/floating.js");
document.getElementsByTagName('head')[0].appendChild(fileref);  

// Destroy element
var webactorsmovie=document.getElementById('FLVPlayer');
webactorsmovie.parentNode.removeChild(webactorsmovie);

//Print function using containers
    function PrintProduct()
    {
	//mark the container as 'divtoprint' to use it
	    var DocumentContainer = document.getElementById('divtoprint');
	    WindowObject.document.writeln(DocumentContainer.innerHTML);
	    WindowObject.document.close();
	    WindowObject.focus();
	    WindowObject.print();
	    WindowObject.close();
    }

//Add bookmark javascript link
<a href="javascript:if(document.all) window.external.AddFavorite(location.href,document.title); else if(window.sidebar)window.sidebar.addPanel (document.title,location.href,'');"> Bookmark this site!</a>

//Pointer change mouseover
onmouseover="document.body.style.cursor='pointer';" onmouseout="document.body.style.cursor='auto';"

//Add event
referenceToElement.onclick = function () { alert('here'); };

//Refresh window
<a href="javascript:location.reload(true)">Refresh this page</a>

//Input box only numbers
<input... onkeyup="javascript:this.value=this.value.replace(/[^0-9]/g, '');" />

Tags: , , , , , ,

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.