Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript objects - out of scope on onbeforeunload

    This is a bit of a strange one. I have been trying to call a function in a child object from the parent object but the child seems to be going out of scope in onbeforeunload function. These function calls work outside of a onbeforeunload, so it only fails when called in onbeforeunload. I can fix it by making the child object a global but I was trying to avoid that. Anyone know why my childobject is out of scope when called in onbeforeunload? Notice I call that same function in windows onload event and it works fine. Here is the code (I have simplified as much as possible to just show the error):

    Code:
    <html>
       
       <head>
       <title>Broken Page</title>    
    	  
    <script language="javascript" type="text/javascript">
    	
    	var myparent;
    	
    	function windowLaunch()
    	{
    		myparent = new parent();
    		myparent.getchildvalue();
    		window.onbeforeunload = myparent.getchildvalue;
    	}
    
    	function parent()
    	{
    		this.mychild = new childobject("myinput");
    		this.getchildvalue = function()
    		{
    			var tmpval = this.mychild.returnvalue();
    		};
    
    	}
    
    	function childobject(thename)
    	{
    		this.myprop = thename;
    		this.returnvalue = function()
    		{
    			return (document.getElementById(this.myprop).value);
    		};
    	}
    	
    </script>
       </head>
       
       <body id="thebody" onload="windowLaunch();">
        <div id="outerdiv">
                <span title="This Input Box">My Input:</span><br />
                <input id="myinput" style="width: 290px"/>
        </div>
       </body>
       
    </html>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    function windowLaunch()
    	{
    		myparent = new parent();
    		myparent.getchildvalue();
    		window.onbeforeunload = function(){myparent.getchildvalue()};
    	}

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,344
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by toekneeh View Post
    I have been trying to call a function in a child object from the parent object but the child seems to be going out of scope in onbeforeunload function.
    that’s because event handlers re-set the scope to the object the handler is attached to (in your case: window).

    that’s the same for setTimeout(), setInterval() & addEventListener()

    another option is using Function.bind() (where supported)
    PHP Code:
        function windowLaunch()
        {
            
    myparent = new parent();
            
    myparent.getchildvalue();
            
    window.onbeforeunload myparent.getchildvalue.bind(myparent);
        } 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

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