Javascript

From DreamsteepWiki

Jump to: navigation, search

Namespace /object

var klmt={};

klmt.awesome = function(){};

klmt.awesome.foo = function(arg){alert('hello '+arg+ '!');};


klmt.awesome.foo('keith');
var yourNamespace = {

    foo: function() {
    },

    bar: function() {
    }
};

function Apple(){

  this.color = 'red';
}


yourNamespace.foo.prototype = new Apple();

hybrid = new yourNamespace.foo();

alert(hybrid.color);


var yourNamespace = {

    foo: function() {
    },

    bar: function() {
    }
};



yourNamespace.foo();

RECURSION

 function factorial (n)
{
    if(n==0) return(1);
    return (n * factorial (n-1) );
}


string =(factorial(5));

JS OBJECTS



function node (name) {
    this.name = name;
    this.color = "red";
    return this;
}

//

function addf(object){
     this.nodes.push(object);
}
//

function graph(){
 //property that stores the radius
 this.nodes = new Array();
 this.add =addf ;
 return this;
}


gr = graph();
nod = node('bent');

gr.add(nod);
gotnod = (gr.nodes[0]);
alert(gotnod.name);


keyboard shortcut

document.onkeyup = function keyPress(event)
{         var wkey=0
          if(document.all) wkey=window.event.keyCode;
          if(document.layers) wkey=event.which;
          if(wkey==13)dg.add('');
          alert(wkey);
}



returning a php databse query part 1 :The javascript

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Jasper Test Map Oct 6, 2009 </title>
    </head>

    <body>
        <script type="text/javascript">
        
        var oRequest = new XMLHttpRequest();
        var sURL  = "http://127.0.0.1/open/examples/post/test2.php";
        oRequest.open("GET",sURL,false);
        oRequest.setRequestHeader("User-Agent",navigator.userAgent);
        oRequest.send(null)
        if (oRequest.status==200) alert(oRequest.responseText);
        else alert("Error executing XMLHttpRequest call!");

     </script>
     
    </body>

</html>


returning a php databse query part 2 :The php script

<html>
  <head>
  </head>

  <body>
<?php

  $host = '127.0.0.1';
  $username = 'postgres';
  $password = 'password';
  $db = 'bentonpg';



    $dbconn = pg_connect("host=$host dbname=$db user=$username password=$password")
    or die('Could not connect: ' . pg_last_error());

    $query  = "SELECT * FROM characters" ;
    $search_db= pg_query($query);
    $results= pg_num_rows($search_db);

    if ($results < 1) {
      echo '<div style="text-align: center; width: 100%; font-weight: bold;">No Records Found</div>';
    } else {
         //THE QUERY STRING
        echo '<table  border="1">' ;
        echo '<tr>' ;
        echo '<td id="results_table">' ;
            print $query;
        echo '</td>' ;
        echo '</tr>' ;

       //THE RESULTS
      while ($row = pg_fetch_array($search_db)) {
        echo '<tr>' ;
        echo '<td id="results_table_td">' ;
        echo $row[1];
        echo '</td>' ;
        echo '</tr>' ;
      }
        echo '</table>';
    }

    pg_close($dbconn);




?>
  </body>
</html>


TYPECASTING

var n = 30; // number 
var m = 70; // number
var s = new String();// "3070"
s = String(n) + String(m);
// "100"
s = Number(n) + Number(m);


TYPEOF

var BooleanValue = true;
var NumericalValue = 354;
var StringValue = "This is a String";
alert(typeof BooleanValue) // displays "boolean"
alert(typeof NumericalValue) // displays "number"
alert(typeof StringValue) // displays "string"


ARRAYS


push

pop

sort

length

joint

concat

reverse

tostring





///////////////

     var fruits = ["b", "o", "a", "m"];
     //o,a,m      //  fruits.shift();
     //k,o,a,m    //  fruits.unshift('k');
     //B,o, a     //  fruits.pop();
     //B,o, a ,e  //  fruits.push('e');

     //add two arrays 
     //var veg = ["reeb", "salad", "litter"];
     //var test = fruits.concat( veg ); //, ..., arrayX

     //slicing 
     //o     //tmp= fruits.slice(1,2)  ;
     //o,a,m //tmp= fruits.slice(1)  ;

     alert(tmp);

   ///////////////



ARRAYS, FOR-IN LOOP

      //////////
      var newar=new Array()

      //this spits out an INDEX (numeric) for var test
      for (var test in response)
      {
        newar[test] = test ;
      }
      alert(newar);




MAKING SCRIPT LINKS COMPATIBLE WITH INTERNET EXPOLODER

    <SCRIPT language='JavaScript' type='text/javascript' SRC='//127.0.0.1/open/examples/jasper_open/cress.js'></SCRIPT>


Open a link in a browser

                   myRef = window.open('http://perihelionvfx.com','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');

Simple example


<html>
<body>
<script type="text/javascript">
document.write("HELLO KITTY!");
</script>
</body>
</html> 


Example with tags


<html>
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>");
</script>
</body>
</html>


TIME IF STATEMENTS

<html>
<body>

<script type="text/javascript">
var d = new Date();
var time = d.getHours();

if (time < 10)
  {
  document.write("<b>Good breakfast!</b>");
  }
  

if (time > 10)
  {
  document.write("<b>WAKE UP!!</b>");
  }
  
</script>

<p>PTAH!.</p>

</body>
</html>





BUTTON, FUNCTIONS

<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!");
}
</script>
</head>

<body>
<form>
<input type="button" value="Click me!" onclick="displaymessage()" />
</form>

<p>By pressing the button above, a function will be called. The function will alert a message.</p>

</body>
</html>


MATH FUNCTIONS

<html>

<script>
 var pi_value=Math.PI; var sqrt_value=Math.sqrt(16);
 document.write(sqrt_value)
</script>
</html>

FUNCTIONS ITERATORS AND FLOW CONTROL

function displaymessagee()
{

var numbers = [1, 2, 3, 4, 5];
var incrementer = 0;
while (incrementer < numbers.length)
{
numbers[incrementer] *= 2;
document.write(incrementer)
incrementer++;
 if (incrementer == numbers.length)
 {
  document.write("eeeeeeeeeeee")
 }
 
}


}

Alerts

var doubleEscape = "She said \"hide\" in a loud voice.";
alert(doubleEscape);


ARRAYS

var myCars=new Array("Saab","Volvo","BMW")


MORE ARRAYS

var shortArray = ["First", "Second", "Third"];
var total = shortArray.length;





DOCUMENT

    * alinkColor - The color of active links.
    * bgColor - Sets the background color of the web page. It is set in the <body> tag. The following code sets the background color to white.

      document.bgColor = "#FFFFFF"
    * cookie - Used to identify the value of a cookie.
    * defaultCharset
    * domain - The domain name of the document server.
    * embeds - An array containing all the plugins in a document.
    * fgColor - The text color attribute set in the <body> tag.
    * FileCreatedDate - Use this value to show when the loaded HTML file was created
    * fileModifiedDate - Use this value to show the last change date of the loaded HTML file
    * fileSize
    * fileUpdatedDate
    * lastModified - The date the file was modified last.
    * layers - An array containing all the layers in a document.
    * linkColor - The color of HTML links in the document. It is specified in the <body> tag.
    * location
    * mimeType
    * nameProp
    * protocol
    * readyState
    * referrer - The Universal Resource Locator (URL) of the document that we got the link to the present document from.
    * security
    * title - The name of the current document as described between the header TITLE tags.
    * URL - The location of the current document.
    * vlinkColor - The color of visited links as specified in the <body> tag/


onabort  	    Loading of an image is interrupted  	
onblur 	      An element loses focus 	
onchange 	    The user changes the content of a field 	
onclick 	    Mouse clicks an object 	
ondblclick 	  Mouse double-clicks an object 	
onerror 	    An error occurs when loading a document or an image 	
onfocus 	    An element gets focus 	
onkeydown 	  A keyboard key is pressed 	
onkeypress 	  A keyboard key is pressed or held down 	
onkeyup 	    A keyboard key is released 	
onload 	      A page or an image is finished loading
onmousedown 	A mouse button is pressed
onmousemove 	The mouse is moved 	
onmouseout 	  The mouse is moved off an element 	
onmouseover 	The mouse is moved over an element
onmouseup 	  A mouse button is released 	
onreset 	    The reset button is clicked 	
onresize 	    A window or frame is resized
onselect 	    Text is selected
onsubmit 	    The submit button is clicked
onunload 	    The user exits the page

LINKS

http://www.w3schools.com/JS/js_examples.asp

http://javascript.internet.com/passwords/advanced-password-checker.html#source

Personal tools