|
|

|
|
Dynamic data using the DOM and Remote Scripting
|
|||||||||||||||||
How it worksAs most of you probably know the DOM allows us to dynamically create almost any element that can be on a page, so
we use it to create a SCRIPT tag that we set a src to. When setting the src of a script tag then adding it
to the page the DOM browsers will try to load the script file that we specify.
function loadContent(file){
var head = document.getElementsByTagName('head').item(0)
var scriptTag = document.getElementById('loadScript');
if(scriptTag) head.removeChild(scriptTag);
script = document.createElement('script');
script.src = file;
script.type = 'text/javascript';
script.id = 'loadScript';
head.appendChild(script)
}
Let's go line by line;
var head = document.getElementsByTagName('head').item(0)
This just makes a short reference to the head element. Used later in the function.
var scriptTag = document.getElementById('loadScript');
This line tries to get the script tag called loadScript. The first time this
function is called this will return null because the tag does not exists.
if(scriptTag) head.removeChild(scriptTag);If the tag exists we remove it because we don't want all the old tags to stay in the page. In explorer you don't really have to do this, you can just set a src to the existing script element but that doesn't seem to work in Mozilla. script = document.createElement('script');
This creates a script tag.
script.src = file;This line sets the src of the script tag to the file that you specify when you call the function. script.type = 'text/javascript';We set the type of the script tag to text/javascript script.id = 'loadScript';Here we set the ID to loadScript so we can reach it with the second line in the code the next time this function is called. head.appendChild(script)Then we add the script tag to the page with the appendChild function. It's added to the head of the page. So if you call the function like this:
loadContent('myfile.js')
it's more or less
the same as adding a script tag with regular code like this:
<> Note that the file you load in a script tag like this HAVE to be a JS file. You cannot load another HTML file or whatever like this. It has to valid script inside the file. Ok, now we know how it works, but what can we do with it? There are tons of things we can do with this technique, let's start of with a simple example Simple exampleI had a hard time finding a very simple example for this one so I'll make a totally useless one
just so you can see that it works.
Menu scriptLet's say you have a menu similar to the foldoutMenu from this site
with 200 menu items. This is a modified and a little simplified version where the script only loads the
top items as HTML (making the older browsers use the main items only), and loads
the other elements with the loadContent function only if and when they are needed.
ASP Menu scriptThis part of this tutorial shows an ASP example and you should know some ASP to understand this.
And in tblProducts:
Now we are ready to start displaying the data. The first page displays the categories as topMenus: (note that I code ASP in JScript - so should you!)
//Pointer to the database
database="Provider=Microsoft.Jet.OLEDB.4.0;"
+"Data Source="+Server.MapPath("example.mdb")+";"
//opening db
q="Select * from tblCategories"
rs=Server.CreateObject("ADODB.Recordset")
rs.open(q,database)
//looping records
while(!rs.EOF){
name=rs("catname").value
id=rs("dbid").value
Response.write('<
The second page "products.asp" is that page that will be loaded with the js loading. If you're thinking "Hey, I thought you
said that it had to be js file!!" your right, so we have to get the server to return the content as a javascript file.
We do that with this little line:
Response.ContentType="text/js"So the entire products.asp looks like this:
@LANGUAGE=JAVASCRIPT
//Getting the ASP page to return a js file
Response.ContentType="text/js"
//Getting the id
catid = Request.queryString("id")
//Pointer to the database
database="Provider=Microsoft.Jet.OLEDB.4.0;"
+"Data Source=" + Server.MapPath("example.mdb") + ";"
//opening db
q="Select * from tblProducts where category = "+catid
rs=Server.CreateObject("ADODB.Recordset")
rs.open(q,database)
//looping database records
while(!rs.EOF){
txt=rs("ProductName").value + " " + rs("ProductInfo").value
id=rs("dbid").value
Response.write('addElements("'+txt+'","","show_product_details.asp?id='+id+'")\n')
rs.moveNext()
}
rs.close()
rs=null
Response.write("loaded()")
Again, this was a simple example, and the only thing it does is get the info from the db and display
in the menu from the last example. I have not included any product display page or any error checking.
But I am sure that if you get the point of this example you'll see
that a similar approuch could be used to load entire tables of data in all kind of cool ways.
DownloadYou can download all the examples from this tutorial here (with the database and ASP files).
Copyright ©2000-2002 DHTMLCentral.com, Bratta Communications. All rights reserved. |