You Are Here Home > JavaScript

JavaScript

Google Maps API V3; infowindow is only attached to the last marker problem

You can fix this issue like this:

for (var i = 0; i < addresses.length; i++) {
			(function () { /* ################################################## */
				var address = addresses[i];
				var lat_lang = new google.maps.LatLng(address.latitude, address.longitude);
				var marker = new google.maps.Marker({
					position: lat_lang,
					map: map
				});
				markers[markers.length] = marker;
				var infowindow = new google.maps.InfoWindow({
					content: address.address
				});
				infowindows[infowindows.length] = infowindow;
				google.maps.event.addListener(marker, 'click', function() {
					close_infowindows();
					infowindow.open(map, marker);
				});
			})(); /* ################################################## */
		}

By adding the lines marked with /* ################################################## */ in the middle of your loop…

I hope this helps someone :)

Google Maps API V3; infowindow is only attached to the last marker problem
Comments (1)   Filed under: APIs, Google Maps API, JavaScript, Web Development   Posted by: Codehead

jQuery: How To Check If An Object Exists

   if ($('#myDiv').length)
      $('#myDiv').show();

Source:
http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_test_whether_an_element_exists.3F

jQuery: How To Check If An Object Exists
Comments (0)   Filed under: JavaScript, Web Design, Web Development, jQuery   Posted by: Codehead

jQuery: Scroll Window To The Top Of An Object

Here is how to do this:

var offset = $('#OBJECT-ID').offset().top;
$('html,body').animate({
   scrollTop: offset
}, 500);
jQuery: Scroll Window To The Top Of An Object
Comments (0)   Filed under: JavaScript, Web Design, Web Development, jQuery   Posted by: Codehead

The Best Overall Code Editor Ever: Komodo Edit

A while back I wrote a post about what I think is the best Python code editor but I decided to write an update and write a little more about Komodo Edit…

I use Dreamweaver to write HTML/CSS/JavaScript/PHP code and a few days ago I decided to open Komodo Edit and see how it does with PHP.

I did try other editors for PHP but their support for HTML and CSS was lame and I’m so used to Dreamweaver’s comprehensive code completion features.

Well, I was surprised that Komodo Edit has all those features (& more) and handles PHP/CSS/HTML/JavaScript beatuifully, open a new PHP file and after ?> type:

<a

And then hit space, it will suggest everything Dreamweaver suggests and more! then write:

style=”

And again it will suggest every CSS property, then type:

:

It will suggest all the possible values, more than Dreamweaver…

The other thing is that it will show you function descriptions for all the functions and that could be very handy.

I have to say that I don’t know if I’m going to pay for Dreamweaver ever again and if you are looking for a free and excelent code editor, try Komodo Edit.

I have no affiliation with ActiveState and what I said above is my personal thoughts and ideas and an attempt to show this excellent/free/open-source product to others.

Happy Coding.

The Best Overall Code Editor Ever: Komodo Edit
Comments (1)   Filed under: CSS, HTML/XHTML, JavaScript, Komodo Edit, PHP, Web Design, Web Development   Posted by: Codehead

A Way To Send A Message To Apple About Flash

Here is what can be done, someone could write a piece of JS code and make a big Flash banner, big I mean very big and then the JavaScript could check for iPad and iPhone and try to show the banner to them…

You can then urge web developers and web masters to put it on their websites and blogs so that every iPhone or iPad user would see a big giant empty box with a message like “Your browser doesn’t support …”.

A Way To Send A Message To Apple About Flash

jQuery UI Dialog And The Enter – Return Key Problem

This is another post for my ‘Annoying Stuff’ collection and this one is very, so very annoying…

The problem is that jQuery UI, supports forms in dialogs but the problem is that a user can’t hit ‘Enter’ to submit the form, it will break everything, a user has to actually hit the ‘Submit’ (or whatever) button manually. This make the whole thing completely useless unless you make some changes that are basically tweaking the internals of jQuery UI, which is ugly and can break if they change things around but sadly this is the only solution for now.

Assuming that you use the same syntax jQuery UI suggests to create your form, the fix is something like this:

$('.dialog').find('input').keypress(function(e) {
	if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
		$(this).parent().parent().parent().parent().find('.ui-dialog-buttonpane').find('button:first').click(); /* Assuming the first one is the action button */
		return false;
	}
});

You might have to modify it a tiny bit, if that’s the case, you most likely have to change the part $(‘.dialog’) so that it selects the right container that wraps the form…

jQuery UI Dialog And The Enter – Return Key Problem
Comments (0)   Filed under: Annoying Stuff, JavaScript, Programming, Web Design, Web Development, jQuery   Posted by: Codehead

document.getElementById On All Browsers – Cross browser getElementById

Here is a little JavaScript function that gets an object according to it’s id and it’s cross browser.

/*********************************************************************
   * Get an object, this function is cross browser
   * Usage:
   * var object = get_object(element_id);
   * @Author Hamid Alipour http://blog.code-head.com/
  **/
  function get_object(id) {
   var object = null;
   if (document.layers) {
    object = document.layers[id];
   } else if (document.all) {
    object = document.all[id];
   } else if (document.getElementById) {
    object = document.getElementById(id);
   }
   return object;
  }
  /*********************************************************************/
document.getElementById On All Browsers – Cross browser getElementById
Comments (0)   Filed under: JavaScript, Web Browsers, Web Development   Posted by: Codehead

onMouseOut fix on nested elements – JavaScript

When you have nested elements and you add an onMouseOut event handler to the parent element, browsers trigger onMouseOut event when mouse pointer hovers it’s child elements.
While this is a standard behaviour, for one project I needed to write a code to override this behaviour.
With this code, when you mouse over the child elements, onMouseOut event will be ignored.

You can download this code here:
http://images.code-head.com/code/javascript/fixOnMouseOut.zip

You can test it here:
http://images.code-head.com/code/javascript/fixOnMouseOuttest.html

This code is cross browser and here is how to use it:

<script language="javascript" type="text/javascript" src="fixOnMouseOut.js"> </script>
<div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');">
   So many child elements
</div>
onMouseOut fix on nested elements – JavaScript
Comments (7)   Filed under: JavaScript, Web Design, Web Development   Posted by: Codehead

AJAX – Beginner AJAX Tutorial – Creating a simple AJAX website with example

We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…

Beginner AJAX Tutorial – Creating a simple AJAX website with example

What you need to know before you read this tutorial:

You need to read the previous two tutorials, they are located at here:
Beginner Ajax Tutorial
Beginner AJAX Tutorial – Display a Progress Bar or a Loading Message

(It will only takes a few minutes and will catch you up to this point.)

We are going to make a simple AJAX website with 4 pages.

A few things to note:

1. We are going to make a website that works just fine for users without JavaScript.
2. The back-end will be PHP, it might not be the best way to implement but it’s simple and effective for the purpose of this tutorial.
3. You will be able to download the complete source code for this AJAX tutorial.
4. In this AJAX tutorial the browser back button is ‘broken’, and users can’t bookmark internal pages of this website; fixing these issue is the topic of the next tutorial.
5. In this tutorial I’m going to demonstrate what can be done with AJAX in a simple way using inline JavaScript; separating presentation and behavior will be the topic of later tutorials.
Inline JavaScript looks like this:

<a href="?page=about" onClick="return get_page('about');">About us</a>

Let’s start with the back-end

Here is the directory structure of this website:

/index.php
/templates/main.php
/templates/home.php
/templates/about.php
/templates/services.php
/templates/contact.php
/images/few images
/yui/our yui code libraries

Here is the index.php source code:

<?php
 
	$templates_folder = 'templates/';
 
	if( !isset($_GET['page']) ) {
		$_GET['page'] = 'home';
	}
 
	/**
	 * Because we are going to get the content of a file based on user input and display it
	 * to the user, we use basename function to make sure a sneaky user doesn't try
	 * something like ../../ to get a password file or something important.
	**/
	$page = trim(basename($_GET['page'])) .'.php';
 
	$page_file = $templates_folder .$page;
 
	/**
	 * Note: This assumes your templates are static, that is: there is no PHP code
	 * in them, I will write about how to handle that situation later but for now,
	 * let's keep it simple :)
	**/
	if( file_exists($page_file) ) {
		$page_content = file_get_contents($page_file);
	} else {
		$page_content = 'This page doesn\'t exist here.';
	}
 
	/**
	 * If the $_GET['AJAX'] is set, then the request is coming from our JavaScript
	 * get_page() function, so we just send the $page_content to the browser.
	**/
	if( isset($_GET['AJAX']) ) {
		echo $page_content;
	} else {
		include $templates_folder .'main.php';
	}
 
?>

What it does is straightforward and you can understand the code just by reading it.
But here is how it works:

1. It checks the contents of the $_GET['page'] variable for the name of the page a visitors wants to visit and assigns it to $page.

$page = trim(basename($_GET['page'])) .'.php';
 
$page_file = $templates_folder .$page;

$page_file now contains the exact path to our template.

Also note that, this part of the code:

if( !isset($_GET['page']) ) {
	$_GET['page'] = 'home';
}

Makes sure that the home page is the default page, and if $_GET['page'] is empty it assigns ‘home’ to it.

2. It checks if the page exists in the templates folder.

if( file_exists($page_file) ) {
	$page_content = file_get_contents($page_file);
} else {
	$page_content = 'This page doesn\'t exist here.';
}

If it exists then it assigns it’s content to $page_content variable if not an error message that this page does not exist.

3. It checks the content of $_GET['AJAX'] variable and if its set, the request is coming from our JavaScript code:

if( isset($_GET['AJAX']) ) {
	echo $page_content;
} else {
	include $templates_folder .'main.php';
}

If it is coming from our JavaScript code then it only prints out the content of the page, but if not, it prints the main.php template.

Main.php source code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Sample AJAX-XHR Website</title>
</head>
 
<body>
 
		<?php echo $page_content; ?>
 
</body>
</html>

This is the main.php in it’s simplest format, I omitted the JavaScript.
What it does is just printing the $page_content variable in the page.

Other templates:

They only contain plain HTML code, you will see everything together soon.

The front end

All the JavaScript code goes into main.php, here is the JavaScript code we had so far in the previous tutorial:

	<script type="text/javascript" src="yahoo.js"> </script>
	<script type="text/javascript" src="event.js"> </script>
	<script  type="text/javascript" src="connection.js"> </script>
 
	<script type="text/javascript">
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
 
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}
 
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
 
		function show_progressbar(id) {
			replace_html(id, '<img src="http://images.code-head.com/progress-bars/4.gif" border="0" alt="Loading, please wait..." />');
		}
 
		function send_request() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
 
		function test_failure() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
 
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
	</script>

The code we use for this tutorial is similar, we will only replace send_request() and test_failure() with one function, get_page():

	<script type="text/javascript" src="yui/yahoo.js"> </script>
	<script type="text/javascript" src="yui/event.js"> </script>
	<script type="text/javascript" src="yui/connection.js"> </script>
 
	<script type="text/javascript">
 
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
 
		function show_progressbar(id) {
			replace_html(id, '<img src="http://images.code-head.com/progress-bars/4.gif" border="0" alt="Loading, please wait..." />');
		}
 
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
 
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death.');
		}
 
		function get_page(page) {
			if( page == '' ) {
				/* Do nothing if 'page' is empty, you might want to show some error message. */
				return false;
			}
			show_progressbar('content'); /* Show progress bar while waiting */
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'index.php?AJAX=true&page=' + page, callback);
			return false;
			/**
			 * return false!
			 * Because this will prevent the browser from navigating to page specified in
			 * the link's href attribute.
			**/
		}
 
		/* Let's preload our progress bar */
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
 
	</script>

get_page() function is very simple, here is what it does:

1. It receives the page name as an argument.

function get_page(page) {
	// ...
}

2. It check if the page variable is empty.

if( page == '' ) {
	return false;
}

Here, this function does nothing if page variable is empty, you might want to show an error message.

3. It shows the progress bar before sending the request to the server, letting the user know that something is actually happening:

show_progressbar('content');

4. It sends the request to server and sets our success_handler and failure_handler as callbacks:

var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
YAHOO.util.Connect.asyncRequest('GET', 'index.php?AJAX=true&page=' + page, callback);

Note: We are sending a request to:

'index.php?AJAX=true&page=' + page

We set the AJAX variable to true and we set the page variable to the page variable that passed to the function. (It can be ‘about’, ’services’, etc.)

5. It returns false. Return false will result in any link that uses this function to appear as not working, that is, the browser won’t try to navigate
to the page specified in the links href attribute. Instead of letting the browser navigate to that page we replace the content of the page with JavaScript.

main.php with the JavaScript and a menu:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>Sample AJAX-XHR Website</title>
 
	<!-- Usual yui stuff -->
	<script type="text/javascript" src="yui/yahoo.js"> </script>
	<script type="text/javascript" src="yui/event.js"> </script>
	<script type="text/javascript" src="yui/connection.js"> </script>
 
	<script type="text/javascript">
 
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
 
		function show_progressbar(id) {
			replace_html(id, '<img src="http://images.code-head.com/progress-bars/4.gif" border="0" alt="Loading, please wait..." />');
		}
 
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
 
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death.');
		}
 
		function get_page(page) {
			if( page == '' ) {
				/* Do nothing if 'page' is empty, you might want to show some error message. */
				return false;
			}
			show_progressbar('content'); /* Show progress bar while waiting */
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'index.php?AJAX=true&page=' + page, callback);
			return false;
			/**
			 * return false?!
			 * Because this will prevent the browser to navigate to page specified in
			 * the link's href attribute.
			**/
		}
 
		/* Let's preload our progress bar */
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
 
	</script>
 
</head>
 
<body>
<div id="main-wrapper">
<div id="main-padding">
 
	<h1>Sample AJAX-XHR Website</h1>
 
	<div id="menu">
		<a href="?page=about" onClick="return get_page('about');">About us</a>
		&nbsp; &nbsp;
		<a href="?page=services" onClick="return get_page('services');">Our services</a>
		&nbsp; &nbsp;
		<a href="?page=contact" onClick="return get_page('contact');">Contact us</a>
		&nbsp; &nbsp;
		<a href="?page=home" onClick="return get_page('home');">Home</a>
	</div>
 
	<div id="content">
 
		<?php echo $page_content; ?>
 
	</div>
 
</div>
</div>
</body>
</html>

2 notes:
1. Menu links are like:

<a href="?page=about" onClick="return get_page('about');">About us</a>

Their href attribute is actually pointing to the right page, so users without JavaScript will be able to navigate through these pages, and search engines will have
no problem indexing these pages as well.

2. On the other hand their onClick event is set to call get_page() function and passes the page name as the argument to the get_page().

Here is how it works all together:
http://images.code-head.com/tutorials/ajax/sample-AJAX-XHR-website/

Please note: Ignore the look of this example, the point is the functionality.
You can try turning off your JavaScript and see that the example site still works just fine without it.

Download the complete source code here if you want to take a closer look at everything:
http://images.code-head.com/tutorials/ajax/sample-AJAX-XHR-website/sample-AJAX-XHR-website.zip

If you have questions or suggestions, feel free to reply to this thread.
-Codehead

Update:

I should mention that it’s a long time that I moved on to use jQuery rather than yui and I think jQuery is much nicer and easier to use library. I will hopefully write some tutrials on jQuery.

If you don’t know jQuery yet, download it here and HAVE FUN FOREVER!
Download jQuery

AJAX – Beginner AJAX Tutorial – Creating a simple AJAX website with example
Comments (5)   Filed under: AJAX, JavaScript, PHP, Web Development, jQuery, yui   Posted by: Codehead

AJAX – Beginner AJAX Tutorial – Display a Progress Bar or a Loading Message

We are closing down our forums, it’s time to move on, but we are keeping some important threads, here are the AJAX tutorials…

AJAX – Beginner AJAX Tutorial – Display a Progress Bar or a Loading Message

In the last AJAX tutorial I wrote about how to send requests to the server and receive the response,
in this short AJAX tutorial I will show you how to show a progress bar to the user when your application is
waiting for the server’s response, so your users will see something is actually happening :)

If you haven’t read the first AJAX tutorial, you’ll find it here:
http://blog.code-head.com/ajax-beginner-ajax-tutorial
It won’t take that long.

Here is the code that we have so far:

<html>
<head>
<title>Beginner AJAX Tutorial - Progress Bar</title>
 
	<script type="text/javascript" src="yahoo.js"> </script>
	<script type="text/javascript" src="event.js"> </script>
	<script type="text/javascript" src="connection.js"> </script>
 
	<script type="text/javascript">
		function success_handler(o) {
			document.getElementById('content').innerHTML = o.responseText;
		}
 
		function failure_handler(o) {
			document.getElementById('content').innerHTML = 'Server or your connection is death';
		}
 
		function send_request() {
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
 
		function test_failure() {
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
	</script>
 
</head>
 
<body>
<a href="javascript:send_request();">Send Request</a> | <a href="javascript:test_failure();">Fail a request</a>
<div id="content">
 
</div>
</body>
</html>

To show a loading progress bar you need to get a progress bar image, you can get one by searching.

I will use this one:
AJAX progress Bar

Let’s begin

First let’s make our code a little bit nicer and make a new function called, replace_html

		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}

The way it works is that it receives two arguments:
‘id’ is the ID of the container where you want to replace the content
‘content’ is the new content to replace the old content :)

Now let’s rewrite our success_handler and failure_handler to use this function:

		function success_handler(o) {
			replace_html('content', o.responseText);
		}
 
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}

In success_handler for example, we call replace_html(‘content’, o.responseText); instead of directly
replacing the content of our ‘content’ DIV like: document.getElementById(‘content’).innerHTML = o.responseText;

Why?

First, because we don’t need to write document.getElementById(‘content’).innerHTML = … all over the place and
we encapsulate this into replace_html function.

Second this will centralize our way of changing the content of a DIV, so if later for example we
find a better way to replace a DIV’s content, then we will just change one function and we don’t need
to go through and find all the lines like: document.getElementById(‘content’).innerHTML = … and
change them.

Third, suppose you find out that on one kind of browser innerHTML doesn’t work, then again you
only need to change one function.

So far, all together we have this:

<html>
<head>
<title>Beginner AJAX Tutorial - Progress Bar</title>
 
	<script type="text/javascript" src="yahoo.js"> </script>
	<script type="text/javascript" src="event.js"> </script>
	<script type="text/javascript" src="connection.js"> </script>
 
	<script type="text/javascript">
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
 
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}
 
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
 
		function send_request() {
			var callback = { success:success_handler, failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
 
		function test_failure() {
			var callback = { success:success_handler, failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
	</script>
 
</head>
 
<body>
<a href="javascript:send_request();">Send Request</a> | <a href="javascript:test_failure();">Fail a request</a>
<div id="content">
 
</div>
</body>
</html>

For our progress bar to show up fast, we have to preload it first, here is a simple way of preloading our progress bar:

		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';

This piece of JavaScript code will make an image instance and sets it’s source attribute to our progress bar, this is enough
to force the browser to preload the progress bar in it’s cache.

To display this progress bar when we are waiting for the server’s response, let’s write another function show_progressbar

		function show_progressbar(id) {
			replace_html(id, '<img src="http://images.code-head.com/progress-bars/4.gif" border="0" alt="Loading, please wait..." />');
		}

This function receives the ID of the container which we want to show our progress bar in and uses our replace_html
function to display the progress bar.
It simply replaces the content of that particular container with this bit of HTML code:

	<img src="http://images.code-head.com/progress-bars/4.gif" border="0" alt="Loading, please wait..." />

Now we will have to change our send_request and test_failure to use this function and show a progress bar when they are
starting to send a request:

		function send_request() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
 
		function test_failure() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}

They simply call show_progressbar with the ID of our content DIV and then send their request to the server.

All together we have this code:

<html>
<head>
<title>Beginner AJAX Tutorial - Progress Bar</title>
 
	<script type="text/javascript" src="yahoo.js"> </script>
	<script type="text/javascript" src="event.js"> </script>
	<script type="text/javascript" src="connection.js"> </script>
 
	<script type="text/javascript">
		function success_handler(o) {
			replace_html('content', o.responseText);
		}
 
		function failure_handler(o) {
			replace_html('content', 'Server or your connection is death');
		}
 
		function replace_html(id, content) {
			document.getElementById(id).innerHTML = content;
		}
 
		function show_progressbar(id) {
			replace_html(id, '<img src="http://images.code-head.com/progress-bars/4.gif" border="0" alt="Loading, please wait..." />');
		}
 
		function send_request() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial.php', callback);
		}
 
		function test_failure() {
			show_progressbar('content');
			var callback = { success:success_handler,	failure:failure_handler, timeout: 10000 };
			YAHOO.util.Connect.asyncRequest('GET', 'http://images.code-head.com/tutorials/ajax/some-file-that-doesnt-exists.php', callback);
		}
 
		var progress_bar = new Image();
		progress_bar.src = 'http://images.code-head.com/progress-bars/4.gif';
	</script>
 
</head>
 
<body>
<a href="javascript:send_request();">Send Request</a> | <a href="javascript:test_failure();">Fail a request</a>
<div id="content">
 
</div>
</body>
</html>

Go ahead and try it here:
http://images.code-head.com/tutorials/ajax/beginner-ajax-tutorial-progress-bar.html

This is it for now, on the next AJAX tutorial we are going to make a little sample website with all of these:)
Thanks,
-Codehead

Next: AJAX – Beginner AJAX Tutorial – Creating a simple AJAX website with example

AJAX – Beginner AJAX Tutorial – Display a Progress Bar or a Loading Message
Comments (5)   Filed under: AJAX, JavaScript, PHP, Web Development, yui   Posted by: Codehead
Older Posts »