This is a mirror of official site: http://jasper-net.blogspot.com/

Interesting tips in JavaScript

| Tuesday, October 26, 2010
Describes about 5 interesting tips in JavaScript


In this article I would like to share some of the interesting things I came to know on JavaScript.

These are the five tips we will see..  

         #1  Dynamic script loading

         #2  Instantiation without "new"  

         #3  Memoizing  

         #4  Mixin classes

         #5  Inheritance by cloning  

#1 Dynamic script loading  

Most of the times, we load all the scripts and other resources initially when the page renders.  Loading all the scripts initially increases the HTTP requests to the server also considerably reduces the time taken to see the page by user.

As an example, when working with Google Maps usually we load the Google Maps API initially and show the map. What will be the case if you want to show the map only the user needs it. Here is the script that does that job!  

var api = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";

//The function that loads any external JavaScript dynamically
function loadScript(src) {
 var script = document.createElement("script");
 script.type = "text/javascript";
 script.src = src;
 document.body.appendChild(script);
}


//Google maps api callback
function initialize() {
 var myLatlng = new google.maps.LatLng(-34.397, 150.644);
 var myOptions = {
   zoom: 8,
   center: myLatlng,
   mapTypeId: google.maps.MapTypeId.ROADMAP
 }
 var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

<div id="map_canvas" style="width:500px; height:500px"></div>
<input type="button" value="Load Map" onclick="loadScript(api)" />  

Read more: Codeproject

Posted via email from .NET Info

0 comments: