发新话题
打印

事件串联的方法 runOnLoad.js

事件串联的方法 runOnLoad.js

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html lang="us-en">
<head>
<title>runOnLoad.js</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="keywords" content=" "/>
<meta name="description" content="" />
</head>
<body>
<script type="text/javascript">
<!--
/* runOnload.js  being
more runOnload.js information website:
http://examples.oreilly.com/jscript5/js5examples.tar.gz/
  js5examples/17/runOnLoad.js
*/
shq={}
shq.runOnload=function(f)
{

         if(shq.runOnload.loaded)f(); // If already loaded, just invoke f() now.
         else shq.runOnload.funcs.push(f); // Otherwise, store it for later

        
}

shq.runOnload.loaded=false;
shq.runOnload.funcs=[];
shq.runOnload.run=function(){
                if (this.loaded) return;
                for(var i = 0; i < shq.runOnload.funcs.length; i++) {
        try { shq.runOnload.funcs(); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
         }
                 shq.runOnload.loaded = true; // Remember that we've already run once.
         delete shq.runOnload.funcs;  // But don't remember the functions themselves.
         delete shq.runOnload.run;    // And forget about this function too!
        }
// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", shq.runOnload.run, false);
else if (window.attachEvent) window.attachEvent("onload", shq.runOnload.run);
else window.onload = shq.runOnload.run;
/* runOnload.js end*/

shq.runOnload(function(){alert(1)})// store event1
shq.runOnload(function(){alert(2)})// store event2
shq.runOnload(function(){alert(3)})// sotre event3
//-->
</script>

<input type="button" onclick="shq.runOnload(function(){alert(4)})" value="Run_event_new">
</body>
</html>

TOP

发新话题