There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Can this be done in Javascript?


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Chris_E's Avatar
Senior Member with 296 posts.
 
Join Date: Feb 2006
Location: Wellingborough, UK
Experience: I have no idea what I don't know - how could I?
19-Feb-2007, 02:55 PM #1
Can this be done in Javascript?
Here's the scoop...

On a webpage is a table. In a cell is the following ASP script:

Quote:
<%
' Select a random number from one to five

randomize
random_number=int(rnd*5)+1

' Assign the variable page_to_read with the file being requested, (incorporating the
' random number into the file name

page_to_read="features"& random_number &".htm"

' Open the document to be read

set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath(page_to_read))

'read all the text on the document

read_text = act.readall

' Close the document

act.close

' Write out the contents of the document to the browser

response.write read_text

%>
This gets a pre-written HTML file and populates the cell with that html page (a small bit of code... don't worry about the content of these pages).

I already have files called 'features1.htm",'features3.htm",'features3.htm" and so on to 5.

Obviously, this is server side ASP but can the same be done in Javascript?

It would make my life easier if it were! CAn anyone help?

Chris
Big-K's Avatar
Distinguished Member with 6,883 posts.
 
Join Date: Nov 2003
Location: Kansas
Experience: Advanced
20-Feb-2007, 10:50 PM #2
1) download some sort of ajax library
2) link to the library in your file
3) create a script such as this:
<script type="text/javascript">
var ajax = new GLM.AJAX();
function populate1(content){
td = document.getElementById('td1');
td.innerHTML = content;
}
ajax.callPage("p1.htm", populate1);
</script>

it will execute automatically when the page loads. Copy/paste that for each td cell, might be better to use an array, replaceing p1.htm with the page, populate1 with populate#oftime, and td1 with the td id. quickly written, sorry
Chris_E's Avatar
Senior Member with 296 posts.
 
Join Date: Feb 2006
Location: Wellingborough, UK
Experience: I have no idea what I don't know - how could I?
21-Feb-2007, 12:55 PM #3
Thanks Big-K, for giving me a clue on this one!

I did as you suggested and downloaded Ajax from Sourceforge and I got the glm-ajax.js file.

Here's what I then did...

- created a folder in my site root called "script" and moved the glm-ajax.js to there.

Followed your guides, putting the content you gave into one cell of a table on a basic new page.

So it looks like this:

Quote:
<HTML>
<HEAD>
<SCRIPT language="JavaScript" src="script/glm-ajax.js"></SCRIPT>
<TITLE>WAFFLE</TITLE>
<BODY>
<TABLE>
<TR>
<TD>
<script type="text/javascript">
var ajax = new GLM.AJAX();
function populate1(content){
td = document.getElementById('td1');
td.innerHTML = content;
}
ajax.callPage("p1.htm", populate1);
</script>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
I then created a web page called p1.htm with a simple text message in it.

Sadly, opening this in IE I see nothing returned. I guess I'm being thick... can you advise please?

I suspect I'm not fully appreciating the use of td and 'td1' and td.innerHTML.

Once this is sorted, will there be a way to randomise the page displayed in the cell of the table.

In otherwords, with 4 pages available (let's call them p1.htm to p4.htm), I'd like to have a random choice of p1 to p4 loaded into the cell on a page load or refresh, kind of idea.

Hope I'm making sense!

Best regards

Chris
Big-K's Avatar
Distinguished Member with 6,883 posts.
 
Join Date: Nov 2003
Location: Kansas
Experience: Advanced
22-Feb-2007, 01:30 AM #4
the td's need id's, make <TD> into <TD ID="td1">.

for a random choice, put this into the script, not in the function though. It will only work if i'm right in thinking you want the different pages to all appear in the same cell if chosen.

<script type="text/javascript">
var td_arr = new array();
td_arr[0] = "p1.htm";
td_arr[1] = "p2.htm";
td_arr[2] = "p3.htm";
td_arr[3] = "p4.htm";

var i = Math.floor(Math.random() * td_arr.length);

var ajax = new GLM.AJAX();

function populate(content){
td = document.getElementById('td1');
td.innerHTML = content;
}
ajax.callPage(td_arr[i], populate);
</script>

its late so there might be bugs in this, i didn't test it, tell me if there are
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:58 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.