...

View Full Version : window onloads not working together



tpeck
01-13-2007, 05:46 AM
How can I add these two window.onloads together:

(they are in different external js files)


if (ie||ns4||ns6)
window.onload=onad

and


window.onload = function()
{
var theInputs1 = document.getElementsByTagName('input');
for(var i = 0; i < theInputs1.length; i++)
{
if(theInputs1[i].type == 'text')
{
theInputs1[i].onkeyup = function()
{
createCookie(this.name,this.value,9999);
}
}
if(readCookie(theInputs1[i].name+'val') != null)
{
theInputs1[i].value = readCookie(theInputs1[i].name+'val');
}
}

var theInputs2 = document.getElementsByTagName('textarea');
for(var i = 0; i < theInputs2.length; i++)
{
theInputs2[i].onkeyup = function()
{
createCookie(this.name,this.value,9999);
}
if(readCookie(theInputs2[i].name+'val') != null)
{
theInputs2[i].value = readCookie(theInputs2[i].name+'val');
}
}
}

I've tried putting the second piece of code in with the first external file, but

window.onload = onad;function() { etc.

...does not work.

Besides, the if (ie||ns4||ns6) is not being obeyed.

It must be possible surely?

TripperTreats
01-13-2007, 06:47 AM
The top level function of an event handler can only be a single function. You would either need to put


if (ie||ns4||ns6) window.onload = onadinside of the other long function, or make that long function a separate one called onloadFn(). Then somewhere after the end of all your javascript you would need one overall onload:


window.onload = function () {
if (ie||ns4||ns6) window.onload = onad;
onloadFn()
}What I do on my site, because I have many onload events which change often, I have a static


window.onload = onloaderin my script and onloader looks like:


function onloader () {
function1();
function2();
function3();
etc., etc.
}

tpeck
01-13-2007, 08:12 AM
Obviously you know what you are doing, but I can't be following it accurately because this does not do both jobs:


<!--
// ********** button *********************
var XX=20;
var xstep=0 ;
var delay_time=20;
var YY=0;
var ch=0;
var oh=0;
var yon=0;
var ns4=document.layers?1:0
var ie=document.all?1:0
var ns6=document.getElementById&&!document.all?1:0
if(ie){
YY=document.body.clientHeight;mbutton.style.top=YY;
}
else if (ns4){
YY=window.innerHeight;
document.mbutton.pageY=YY;
document.mbutton.visibility="hidden";
}
else if (ns6){
YY=window.innerHeight
document.getElementById('mbutton').style.top=YY
}
function reloc1()
{
if(yon==0){YY=YY-xstep;}
else{YY=YY+xstep;}
if (ie){
ch=document.body.clientHeight;
oh=mbutton.offsetHeight;
}
else if (ns4){
ch=window.innerHeight;
oh=document.mbutton.clip.height;
}
else if (ns6){
ch=window.innerHeight
oh=document.getElementById("mbutton").offsetHeight
}
if(YY<0){yon=1;YY=0;}
if(YY>=(ch-oh)){yon=0;YY=(ch-oh);}
if(ie){
mbutton.style.right=XX;
mbutton.style.top=YY+document.body.scrollTop;
}
else if (ns4){
document.mbutton.pageX=XX;
document.mbutton.pageY=YY+window.pageYOffset;
}
else if (ns6){
document.getElementById("mbutton").style.right=XX
document.getElementById("mbutton").style.top=YY+window.pageYOffset
}
}
function onad()
{
if(ns4)
document.mbutton.visibility="visible";
loopfunc();
}
function loopfunc()
{
reloc1();
setTimeout('loopfunc()',delay_time);
}

// ************** cookie ************************

function createCookie(name,whatval,days)
{
if(whatval != '')
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"val="+whatval+expires+"; path=/";
}
}

function readCookie(name)
{
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function onloadFn()
{
var theInputs1 = document.getElementsByTagName('input');
for(var i = 0; i < theInputs1.length; i++)
{
if(theInputs1[i].type == 'text')
{
theInputs1[i].onkeyup = function()
{
createCookie(this.name,this.value,9999);
}
}
if(readCookie(theInputs1[i].name+'val') != null)
{
theInputs1[i].value = readCookie(theInputs1[i].name+'val');
}
}

var theInputs2 = document.getElementsByTagName('textarea');
for(var i = 0; i < theInputs2.length; i++)
{
// if(theInputs[i].type == 'text')
// {
theInputs2[i].onkeyup = function()
{
createCookie(this.name,this.value,9999);
}
// }
if(readCookie(theInputs2[i].name+'val') != null)
{
theInputs2[i].value = readCookie(theInputs2[i].name+'val');
}
}
}

window.onload = function () {
if (ie||ns4||ns6) window.onload = onad;
onloadFn()
}

// End -->


I have combined both files into one, and done as you suggested at the bottom. You can see there are two major things happening...a floating button which should show up, and the reading of some cookies.

For some reason, the "onad" function doesn't seem to get called. And, if the cookie is called on the page I get an "object required" error in line 14 ("YY=document.body.clientHeight;mbutton.style.top=YY;")

What have I done wrong do you think?

TripperTreats
01-13-2007, 01:33 PM
This is why the onad function isn't getting called:


window.onload = function () {
if (ie||ns4||ns6) window.onload = onad;
onloadFn()
}What's happening is that once the window loads, this overarching function is called. Now, since the window is already loaded, the


if (ie||ns4||ns6) window.onload = onad;is too late to tell the window what to do onload. Just replace it with


if (ie||ns4||ns6) onad();Fix that, and then let's see whether you still get that "object required" error. If so, we'll tackle that problem next.

TripperTreats
01-14-2007, 03:53 AM
You know I just realized that I was the one who typed the original window.onload incorrectly, necessitating my previous post. I'm sorry about that! I hope I didn't sound snooty when I was correcting you when it was actually my previous typo I was correcting. Did you get it to work?

tpeck
01-14-2007, 03:59 AM
Not at all! No worries...

I'm always amazed at how helpful people are. I'm not much use, but I post these demos so that others might profit from them.

Here is a demo of the idea you corrected in action.

(There is still a problem with the two variables somehow saving as the same cookie - ie. the input box and the textarea box don't save separate values. But the onload now seems to be working for both routines, the button and the cookies.)

http://aapress.com.au/demo/onload/page1.html

Thanks again.

tpeck
01-14-2007, 04:12 AM
By the way, Jason, your site only shows the navigation scheme in FF; it doesn't appear to function in IE7. (Maybe in 6, I don't know.) And I get a stack overflow at line 0 error too.

Nice site though, but only in FF for me. Is it my browser?

TripperTreats
01-14-2007, 04:46 AM
From the time of your post, you may have accessed my site in the middle of my updating it. I have been working on it all day and at various times it was not working (the nature of inchoate coding).

By the way, do you know what that stack overflow error is all about? I've been getting it a lot, but only in IE.

Would you do me a really big favor? I don't have (and can't get) IE7, because that takes a valid windows key (which I don't have). Would you mind checking out my site again quickly in IE7?

I'm hoping that the navigation problem you mentioned was just a result of my interim repairs. By the way, I thought I had a great CSS layout before, but then I came across http://www.positioniseverything.net/articles/pie-maker/pagemaker_form.php. You should check it out! My new and improved products page is thanks to it. Thanks so much for your reciprocal help with my site! Isn't this forum great?

tpeck
01-14-2007, 05:12 AM
I think it's my problem. Perhaps I haven't got Flash installed correctly. Sometimes flash works for me in IE7 but not always. Reinstalling it does nothing. I don't think it's IE7's problem, but it's something I've done.

Maybe it's a bit dicey having a nav bar that is only flash, though - perhaps both a flash nav bar AND an ordinary one down the bottom (or my next/previous idea!)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum