|
|
#1 |
|
Bronze Member
![]() Join Date: Apr 2010
Posts: 35
|
Some final questions about onload and onresize
I've been working about two weeks on a script that can accurately determine the width of the browser window. I think I have my test page showing everything now (code at end of post). Line 3 on each test shows the sequence of events as the web page sees them. The first letter indicates the onLoad or onResize event, the numbers tell the second and millisecond when the event occurred, red events were ignored because the page width did not change and green events updated the page display. Line 1 or 2 is updated depending on the event and line 4 is updated whenever the script is actually run. Save the code to disk and double click it to open a browser window and display the page: Code:
resize the page: Code:
reload the page with F5 Code:
resize the page again Code:
Thanks in advance PapaGeek! Code:
<!-- saved from url=(0014)about:internet -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Display window size</TITLE>
<SCRIPT language="JavaScript">
var runs = "";
var lastWidth = 0;
function showWid2()
{
//<BODY onload=showWid2() onresize=showWid3()>
if (window.onresize == null) {
window.onresize=showWid3;
runs = runs + " set onResize";
}
var wid2 = screen.width;
if (document.layers) {
wid2= window.innerWidth;
}
// Explorer 6 Strict Mode
else if (document.documentElement && document.documentElement.clientWidth) {
wid2 = document.documentElement.clientWidth;
}
else if (document.all) {
wid2 = document.body.clientWidth;
}
else {
wid2 = "Unknown DOM";
}
var currentTime = new Date()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
var millis = currentTime.getMilliseconds()
if (lastWidth == wid2) {
runs = runs + " <font color=red>L" + seconds + "." + millis + "x<font color=black>";
document.getElementById("showList").innerHTML = runs;
}
else
{
runs = runs + " <font color=green>L" + seconds + "." + millis + "<font color=black>";
lastWidth = wid2;
document.getElementById("widthOfPage").innerHTML = wid2;
document.getElementById("showWidthOnLoad").innerHTML = wid2;
document.getElementById("showOnLoadTime").innerHTML = minutes + ":" + seconds + "." + millis;
document.getElementById("showList").innerHTML = runs;
}
}
function showWid3()
{
var wid3 = screen.width;
if (document.layers) {
wid3= window.innerWidth;
}
// Explorer 6 Strict Mode
else if (document.documentElement && document.documentElement.clientWidth) {
wid3 = document.documentElement.clientWidth;
}
else if (document.all) {
wid3 = document.body.clientWidth;
}
else {
wid3 = "Unknown DOM";
}
var currentTime = new Date()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
var millis = currentTime.getMilliseconds()
if (lastWidth == wid3) {
runs = runs + " <font color=red>R" + seconds + "." + millis + "x<font color=black>";
document.getElementById("showList").innerHTML = runs;
}
else
{
runs = runs + " <font color=green>R" + seconds + "." + millis + "<font color=black>";
lastWidth = wid3;
document.getElementById("widthOfPage").innerHTML = wid3;
document.getElementById("showWidthOnResize").innerHTML = wid3;
document.getElementById("showOnResizeTime").innerHTML = minutes + ":" + seconds + "." + millis;
document.getElementById("showList").innerHTML = runs;
}
}
</SCRIPT>
</HEAD>
<BODY onload=showWid2()>
<ol>
<li>At <b id=showOnLoadTime>??:??</b> onload calculated a page width of <b id=showWidthOnLoad>Jave Script did not run yet</b>.</li>
<li>At <b id=showOnResizeTime>??:??</b> onresize calculated a page width of <b id=showWidthOnResize>Jave Script did not run yet</b>.</li>
<li>Sequence: <b id=showList>??</b></li>
<li>The last <font color=green><b>Green</b><font color=black> event in the sequence calculated the page as <b id=widthOfPage>00</b> pixels wide.</li>
</ol>
</BODY>
</HTML>
|
|
|
|
![]() |
| Tags |
| onload, onresize, script |
| Thread Tools | |
| Display Modes | |
|
|