HighDots Forums  

Removing an event from the html code.

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Removing an event from the html code. in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
graham.reeds@gmail.com
 
Posts: n/a

Default Removing an event from the html code. - 11-02-2005 , 09:03 PM






I originally posted this to comp.lang.javascript and got one response
that completely ignored the original request and in fact suggested
changes that wouldn't work. I am now posting here hoping to have a
slightly better reaction. I have modified it from my original post to
hopefully clarify my intent.

I am updating a website that uses a countdown script embedded on the
page. When the page was served the var's are set to how long the
countdown has left in minutes and seconds, but the rest of the script
is copied untouched. Obviously that means that 500bytes are needless
transferred per page request.

I want to take the script out of the page and have it as a seperate
file that can be cached, reducing serving costs - the page gets hit a
couple of thousand times per day, sometimes as high a 5K, so any
savings are multiplied.

The original script is displayed below (with added whitespace). The
variables min and sec are given the values of 40 each to aid in local
debugging. The actual values are written in when the page is served to
the user.

The original script is:
----------
var min=40;
var sec=40;

function countdown()
{
if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }

msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';

if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;

if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout('countdown()',1000);
}

window.onload=countdown;
----------

To reiterate: the vars are set to 40 for testing purposes. The values
are set when the page is sent from the server and could be anything. 40
was picked because it seemed as good a value as any. I guess to be more
geeky I could of picked 42...

I have sinced learned that the document.all section is IE4 specific and
as such can be removed safely. The version I have now no longer has it
in and works on FF, IE6 and Opera safely (all W2K).

So I thought the easiest way to do this would make the function accept
two vars, and then have the server generate a small bit of code to call
the JS function with.

However the closest I can get is this:
----------
function countdown(m, s)
{
sec = s;
min = m;

if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }

msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';


if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;

if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout("countdown(min,sec)",1000);
}

window.onload=countdown(40,40);
----------

The window.onload line was left in to make sure that the code worked on
IE & FF. Originally it did not appear to work in IE6 but bizarrely it
now seems to. As soon as I move the 'window.onload' line out of the .js
file and into the HTML code it stops working.

I thought the code necessary to get it working would be the following,
contained in <head>:
----------
<script src="scripts/countdown.js" type="text/javascript">
<!--
window.onload=countdown(40,40);
//-->
</script>
----------

What do I need to do to get this working?

Thanks, Graham Reeds.


Reply With Quote
  #2  
Old   
Jasen Betts
 
Posts: n/a

Default Re: Removing an event from the html code. - 11-04-2005 , 02:05 AM






On 2005-11-03, graham.reeds (AT) gmail (DOT) com <graham.reeds (AT) gmail (DOT) com> wrote:
Quote:
I want to take the script out of the page and have it as a seperate
file that can be cached, reducing serving costs - the page gets hit a
couple of thousand times per day, sometimes as high a 5K, so any
savings are multiplied.

put this part in the external javascript file.

Quote:
function countdown()
{
if (sec==0 && min>0)
{ sec=59; min--; }
else
{ sec--; }

msg = '<b>Next Tick: ' + min + ':' + ((sec < 10) ? '0' : '') + sec +
'</b>';

if (document.all)
document.all.tel.innerHTML = msg;
else if (document.getElementById('tel'))
document.getElementById('tel').innerHTML = msg;

if (sec==0 && min==0)
{ min=60; sec=00; }
if (sec>0 || min>0)
setTimeout('countdown()',1000);
}

put this part in the main HTML file.

Quote:
var min=40;
var sec=40;
window.onload=countdown;

Bye.
Jasen


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.