Difference between revisions of "Javascript"

From Alessandro's Wiki
 
Line 117: Line 117:
* select all friends / invite all friends
* select all friends / invite all friends
  javascript:fs.select_all();
  javascript:fs.select_all();
== jQuery ==
<syntaxhighlight lang=bash>
$(this) Current HTML element
$("p") All <p> elements
$("p.intro") All <p> elements with class="intro"
$("p#intro") All <p> elements with id="intro"
$("p#intro:first") The first <p> element with id="intro"
$(".intro") All elements with class="intro"
$("#intro") The first element with id="intro"
$("ul li:first") The first <li> element of the first <ul>
$("[href$='.jpg']") All elements with an href attribute that ends with ".jpg"
$("div#intro .head") All elements with class="head" inside a <div> element with id="intro"
</syntaxhighlight >

Revision as of 22:43, 22 January 2012

Image Preload

  • Tratto da i templates di Quanta Plus
/* To include this script into an xhtml page without copying and pasting it in 
add the following tags into your xhtml page. Please note that these comments are 
only valid within .js (JavaScript files), 
do not include them if you wish to use this script within an xhtml document.
<script type="text/javascript" src="./preload.js"></script> 
Or copy and paste the script into your document head enclosed in 
<script type="text/javascript"></script> tags */
var arImages=new Array();
function Preload() {
 var temp = Preload.arguments; 
 for(x=0; x < temp.length; x++) {
  arImages[x]=new Image();
  arImages[x].src=Preload.arguments[x];
 }
}
/*this replaces your normal 'body' tag
substitute your own image names*/
/*
< body onload="Preload('thing.png','anotherthing.png','etc etc.png')" >
*/

AJAX Call

  • Create the object
if (window.XMLHttpRequest ){   var XmlHttp = new XMLHttpRequest();                     }
else {                         var XmlHttp = new ActiveXObject("MSXML2.XMLHTTP.3.0");  }
  • Create the Reequest:
if ( ! XmlHttp ) {
  XmlHttp = new XMLHttpRequest();
}
else {
  XmlHttp.abort();         
}
  • Get the url fetched:
XmlHttp.open("get", PageUrl , true);
XmlHttp.onreadystatechange = function () {
  if (XmlHttp.readyState == 4) {
     if (XmlHttp.status == 200) {
  • if everything goes well, put the result in a div called (with id) "divContent".
         document.GetElementById( "divContent" ).innerHTML = XmlHttp.responseText;
     } else
  }      alert( "error : " + PageUrl );
};
XmlHttp.send( null );

Create element "on the fly"

var but = document.createElement( "INPUT" );
    but.setAttribute( 'type'  ,  'submit' );
    but.setAttribute( 'value' ,  'upload' );

Syntax

Switch

switch( action ) {
 case "stock":
 /* code code code */
 break;
 default:
 /* code code code */
 break;
}

Strings

  • Concatenating:
var BigString = StringFirst + " words between strings " + StringLast ;

popup window

<input type="button" value="open in new window" onclick="window.open('http://www.ciao.com')">


Null

from: http://lists.evolt.org/archive/Week-of-Mon-20050214/169524.html

  "
    Well, first of all, in JavaScript null is an object. There’s another
    value for things that don’t exist, undefined. The DOM returns null for
    almost all cases where it fails to find some structure in the
    document, but in JavaScript itself undefined is the value used.

    Second, no, they are not directly equivalent.
    If you really want to check for null, do:

    if (null == yourvar) // with casting
    if (null === yourvar) // without casting

    If you want to check if a variable exist

    if (typeof yourvar != ‘undefined’) // Any scope
    if (window['varname'] != undefined) // Global scope
    if (window['varname'] != void 0) // Old browsers

    If you know the variable exists but don’t know if there’s any value
    stored in it:

    if (undefined != yourvar)
    if (void 0 != yourvar) // for older browsers

    If you want to know if a member exists independent of whether it has
    been assigned a value or not:

    if (‘membername’ in object) // With inheritance
    if (object.hasOwnProperty(‘membername’)) // Without inheritance

    If you want to to know whether a variable autocasts to true:

    if(variablename)
  "

facebook

  • select all friends / invite all friends
javascript:fs.select_all();


jQuery

$(this)	Current HTML element
$("p")	All <p> elements
$("p.intro")	All <p> elements with class="intro"
$("p#intro")	All <p> elements with id="intro"
$("p#intro:first")	The first <p> element with id="intro"
$(".intro")	All elements with class="intro"
$("#intro")	The first element with id="intro"
$("ul li:first")	The first <li> element of the first <ul>
$("[href$='.jpg']")	All elements with an href attribute that ends with ".jpg"
$("div#intro .head")	All elements with class="head" inside a <div> element with id="intro"