WebDev Masters

Web Development & Webmaster Resources

Browsing Posts tagged Javascript

There are many JavaScript frameworks available today for programming rich client-side interactions in web applications. With many such different options, it is important to choose a framework that not only serves your current requirement, but also serves you in the long run. We drew up a visual comparison between Ext JS and four other popular JavaScript frameworks: Prototype, Dojo, JQuery and YUI. Our detailed comparison spanned 20 different attributes. In addition to feature-set and technical comparisons, we also looked at parameters such as community support, design principles and quality of documentation and samples to provide a sense of the framework’s durability.

Overall, Ext JS and JQuery came out strong with respect to their support of basic technical features. Both are upcoming frameworks that show commitment towards robustness of design and conceptual modeling that is needed for serious web applications. Ext JS has a rich library of widgets, both of them integrate with other JavaScript libraries in some manner, and both can model complex user interactions in their own way. Ext JS is emerging as an “industry-strength” framework and is being increasingly used in the enterprise. Ext JS also supports a robust client-side data model and support for component model and design patterns.

Your initial choice of JavaScript framework is an important one and the differences between these frameworks can be subtle. Being client-side technologies, these frameworks handle multiple priorities like usability, scripting ease, support for browsers, performance and many others, and making an informed choice about these technologies requires thorough analysis.

 

by Shivesh Vishwanathan

I recently set up a clicksor account, because I wanted to be able to compare google adsense revenue with a competitor.  This was my first time with clicksor.  I submitted the site through the standard review process, and I clearly indicate in the “get my javascript embed code” check list that I did not want any of their in-text features or pop-up features – just the banner. 

 

Unfortunately, as soon as I installed the banner code, my site was also peppered with intext-links.  I did a quick search, and noticed that I wasn’t the only one who came across the problem.  

 

Here’s another example of clicksor gone wrong.  I won’t be using Clicksor as long as their modus operandi is based on tricking the users and maliciously inserting javascript functionality that embeds advertising inlinks I didn’t approve.  It’s probably fraud, but in any case, I just won’t use it and will move on to another option.

 

Another related question is whether these types of inlinks are against google’s quality guidelines – ie, will you get a google penalty if your site has these types of advertisements?  My opinion is that it doesn’t matter what a lawyer will say about the quality guidelines.  What is important is what Matt Cutts says.  And Matt Cutts says that you have to think about what your users want – what provides the best experience for the users?  Whether google’s algorithm or manual penalizers will whack your site for it right away – or whether your readers will just avoid your site and you won’t get links –  doesn’t matter.  Your users want to clearly see and know which links you recommend; they don’t want to have automatic link insertions that confuse them. 

I recently set up a clicksor account, because I wanted to be able to compare google adsense revenue with a competitor.  This was my first time with clicksor.  I submitted the site through the standard review process, and I clearly indicate in the “get my javascript embed code” check list that I did not want any of their in-text features or pop-up features – just the banner. 

Unfortunately, as soon as I installed the banner code, my site was also peppered with intext-links.  I did a quick search, and noticed that I wasn’t the only one who came across the problem.  

Here’s another example of clicksor gone wrong.  I won’t be using Clicksor as long as their modus operandi is based on tricking the users and maliciously inserting javascript functionality that embeds advertising inlinks I didn’t approve.  It’s probably fraud, but in any case, I just won’t use it and will move on to another option.

Another related question is whether these types of inlinks are against google’s quality guidelines – ie, will you get a google penalty if your site has these types of advertisements?  My opinion is that it doesn’t matter what a lawyer will say about the quality guidelines.  What is important is what Matt Cutts says.  And Matt Cutts says that you have to think about what your users want – what provides the best experience for the users?  Whether google’s algorithm or manual penalizers will whack your site for it right away – or whether your readers will just avoid your site and you won’t get links –  doesn’t matter.  Your users want to clearly see and know which links you recommend; they don’t want to have automatic link insertions that confuse them. 

Palma SEO by Drupal SEO is your solution provider for website implementation. I specialize in drupal installation and search engine optimization consultation. Check out SEO Hawaii website for more useful tips.

When you surf the web, you may encounter a very common message displayed to you about a javascript error.  Receiving a javascript error message can be very frustrating, especially when you are simply chatting with friends, ordering a product or surfing the web.  This is a problem that can easily be fixed, without any particular knowledge on the userâ??s part.  A simple registry cleaner should do the trick.

Here is a little information that will help you understand why things like this occur.  When you first got your PC, everything on it is clean, and without congestion.  Over time, the things you do eventually cause your Windows registry to contain broken and corrupt files.  When you download programs or software, they often install over top of files in your registry that you no longer use, but that were never uninstalled.  Data may be misfiled, causing duplicate entries.  Of course, you don’t realize this is happening until you begin having javascript problems.

Another symptom that is common when your registry is corrupt is poor computer performance in general.  Many users notice that their PC becomes slow or sluggish.  It may seem to take much longer than normal for Windows to start up, or to open a link you are trying to visit.  If not repaired, a corrupt registry can lead to hardware failure.  A good product to solve the problem will scan your Windows registry for corrupt and missing files, then repair all errors so that your computer works efficiently once again.

The information contained in your registry is largely responsible for every operation your computer performs.  This is why it is important that you perform regular maintenance with a product designed specifically for that purpose.  All that you do when you are online gets collected, which will cause your PC to become congested and affect its performance.  Even if you know nothing about how a computer operates, all of these annoyances are easily corrected.

When you want your system to run quickly and perform as it once did, get the best registry cleaner to repair javascript errors, leaving you with nothing but a highly-optimized PC.

Optimize-Your-PC will help you, with its advanced registry cleaning technology, it will scan, diagnose and fix all javascript errors and erroneous Windows entries, leaving you with nothing but a highly-optimized PC.

Web page menu header or navigation bars are essential parts of website design. In the past most people use Javascript to create pull-down menus. It would take considerable amount of java scripting for web designer to get the right design and right options on the navigation menu, and incompatibility problems arise often between different web browsers and Javascript versions.

Because of the popularity of Flash, more and more websites are using flash menu to replace javascript menu systems. Flash menus provide better browser compatibility — as flash web menu appears and works the same in all browser and operating system conform to the Adobe Flash standard. Base on Adobe’s website information, over 98% of web users now have flash installed with their browser.

A main attracting factor of your website is the website’s appearance and design. Javascript language does not support high quality graphics or animations. In contrast, flash menu is often visually Rich, and comes with high quality graphics and animated effects, which make using a flash header and navigation menu bar much more attractive than a static Javascript menu. In additional, Javascript is disabled by many users for security reasons and popup blocking. For these users, Javascript menus will simply become invisible and leave a big blank area on your website — which gives your visitors a very poor impression about your website.

Another main advantage of flash menu is its easy maintenance. Most flash menu stores its settings in a single SWF file, changes to site navigation are very easy. Just upload the new file to your site and refresh are instant. If you use Javascript menu, as the javascript code is repeated to all pages, you would need to edit all pages.

To create a flash menu, you can do it in 2 ways. You can either program it yourself using Adobe Flash software plus actionScript — which require techniques such as timeline control, frame labels, use of button event handlers, and animation skills.

To create flash menu the easy way, you can consider to use a flash menu building software. Most flash menu builder software provide a wizard and easy steps to make flash menu, you can simply choose a ready made design from a collection of flash menu templates, and then fill your menu captions, links, and define sub-menus.

A flash menu creator makes it easy for beginners and starters to build a flash menu navigation bar in minutes with just few mouse clicks. Even highly skilled webmasters might find the flash menu software a great time saver for creating flash dropdown menus.

Godfrey Ko, website software and developer since 1996. Developed software including a flash menu builder, website builder, interactive map software and website video player.

You have gone looking for a solution to all those JavaScript error messages you keep getting and you canâ??t seem to make heads or tails of all the information available.  One website says one thing, the others say the problem is related to something else entirely.  Well here are a few simple things to look at when trying to resolve these issues.

First off, the JavaScript programming language is a special type of universal software and works with many different programs.  Though most of the time this program works with online webpages.

If you are having problems with JavaScript error messages only on a few webpages once in awhile, you should start with installing all the current updates on your web browser.  Many web browsers need to be updated periodically to keep up with changes.

To address the problem when you get JavaScript error messages while online and off try to identify changes in the computerâ??s registry.  If you have recently installed or uninstalled programs the process might have inadvertently affected the registry in an unintended manner.

Sometimes when installing and uninstalling software there can be data left behind or broken that the computer processor has trouble understanding.  In many cases the processor will try repeatedly to correct the issue which can lead to an undesired data loop.  These loops eat up processor resources and can harm your computer.

Most people do not realize that the harder a computerâ??s processor works the hotter it gets, but as many people have experienced, a hot processor is one with a short life.  It is much easier to correct many of the issues that lead to a burned out computer processor than it is to regularly purchase a new computer, especially when many of the problems that lead to it can be corrected.

To solve many of these issues all a computer user needs to do is always remember to keep all their programs up to date and regularly run all the programs designed to fix problems.  Those include all of the programs designed to protect, like virus and malware programs.  Users should also frequently use their computerâ??s defragmenting software as well as a good registry cleaning program.

More times than not software errors, including JavaScript error messages, are related to registry problems and outdated software issues.  If you have already updated all the programs that give you a JavaScript error message then your problem is most likely registry related.

With just a little research you should have no problem finding a reliable registry cleaning program that can optimize your PC and fix all your JavaScript error problems.

Optimize-Your-PC can help you, with its advanced registry cleaning technology; it will scan, diagnose and fix all erroneous JavaScript error registry entries, leaving you with nothing but a highly-optimized PC.

Here is a simple tutorial on how to use Javascript to validate a form’s name field. In this tutorial I’ll show how to display the error beside the name field rather than pop-up the error message using the alert function. The error will display stating there is no name in the name field (I use a username field; however, the code can easily apply to a name field) in the error region when the user clicks on the submit button. When the user enters a name in the name field and then resubmits the error is no longer displayed.

Here is the HTML form:

<form name="register" method="POST" action="connect2.php" onsubmit="return checkWholeForm(this)">
<fieldset>
<div id="usernameField"><label style="padding-left:20px;" for="username">username:</label><input type="text" name="username" id="username" size="30" maxlength="45" /><span id="errorMesUsername"></span></div>
<div><label style="padding-left:20px;" for="pass">password:</label><input type="password" name="pass" id="pass" size="30" maxlength="45" /></div>
<label for="email">email:</label><input type="text" name="email" id="email" size="30" maxlength="45" /><br />
</fieldset>
<input type="submit" value="submit" name="submit" />
</form>

Basically, when the user clicks on the submit button the browser is expecting some return value from the checkWholeForm() function. The word ‘this’ is referring to the form as it’s an object. In other words, ‘this object’ is the form. Notice There is a span section beside the username’s input field.

<span id="errorMesUsername"></span>

There is nothing displayed to the username when the form is opened as this is where Javascript will return the error message if there is no name entered in the field.

VALIDATE THE FORM USING JAVASCRIPT FUNCTION

Here is the Javascript function for checkWholeForm():

function checkWholeForm(theForm) { with(theForm) { checkUsername(username.value); } return true; }

Notice the above uses a Javascript with() function. All this is saying is that all properties within the curly brackets {} will use the ‘theForm’ object. In other words, this ‘username’ is the property of ‘theForm’ object. The ‘username’ is the value of the ‘name’ attribute in the input tag as follows:

<input type="text" name="username" id="username" size="30" maxlength="45" />

If we chose not to use the with() function then we could write the checkWholeForm(theForm) as follows;

function checkWholeForm(theForm) {
checkUsername(theForm.username.value);
return true;
}

If we were to create additional validations such as for the password field and use the with() function then we could write the checkWholeForm(theForm) as follows:

function checkWholeForm(theForm) {
with(theForm) {
checkUsername(username.value);
checkPassword(pass.value);
}
return true;
}

The ‘pass.value’ is passing the value of the input of the password field to the checkPassword() function. The ‘pass’ is the value of the ‘name’ attribute of the ‘input’ tag. This function is incomplete as it will eventually be validating the password using PREG (Perl Regular Expressions).

function checkUsername (usernameVal) {
this.errorMes = document.getElementById(“errorMesUsername”);

var error = “”;
if (usernameVal == “”) {
error = “You didn’t enter a username.n”;
this.errorMes[removed] = error;
} else if (this.errorMes[removed] != “”) {
this.errorMes[removed] = “”;
} else {
return true;
}
}

DISPALY ERROR MESSAGE IF NO NAME IS ENTERED

The first line with ‘this.errorMes’ is simply creating a local variable. You could create a variable as ‘var errorMes’ instead. I program primarily in PHP so I’m used to using the term ‘this’ when referring to objects. We are getting the element by the id’s value. In other words, the span tag’s attribute ‘id’ has a value of ‘errorMesUsername’. The span tag as you will notice is immediately after the input tag of the user field. This is where the error will display if the user did not enter any value in the user field. The reference to this DOM will be used later in the conditional statements.

Then in the next line we make the variable ‘error’ equal to nothing in case there is a previous value attached to it. Then we check for some conditions. Since this function (i.e. checkUsername()) is checking to see if the user entered any text the first conditional statement is “if (usernameVal == “”)”. Don’t forget the double equal (i.e.’==’) signs for conditional statements. This is different than assigning a value to variable where you only need one equal (i.e.’=') sign. If it turns out that the user did not enter any text in the username field then we assign the value of variable ‘error’ which equals “You didn’t enter a username.n” to be placed within the span tag (i.e. innerHTML) where the attribute ‘id’ equals ‘errorMesUsername’.

DELETE ERROR MESSAGE IF NAME IS ENTERED

The second conditional “else if (this.errorMes.innerHMTL != “”)” is checking to see if there is any value within the span tag. In other words, if there is any error displayed. We need this conditional when the user then enters a name in the field after reading the error message and then resubmits the form. To further explain, if the condition ‘usernameVal == “”‘ returns false or, in other words, if the there is now some text that was entered by the user in the name field then go to the next conditional which is where we are at. Since there is not some entered text in the name field we need to check to see if there is an error displayed in the span tags and if there is then we need to delete it (i.e. not display it). We do this by assigning the value of the span tag to equal to nothing with this line ‘this.errorMes[removed] = “”‘.

As an extra precaution we could have written this ‘else if’ condition as ‘else if (this.errorMes[removed] != “” && usernameVal != “”)’. The two ampersands are an extra condition that must be met. ‘usernameVal != “” means if the variable ‘usernameVal’ does not equal to nothing or more simply put if the variable ‘usernameVal’ equals to something (i.e. some text was entered in the name field). Double negative equals a positive.

Just as a note, we could have used the Javascript function onchange() instead of using this conditional statement. Onchange() function will check if the user starts typing any text in the name field. As soon as the user types any text then we can return another function value based on what the user starts typing.

The last ‘else’ conditional is simply a catchall in case the other conditions don’t pass for whatever reason.

So the entire Javascript code looks like:

function checkWholeForm(theForm) {

with(theForm) {
checkUsername(username.value);
}

return true;
}

function checkUsername (usernameVal) {
this.errorMes = document.getElementById(“errorMesUsername”);

var error = “”;
if (usernameVal == “”) {
error = “You didn’t enter a username.n”;
this.errorMes[removed] = error;
} else if (this.errorMes[removed] != “”) {
this.errorMes[removed] = “”;
} else {
return true;
}
}

This is a basic example of Javascript validating a name field. We’ll go into more validation checks for the form’s fields in upcoming tutorials.

Javascript Tutorial, tips, guides. Victor Kimura
Javascript Tutorial Javascript Validate Name Field

Many webmasters are confused with the advantages and disadvantages in using Javascript to display their RSS feeds. Javascript can be easily used to display RSS feeds on a website. However, some say it is not the most search engine-friendly method available.

PHP coupled with HTML is the preferred method for displaying RSS feeds on a website when it comes to search engine optimization. This is so because servers use the PHP language to communicate with an HTML website. The search engine does not recognize the PHP, but it recognizes the HTML that PHP initiates and controls via the server.

Search engines are very ancient in regards to recognizing new markup language technology. One could say search engines are lagging so far behind in terms of markup language interpretation that it is surprising that they still manage to continue functioning. They continue to function because HTML is still the main language used by websites.

On the other hand, search engines have made great strides in recognizing other nuances of searching on the internet, such as recognizing when illegal linking schemes are present on websites. So, they are not ancient in certain context, but they definitely are, in the new language recognition department. This brought us back to our discussion about Javascript and RSS feeds.

Javascript is a web markup language that is recognized and read by the user’s browser, but not all the time, depending on the set up of the site. Javascripts can also be controlled by the host server in displaying RSS feeds on a website. In either case, it is still not recognized by search engines—search engines cannot read Javascripts.

Therefore, the only benefit a website that is displaying RSS feeds via Javascripts get is user interactivity statistics, which is a component of search engine analytics. If search engines, recognize that a particular website displaying RSS feeds via Javascript is being frequent by visitors and those visitors are sticking around for long periods, then it will give credits to that site by ranking it higher than it normally would have without the interactivity.

It is much more difficult to implement RSS feeds technology using PHP than it is Javascript, but it is worth the effort. Webmasters would have had to learn the PHP markup language, which isn’t, easy before they would be able to use it to display RSS feeds. There are many RSS feed reader software on the market and there are also many websites that offer free versions.

The PHP to HTML option of displaying RSS feeds is much more search engine friendly. When the server communicates to the website, it uses the PHP language to do the communicating. The website then responds to the signals from the server by using the HTML markup language, which is what the search engine seas and can recognize.

The RSS feed is then displayed among the HTML codes and tags where the search engine spiders picks them up and deliver them to the search engine directory for archiving. So when search terms corresponding to those key words are entered into the search engine search bar, the search engine displays the archived information to the human searcher.

The search engine therefore perceives the RSS content between the HTML codes on a website displaying RSS feeds as belonging to that website. So even though the RSS content or feed is being fed from another site, the search engine thinks that it originates from the website displaying the RSS feed. This is the advantage of using PHP and HTML to display RSS feeds on a website.

In concluding thoughts, using Javascript for displaying RSS feeds does have some good, but it definitely has its share of drawbacks. Using PHP and HTML to display RSS feeds on the other hand, is obviously the better option; however, it is the most difficult.

Ever Wanted to Promote Your Brand Online in Forums, Blogs, Message Boards, Comment Blocks, or Article Directories and Was Never Afforded the Chance or Opportunity to do so By Site Moderators? Well, here is your chance. Mstardom is a social networking community where members submit promo articles in forums and on blogs about their products and services for free. Members can also import their promo YouTube videos about their company and add their music. Glenford S Robinson is a Clinical Lab Scientist, Expert Author, and President of Mstardom.com

What is JavaScript?

JavaScript is a script language – a system of programming codes, created by Netscape, that can be embedded into the HTML of a web page to add functionality. JavaScript should not be confused with the Java programming language. In general, script languages such as JavaScript are easier and faster to code than more structured languages such as Java and C++.

JavaScript is client-based, which means it does not have to make any server requests to run. This makes it ideal for fast response and initial validation of form input.

When combined with CSS, JavaScript can create dynamic effects using Dynamic HTML.

JavaScript is event driven, meaning it acts on events that happen on the screen such as clicking a button.

JavaScript is normally inserted into the HEAD section of the web page surrounded by a <SCRIPT> tag. It is generally called from the HTML in the web page BODY in response to an event, such as a mouse click. JavaScript can also be inserted directly into the BODY section of the web page.

Here are some basic scripts that may come in useful. Modify and experiment with them

A pop up window

<HTML>

<HEAD>

<TITLE>Opening a pop-up window</TITLE>

<SCRIPT LANGUAGE=”JavaScript”>

function popupwin(){

winpopup=window.open(“”,”popup”,”height=300,width=400″);

winpopup.[removed](‘<HTML>\n<HEAD>\n’);

winpopup.[removed](‘<TITLE>This is a popup window</TITLE>’);

winpopup.[removed](‘</HEAD\n<BODY>\n’);

winpopup.[removed](‘<FORM NAME=”otherform”>\n’);

winpopup.[removed](‘<INPUT VALUE=”Close Window”>\n’);

winpopup.[removed](‘</FORM>\n</BODY>\n</HTML>’);

winpopup.document.bgColor=”blue”;

return;

}

</SCRIPT>

</HEAD>

<BODY>

<H1>JavaScript Pop-up Window</H1>

<P>

<FORM NAME=”form”>

Please CLICK here to open a popup:

<INPUT VALUE=”Open a Pop-up window”>

</FORM>

<BODY>

</HTML>

Drop down menu link

<HTML>

<HEAD>

<TITLE>Opening a pop-up window</TITLE>

</HEAD>

<BODY>

<H1>Drop Down Menu Link</H1>

<FORM>

<SELECT NAME=”menu”>

<OPTION VALUE=”http://google.com”>Google</OPTION>

<OPTION VALUE=”http://yahoo.com”>Yahoo</OPTION>

<OPTION VALUE=”http://ca-development.co.uk “>CA-Development</OPTION>

</SELECT>

<input type=”button” value=”Take me there”>

</FORM>

<BODY>

</HTML>

Alert box

<HTML>

<HEAD>

<TITLE>Alert Box</TITLE>

</HEAD>

<BODY>

<SCRIPT LANGUAGE=”JavaScript”>

alert(“Welcome make yourself at home”);

</SCRIPT>

</BODY>

</HTML>

I offer bespoke web design and web application development, aswell as custom software solutions for small business.

I believe many a computer enthusiasts have ever encountered a problem when they tried to visit some websites with Internet Explorer Browser which is JavaScript Error.Internet Explorer would display an error message “JavaScript Error“.If this happens to one or two pages on the same website then it means there are something wrong with this website or this site is broken totally.In fact that is really a common issue because some of the website compiled by programmers contain wrong and uncompatible code so that some computers will alert “There is an error occurred on Java complier”.It is no sense for you to check which line the error occurred on but to programmers. Website programmers can fix the wrong code according to this error message.However if you encounter this problem on any pages that the Javascript interpreter on your computer must be broken.

There are lots of solutions for this error on the Internet such as Debugging Scripts of Internet Explorer,unselecting “display script error” item in the property of Internet Explorer etc. Although those solutions can fix JavaScript Error temporarily,they also will block some normal website information.So they are not the perfect answers actually.

Here are some practical and effective solutions for JavaScript Error:

1.Fix Javascript interpreter of Internet Explorer

Open the registry editor (Start–Run–type in ” regsvr32 jscript.dll ” —Enter. This solution is to let you re-register dll file.)

If you would like to enjoy a simple repairing method, you can go to www.regeasycleaner.com to download the software which contains “register activex” function and it will repair activex error of system.

2.JavaScript Error caused by COMCTL32.DLL

Sometimes the COMCTL32.DLL will be replaced by another file during the process of IE or Windows updating while sometimes there may be a bug in the latest version of COMCTL32.DLL. Get a Windows Xp installation disk and search it to find out a file named COMCTL32.DL_.Copy this file and open it with winrar. Extract this file as COMCTL32.DLL to cover the current one.If you don’t have installation disk please download comctl32.dll from the Internet directly.

3.JavaScript Error caused by wrong DCOM Property Settings.

1. Click Start, click Run, type dcomcnfg, and then click OK.

2. Click the Default Security tab.

3. In the Default Access Permissions box, click Edit Default.

4. Verify that both System and Interactive are listed with Allow Access. If either are missing, add them by    clicking Add.

5. In the List Names From box, make sure that the local computer name is listed.

6. Set Type of Access to Allow Access.

7. Click the account that is missing, and then click Add.

8. Repeat Step 7 if both accounts are missing.

9. Click OK, click OK, and then click OK again.

To fix JavaScript Error, I highly recommend you try to use a great repair tool Registry Easy which is also my personal choice for many years  to maintain and protect your computer perfectly.

You can click here to download Registry Easy to scan your computer for free from now on.

Frankly speaking, JavaScript Error is a usual problem of Internet Explorer.We’d better install Firewall on our computer when we surf Internet in order to protect system settings from attacking,modifying by some illegal websites which would cause problem on browser.

Preloading Images with Javascript

Fast-loading pages reduce errors, conserve bandwidth, and please visitors.  One way to decrease loading times and enhance performance involves maximizing image display efficiency.  Your mantra for achieving image efficiency should be “reuse, optimize, and preload.”  While each of these methods plays an important role, we will focus on methods for preloading images.

What javascript does is allow you to start loading the images in the HEAD section of your page, which means that with this technique (unless the images are large or great in number) your viewers will have the necessary images in their browser’s cache before the script starts to run.  Therefore, an image rollover will be less likely to make the viewers wait for the browser to download the second image, because it is in their browser’s cache.

To get this going, you need to have a small section of script in the HEAD section of your page. Here is a sample of a script that preloads a single image:

<script type=”text/javascript”>
Image01 = new Image(“width”,”height”);
Image01.src = “http://www.example.com/image01.png”;
</script>

First let me explain what is going on. In the first line we are declaring a new variable “Image01” and telling Javascript that it is of type Image with a width=width and a height=height.  A quick note about the height and width, in place of “width” and “height” you would enter the dimensions of your image in pixels minus the px and quotation marks that I have used.  So if your image is 225×40 pixels, 225px wide and 40px tall then, your value for the width is 225 and the value for height is 40:

//Image width=225px and height=40px
Image01 = new Image(225,40);

The second defines the URL or web address of the image. You would replace this with the URL of your image. Not too bad now, is it?

A simple and effective method to get you started on your preloading campaign.  However with anything simple it is not too effective.  There is a chance your viewer will come through with an older browser which doesn’t support the image object.  So, to be on the safe side you may wish to implement a form of browser or object detection to keep from creating a javascript error on older browser.  Object and browser detection are outside the scope of this article but I will provide a quick demonstration I use.

I will utilize object detection as it is quick and requires far less explanation and code.  We want to know if the image object exists, which is conveniently known in javascript as “document.images.”  We simply need to check whether or not this object exists using a simple if statement, if the objects exists then we run our preloader, if it does not we do nothing and let the webpage load as it would.  We take our previous code and make one simple tweak:

<script type=”text/javascript”>
If (document.images) {
    Image01.src = “http://www.example.com/image01.png”;
}
</script>

All we are doing is stating if document.images exists then we want to perform the instructions in between these brackets { }.  If it does not then we do nothing.  Here we have it, a simple and effective preloader with the ability to check for Javascript compatibility.

Our next issues is what if we have multiple images that we wish to preload.  We could go about this in two manners.  The first is simply taking what we have already done and repeating it as so:

<script type=”text/javascript”>
If (document.images) {
//Image 1
Image01 = new Image(“width”,”height”);
Image01.src = “http://www.example.com/image01.png”;
//Image 2
Image02 = new Image(“width”,”height”);
Image02.src = “http://www.example.com/image02.png”;
//Image 3
Image03 = new Image(“width”,”height”);
Image03.src = “http://www.example.com/image03.png”;
}
</script>

It works but is not ideal for my needs and it becomes rather tedious especially when dealing with a large number of images.  Therefore, we need to find a simple and effective method that lends itself to being reusable and scalable.

<script type=”text/javascript”>
If (document.images) {
//variable counter starting point
i = 0;
//create image object
imageObj = new Image;
//create new image array
image = new Array ();
    image[0] = “image01.png”;
    image[1] = “image02.png”;
    image[2] = “image03.png”;
    image[3] = “image04.png”;
    image[4] = “image05.png”;
    image[5] = “image06.png”;
    image[6] = “image07.png”;
    image[7] = “image08.png”;
//Our preloader
//”i” must be <= to the number of images we wish to load
for (i=0; i <= 8; i++)

    {
    imageObj.src = image[i];
    }
}
</script>

In our example we added a few new elements.  Firstly we created a variable “i” that is our counter and set it value to zero.  Next, we created a new object “imageObj” that is a place holder for images we wish to load.  We created an array “image” to hold the name of our images we wish to preload.  Finally we added a counter that cycles through our array and loads the images.

The code is fully scalable, we can use for one image or one hundred images.  The only things we have to add or change are the elements in the array and the number “i” is less than or equal to.  There you have it, we have our Javascript preloader.

Harold Pettegrove
NorthRockSEO

http://www.northrockseo.com

http://blog.northrockseo.com/article/preloading-images-with-javascript