Home arrow JavaScript arrow Page 2 - Handling Remote Files with JavaScript Click Interceptions
JAVASCRIPT

Handling Remote Files with JavaScript Click Interceptions


If you’re a web developer who frequently builds JavaScript applications, then you may have already used click interceptions, even without being aware of their numerous advantages. This four-part series, of which this is the final part, shows you how to get the most out of them.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
November 26, 2008
TABLE OF CONTENTS:
  1. · Handling Remote Files with JavaScript Click Interceptions
  2. · Showing the contents of several text files in different windows
  3. · Loading the contents of several text files without web page reloads
  4. · The full source code of the text file reading application

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Handling Remote Files with JavaScript Click Interceptions - Showing the contents of several text files in different windows
(Page 2 of 4 )

As I explained at the beginning, first I’m going to build a rudimentary web page that contains a bunch of links. Once the links have been created, they will open distinct sample text files in a new window, a functionality you've probably coded dozens of times before.

So, I’ll start by building four basic text files, whose respective signatures are listed below:

(definition of textfile1.txt file)


This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1. This is the content of text file 1.


(definition of textfile2.txt file)


This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2. This is the content of text file 2.


(definition of textfile3.txt file)


This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3. This is the content of text file 3.


(definition of textfile4.txt file)


This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4. This is the content of text file 4.


As you can see, the respective definitions of the above sample text files are actually fairly trivial, yet they’ll be more than enough to demonstrate another case where click interceptions can be really useful. But I’m getting ahead of myself; having created the files in question, the only thing that remains undone is building a group of links that load the contents of these files in a different window.

With that concept in mind, you will see below a brand new web page that includes precisely those links:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Example on reading text files</title>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h1{

font: bold 18pt Arial, Helvetica, sans-serif;

color: #000;

}

h2{

font: bold 16pt Arial, Helvetica, sans-serif;

color: #000;

}

a:link,a:visited{

font: normal 10pt Arial, Helvetica, sans-serif;

color: #00f;

}

a:hover{

color: #f90;

}

p{

font: normal 10pt Arial, Helvetica, sans-serif;

color: #000;

}

#linkcontainer{

width: 300px;

padding: 5px;

background: #ffc;

}

#filecontainer{

width: 300px;

}

</style>

</head>

<body>

<h1>Example on reading text files</h1>

<div id="linkcontainer">

<ul>

<li><a href="textfile1.txt" title="Read contents of text file here...">Read contents of text file 1...</a></li>

<li><a href="textfile2.txt" title="Read contents of text file here...">Read contents of text file 2...</a></li>

<li><a href="textfile3.txt" title="Read contents of text file here...">Read contents of text file 3...</a></li>

<li><a href="textfile4.txt" title="Read contents of text file here...">Read contents of text file 4...</a></li>

</ul>

</div>

<div id="filecontainer"></div>

</body>

</html>


Well, certainly you’ll have to agree with me that the above web document is very simple! As you can see, it contains a list of regular links. When clicked on, these links will load the contents of different text files in the same window. That’s precisely the default behavior of these links, something that’s clearly illustrated by the following screen shot:



So far, everything looks good, right? If you test the previous web page, along with the four text files created earlier, you’ll see that each time you click on one link, the contents of a specific file will be neatly fetched by the browser, and the whole web page will be reloaded. However, and here’s where click interceptions come in, I’d like to change this behavior and make these contents be displayed within a unique DIV on the same web document, without any additional web page reloads.

Is it possible to do such a thing? Of course it is. By means of a few click interceptions I’ll show you how to load the contents of these sample text files on the same web page.

This topic will be covered in detail in the following section, so jump ahead and read the next few lines. I’ll be there, waiting for you.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials