#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14

    Javascript: object not recognized, even though it's clearly there


    The error is 'document.callentry.whoathere is not an object'

    Code:
    <html><head><title></title></head>
    <body>
    <p>
    	<SCRIPT LANGUAGE="JavaScript">
    	<!-- Beginning of JavaScript --------
    
    	var strMsg = 'Leaving this page without using the Submit button will cause your narritave to be lost.' 
    	if (document.whoathere.whoathere.value==0) {
    		window.event.returnValue = strMsg; 
    	} 
    	
    	// -- End of JavaScript code -------------- -->
    	</SCRIPT>
    	<form name="callentry" method="post" action="process_callentry.phtml" style="margin:0px">
    	<input type="hidden" name="fnum" value="p9999998">
    	<input type="text" name="whoathere" value="0">
    	<input type=submit value='Submit' onClick="document.callentry.whoathere.value=1;"></form>
    	</form>
    </body></html>
    I've tried replacing the form name with form[0] but the same error appeared (documet.forms.0.whoathere is not an object).

    Any clues?
  2. #2
  3. T-Shirt Tragic
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    886
    Rep Power
    321
    The browser reads the file line by line the same way a human does.. so it was executing the javascript code before your form objects were actually created.. sooooo.. at the time the browser was parsing the "whoathere" thingo it actually didn't exist!

    try this-
    Code:
    <html>
    <head>
    <title></title>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    
    var strMsg = 'Leaving this page without using the Submit button will cause your narritave to be lost.';
    function preprocess_form() {
        
        if (document.whoathere.whoathere.value==0) {
    	window.event.returnValue = strMsg; 
        } 
    }
    	
    // -->
    </SCRIPT>
    
    </head>
    <body>
    
    <form name="callentry" method="post" action="process_callentry.phtml" style="margin:0px">
    <input type="hidden" name="fnum" value="p9999998">
    <input type="text" name="whoathere" value="0">
    <input type="submit" value="Submit" onClick="document.callentry.whoathere.value=1;preprocess_form()">
    </form>
    
    </body>
    </html>
  4. #3
  5. No Profile Picture
    OpenBSD fella
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    Chile, South America
    Posts
    11
    Rep Power
    0
    Jonathon is right, and...

    Code:
    <html><head><title></title></head>
    <body>
    <p>
    	<form name="callentry" method="post" action="process_callentry.phtml" style="margin:0px">
    	<input type="hidden" name="fnum" value="p9999998">
    	<input type="text" name="whoathere" value="0">
    	<input type=submit value='Submit' onClick="document.forms[0].whoathere.value=1;"></form>
    	</form>
    	<SCRIPT LANGUAGE="JavaScript">
    	<!-- Beginning of JavaScript --------
    
    	var strMsg = 'Leaving this page without using the Submit button will cause your narritave to be lost.' 
    	if (document.forms[0].whoathere.value==0) {
    		window.event.returnValue = strMsg; 
    	} 
    	
    	// -- End of JavaScript code -------------- -->
    	</SCRIPT>
    </body></html>
    That works for me, except the window.event thingie (doesnt work in NS4)

    .pd
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14
    Pda0, your example returns this error: 'window.event is not an object'. It was a good idea, but I'm not sure why that error appears. (btw, i'm using ie 5x)

    Jonathon, your solution does not produce any error, however the script is rendered non-functional.

    The idea here is to check the current value of whoathere anytime a window event occurs (closed via the X, alt-f4, click a link to go to another page, whatever). The only time whoathere is updated to a 'safe' number, that being 1, is when the user clicks the submit button or submits the forum by pressing enter in any of the textboxes.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Norcross, GA
    Posts
    458
    Rep Power
    14
    Found the problem.

    <SCRIPT LANGUAGE=JavaScript FOR=window EVENT=onbeforeunload>

    Before I just had <SCRIPT LANGUAGE=JavaScript>.

    Works like a charm now (with my original code example).

IMN logo majestic logo threadwatch logo seochat tools logo