View Full Version : undefind error on page load as home page only?
Crash1hd
12-13-2003, 10:08 AM
OK I am useing the code below to create a polite reminder that they browser they are useing is not the most current version! the problem I am having is that it works fine on all browsers and it works fine on netscape 4.8 as long as I dont set the page as my homepage and first open the browser when I do that it tells me that available_width is undefind however if I reload the page it works fine?
code here
<script language="JavaScript">
function browserdimensions(){
if(ns||w3) {
available_width=innerWidth;
available_height=innerHeight;
}
if(ie) {
available_width=document.body.clientWidth;
available_height=document.body.clientHeight;
}
}
ONLOAD=browserdimensions();
var wa
wa = ((available_width/2)-215)
function browservers(){
if(ie1 || ns1 || mz1 || op || ns){
if(ie1 && !op){window.open('http://v4.windowsupdate.microsoft.com/en/default.asp','','toolbar,menubar,scrollbars,status,location,resizable,width=800,height=600')}
if(mz1 && !ns1 && !ie1 && !ns){window.open('http://www.mozilla.org','','toolbar,menubar,scrollbars,status,location,resizable,width=800,height=600')}
if(ns1 || ns){window.open('http://channels.netscape.com/ns/browsers/default.jsp','','toolbar,menubar,scrollbars,status,titlebar,location,resizable,width=800,height=600' )}
if(op){window.open('http://www.opera.com','','toolbar,menubar,scrollbars,status,location,resizable,width=800,height=600')}
}
else{alert("Please visit your browsers website to upgrade!")}
}
if((ie1 && app < 4) || (!ie1 && app < 5) || (op && navigator.userAgent.substring (57,61) < "7.23")){document.write("<Div style='position:absolute; top:0; left:"+wa+"; width: 430px; z-index: 3'><font color=red><A HREF='#' onClick='browservers()' style='text-decoration:none;color:red'>Your browser is older then its current version click here to update.</A></font></div>");}
</script>
Any thoughts anyone?:confused:
Maybe it's parsing the page and executing the code before the browser is fully set up, ie before availWidth and availHeight are defined?
Crash1hd
12-13-2003, 10:11 AM
ok is there a way of correcting that cause I even tried useing
available_width=800;
and it still says its undefined even though I have defined it!
You say you have your own variable called available_width which you're using, or you're trying to set availWidth? If it's the latter, you'll run up against problems, because not only have the variables not been set, they haven't even been created.
Crash1hd
12-13-2003, 10:14 AM
oh I am putting that line in here
function browserdimensions(){
if(ns||w3) {
available_width=800;
available_height=innerHeight;
}
if(ie) {
available_width=document.body.clientWidth;
available_height=document.body.clientHeight;
}
}
Crash1hd
12-13-2003, 10:35 AM
Ok I found by changing innerWidth; to window.innerWidth; solves the browser undefind error! I had done a test by creating a page that just said
<script>
document.write(innerWidth;)
</script>
didnt show anything on browser load however when I put
<script>
document.write(window.innerWidth;)
</script>
showed the browsers innerWidth on browser load!
now I have to figure out why the data inbetween the div tags doesnt show up on the first load but after a reload it does?
Crash1hd
12-13-2003, 12:07 PM
Ok so if anyone has any idea why the following code doesnt show up in netscape 4.8 on browser load with the page as your homepage if you reload or load it while the browser is already open it works fine and it works fine in all other browsers!
<html><head>
<script src="BrowserSniffer.js"></script>
<SCRIPT LANGUAGE="JavaScript">
<!--
document.write(available_width)
//-->
</SCRIPT>
</head>
<body>
</body>
</html>
inside BrowserSniffer.js is
available_width=window.innerWidth;
doesnt come in but when I do this it does work?
<html><head>
<SCRIPT LANGUAGE="JavaScript">
<!--
available_width=window.innerWidth;
document.write(available_width)
//-->
</SCRIPT>
</head>
<body>
</body>
</html>
Probably because it's parsing the second script before the first.
Crash1hd
12-13-2003, 12:11 PM
ok I found out if I put
if(ns){available_width=window.innerWidth;}
instead it doesnt work but if I put
if(document.layers){available_width=window.innerWidth;}
it does I have a feeling its not calling the js file soon enough is there a way of calling the js file better then
<script src="BrowserSniffer.js"></script>
so that it calls it sooner or am I calling it in the wrong area?
glenngv
12-13-2003, 01:30 PM
Change this line:
ONLOAD=browserdimensions();
to:
onload=browserdimensions;
You should not use parentheses when assigning event handlers in javascript. If you have that, the function will be executed immediately, not assigned to the handler.
Crash1hd
12-13-2003, 09:23 PM
thats good to know but I removed the calling of the browserdimensions so that it was called all the time! however for some reason it doesnt call what is in the () of the if statement right away I found that even with this basic code
<html><head><script type="text/javascript" src="BrowserSniffer.js"></script>
</head>
<body>
<SCRIPT>
<!--
if(ns){available_width=window.innerWidth;}
if(!ns){available_width=document.body.clientWidth;}
document.write(available_width)
//-->
</SCRIPT>
</body>
</html>
it needs to be reloaded before it shows up but if I remove the if(ns){} part it calls it right away cause its having trouble calling what is in the js file?
glenngv
12-15-2003, 01:07 AM
Can you show what's in BrowserSniffer.js?
Crash1hd
12-15-2003, 07:40 AM
Just the following
var ns=(document.layers);
however the original is much larger but I removed everything in the example just to determine that its cause its not loading the js file cause if you put
var ns=(document.layers);
right into the page in <script></script> it works fine!
glenngv
12-15-2003, 07:50 AM
Do you have the page online so I can test it in my NS4.7?
Crash1hd
12-15-2003, 07:58 AM
Yep goto http://test.alwaysremember.ca/open.htm
but remember you have to make it your homepage and then close and reopen or save the file locally and open it for the problem to happen
glenngv
12-15-2003, 08:44 AM
It maybe a bug for document.layers in the external script
If I replaced that with:
var ns=(navigator.appName=="Netscape" && parseInt(navigator.appVersion)==4);
it works.
I've heard from adios in one of the recent threads here that there's a bug for document.layers called in the head.
Crash1hd
12-15-2003, 08:49 AM
are you sure you are opening the link on the browser opening meaning there is absolutly nothing open before it (homepage) cause it works great if netscape is already open and you goto the page after already viewing another page but I am trying to get it to work on page open? so if for some reason the user has this page sent to them in a link in there email and they click on it and there default client is netscape 4 or something like that it will work right away and not on the reload!
Personally I realize it doesnt matter cause the minute they goto any other part of the site it will show up! I just like seeing if there is a fix for a glitch!:D
glenngv
12-15-2003, 08:57 AM
I copied the html and js source and opened the page locally in NS4. I saw what you were experiencing -- not displaying the innerWidth value on first load. And modifying it the way I did, fixed it. So, the glitch is in document.layers in external script, when immediately called as the page is being rendered.
Crash1hd
12-15-2003, 09:02 AM
:o :o :D :D Yep but so does this
var ns=(document.layers);
if you put it just in the open.htm file and then load it! It also works.
:) Thats why i came to the same conclusion! I was just wondering if there was a work around for that glitch I guess I could always just put the
var ns=(document.layers);
right into the main page and it would work but then it wouldnt be global. :thumbsup:
glenngv
12-15-2003, 09:10 AM
As long as you put that outside any function, that would be global. But if you want to reuse that variable in other pages, it's better to put it in an external js and instead use the code below to avoid that document.layers bug.
var ns=(navigator.appName=="Netscape" && parseInt(navigator.appVersion)==4);
Crash1hd
12-15-2003, 09:17 AM
Lol I had a similar script for to solve that problem
this detects the newest version
var ns1 = navigator.appName=="Netscape" && navigator.userAgent.substring (71,79)=="Netscape";
but I think I will add that too my other js file :)
Crash1hd
12-15-2003, 09:36 AM
Ok so this is Ironic I put your code into my js file of the website and now it works on load :) lol too funny:rolleyes::confused::rolleyes:
So hey it works and thats all that matters :thumbsup:
glenngv
12-16-2003, 01:25 AM
I told you...but you didn't listen at once...:D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.