Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Saturday, March 14, 2015

JavaScript Hoisting, What are JavaScript Hoisting?

JavaScript hoisting refers to a default behavior of moving declarations to the top. JavaScript allows a variable declared after it has been used , or in other word we can say we can declare a variable after using it. However, JavaScript only hoists declarations but not initialization. For e.g.: you can declare (var X) but you cannot initialize (var X=3).

Example:
 <!DOCTYPE html>  
 <html>  
 <head lang="en">  
 <meta charset="UTF-8"/>  
 <title>JavaScript Hoisting Tutorials</title>  
 </head>  
 <body>  
 <!-- Declaring Hoisted -->  
 <h2>Example of Hoisting</h2>  
 <p>First Number:<span id="first"></span></p>  
 <p>Second Number:<span id="second"></span></p>  
 <p>Sum of Two Numbers: <span id="sum"></span></p>  
 <p>Example of Hoisting</p>  
 <p>First Number:<span id="a"></span></p>  
 <p>Second Number:<span id="b"></span></p>  
 <p>Sum of Two Numbers: <span id="c"></span></p>  
 <script>  
  //JavaScript Declarations are Hoisted  
  function declare() {  
  //JavaScript Hoisting is the default behavior of moving all declarations to the top of the current  
 scope  
 var a, b, c;//declaration  
  a = 20, b = 15;//assigning value to the variable  
  c=a+b;  
 x=document.getElementById("first"); y=document.getElementById("second"); z=document.getElementById("sum"); x.innerHTML=a; y.innerHTML=b; z.innerHTML=c; //You can also declare variable here. eg,vara,b,c works } function initialize(){ //JavaScript initialize are Hoisted var a = 30, b = 40; var c=a+b; document.getElementById("a").innerHTML=a; document.getElementB(yId"b").innerHTML=b; document

Friday, March 13, 2015

Creating a simple search engine using Javascript

Before we start, you must have a little knowledge on functions in JavaScript and some basic HTML tags. If you are still unknown about these things you may copy the example here but you wont be able to modify it for your purpose of use, so please refer to JavaScript and HTML official web site for details in them and get some time to study functions and basic tags.

For those who already carry information on JavaScript and HTML, here we go. Firstly lets us sketch the skeleton for our search user interface.


search.html
[Note: please fill in other validating tags for a complete HTML file]

 <table width ="200" border="0" cellspacing="0">  
 <tr>  
 <td colspan="2">Sarch </td>  
 </tr>  
 <tr>  
 <td>  
 <input type="text" name="query" size="100">  
 </td>  
 <td>  
 <input type="button" name="Search" value="GO">  
 </td>  
 </tr>  
 </table>  



after we are done with html now we go to JavaScript Section, here we first create a function that does search as it is the vital function for our project.

 //For commencing search  
 function doSearch ( s ) {  
 openDbRelativeURL("All?SearchView&Query=" + s.value);  
 }  

Now as we have done our doSearch function

 function doSearch ( s ) {  
 var regExp1 = /\bfield\b/;  
 var regExp2 = /[(,),<,>,\[,\]]/;  
 var str = s.value; if ( str == "" ){  
 alert("Please be sure to enter something to search for.");  
 s.focus();  
 } else {  
 if ( typeof regExp1.source != 'undefined' ) //supports regular expression testing  
 if ( regExp1.test( str ) || regExp2.test( str ) ){  
 var alrt = "Please note that you can not include:";  
 alrt += "\n\nThe reserved word 'field'\nthe characters [, ], (, ), < or >";  
 alrt += "\n\nin your search query!\n\nIf you are confident that you know";  
 alrt += "\nwhat you are doing, then you can\nmanually produce the URL required."  
 s.focus();  
 return alert( alrt );  
 }  
 openDbRelativeURL("All?SearchView&Query=" + escape( str ) + "&start=1&count=10");  
 }  
 } 

after dosearch function the components that are searched should be displayed by finding it so we create another function  openDBRelative URL

 //For opening the related URL  
 function openDbRelativeURL( url, target ){  
 //Check we have a target window;  
 target = (target == null ) ? window : target;  
 //Work out the path of the database;  
 path = location.pathname.split('.nsf')[0] + '.nsf/';  
 target.location.href = path + url;  
 }


Lastly Paste these codes inside <script> </script> tags inside your html file or include them providing link.

[Note: I haven't checked this example in m local machine or live server.... might work please let me informed if something goes wrong]





Testing Java Script, setting environment for Java Script

While starting to develop a JavaScript or writting codes and programs in JavaScript, a good working environment with atleast basic knowledge about JavaScript is required. JavaScript is written in a code editor/notepad etc. 

It can be written in a normal text editor or you can use software/I.D.E like Dream Weaver, PHP Storm, Eclipse, Net Beans, etc. The editor should have syntax highlighting and auto complete features for making your coding easier and convenient if you are a starter. 


As browser is the most and the developer tools provided by them plays a vital role while working with JavaScript, many popular browsers like Mozilla Firefox, Google Chrome, Internet Explorer, etc. support JavaScript and have developer tools in them but they may differ in every browser. 

Following are the steps to put JavaScript file according as your requirement in document.

Step1

Open the editor on your machine editor can be notepad, Eclips, PHP Strom or whichever IDE you have with you .

Step2

Write the code of JavaScript internally or externally that can be including within HTML Document like as follows within <script> and  </script> tag.
 <script>  
     function changeImage() {  
 varimg = document.getElementById('image');  
       if (img.src.match("logo")) {  
 img.src = "funny.png";  
       } else {  
 img.src = "ft_logo.jpg";  
       }  
     }  
 </script> 

Step3

Save the file in particular directory or drive with appropriate extension.

Step4


Open the saved file with one of your Browser to see the effect of JavaScript. [recommended Google Chrome]



Now while talking about development tools provided by browsers, there are many options that helps debug,test and utilize your coding. We shall follow up those materials in another post.