Forum Moderators: open

Message Too Old, No Replies

Embed text into HTML, automatically, using a script

         

alex100

5:40 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



Hi there,

I would like to find a way that will allow me to put a code in a web page (javascript or whatever) and upon loading the page in the browser that code will need to fetch some text data from the server, *automatically*, without any user interaction. The problem is that I need this data to be retrieved based on the day time of the computer running the browser session. When local day is Monday, it must retrieve some data, when for users the other part of the world is already Tuesday, some other data. The page is not stored locally, but on some other web servers.

I'm new to Javascript (just started to look over the web) and I found that I could do something like this:

<script language='JavaScript' type='text/javascript'>
<!--
var time = new Date()
var current_month = time.getMonth()
var current_day = time.getDate()
document.write("<script language='JavaScript' src='")
document.write("http://server/show_" + current_month + "_" + current_day + ".js'></script>")
//-->
</script>

This script will automatically execute the show_(current_month_&_day).js file, which contains the needed data for that particular day of the year. Since Javascript is executed on client side, I will need to have 365 .js files on the server, right? I'm not fully clear on this, but this is how it appears to me now.

I would like to find a way that will allow me to call and execute something like a perl script, with two variables (day & month), and since the perl script is executed on the server side, it can output data based on the input variables.

Something like:

[server...]

This could be called from the javascript, if that's possible! The role of the javascript is to call the perl script automatically and send it the current day and month of the local computer, in the two variables.

Upon execution, the perl script will output the text information associated with that particular date, and this information should be embedded in the web page that called the script (in the same location which the script was called from). Again, the web page is not local, otherwise I would have used SSI, which can do this with no problem. And it must work automatically, no user action at all, other than loading the HTML page.

What do you think, can this be done some way or another, and how?

I would like to find a more natural/elegant solution, other than needing to have 365 scripts on the server.

Sorry, hope you could understand me, at least a bit ;) ... english is not my primary language.

Thanks,

Alex

alex100

10:48 pm on Apr 6, 2008 (gmt 0)

10+ Year Member



Ok, after some more digging I've finally been able to make it work the way I want. Very simple and straight forward, I wish I tried this in the first place:

<script language='JavaScript' type='text/javascript'>
<!--
var time = new Date()
var current_month = time.getMonth()
var current_day = time.getDate()
document.write("<script language='JavaScript' src='")
document.write("http://server/script.pl?month=" + current_month + "&day=" + current_day + "'></script>")
//-->
</script>

alex

httpwebwitch

1:30 pm on Apr 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



good job! glad you got it working.

personally I would have taken a different route.
Instead of creating a server script that writes out procedural javascript, I would have had my server script output some JSON. Then I'd keep the logic on the client, and request the data with an XmlHttpRequest, ala AJAX.