Results 1 to 3 of 3

Thread: TypeError: document.getelementbyid(...) is null

  1. #1
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TypeError: document.getelementbyid(...) is null

    HTML Code:
    <html>
    
    	<head>
    		<title>Port Scanner for Mozilla Firefox</title>
    	</head>
    	
    	<body>
    
    		<form name="my_form" id="my_form" method="post" action="#" onsubmit="return displayMessage();">
    			<p align="center">URL/Domain Name/IP Address: <input type="text" size="20" name="uri" value="" placeholder="http://www.google.com" required/></p>
    			<table align="center" border="1" width=100% bgcolor="#f4fff0" bordercolor="green" cellspacing="2" cellpadding="2">
    				<tr>
    					<td align="center">
    						Start port: <input type="text" size="5" name="start_port" maxlength="5">
    						End port: <input type="text" size="5" name="end_port" maxlength="5"><br>
    						<input type="submit" value="Scan all ports" name="scan_all"><br>
    						<input type="submit" value="Scan only open ports" name="scan_open">						
    					</td>
    				
    				</tr>
    				<tr>
    					<td align="center">
    						<input type="text" size="20" name="scan_tcp"><br>
    						<font color="#b1b1b1">Enter port numbers separated by space. (e.g. 21 25 80)</font><br>
    						<input type="submit" value="Scan TCP ports" name="scan_tcp_ports">					
    					</td>
    				</tr>
    				<tr>
    					<td align="center">
    						<input type="submit" value="Scan common service ports" name="scan_common_tcp"><br>
    						<input type="submit" value="Scan common Trojan ports" name="scan_common_trojan_tcp">					
    					</td>
    				</tr>
    			</table>
    		</form>
    	    <script type="text/javascript">
    			function displayMessage()
    			{
    					if(document.my_form.uri.value==0)
    					{
    						document.write("<font color='red'><i>Please enter URL/Domain Name/IP Address.</i></font>");
    						return false;
    					}
    					document.write("<DIV id='loading'><BR><BR><font color='#FF6600'><i>Please wait... The ports are being scanned...</i></font></DIV>");
    					document.getElementById("my_form").submit();					
    			}
    		    window.onload=function()
    			{  
    				document.getElementById("loading").style.display="none";  
    		    }  
    		</script>
    	</body>
    </html>
    I want to submit above form. But it shows me JavaScript error in Console that
    "TypeError: document.getelementbyid(...) is null"
    This error is for code :
    document.getElementById("my_form").submit();

    I am using Firefox 24.0 and Windows 7. I have installed only 1 add-on. That is 'Firebug 1.12.3'.
    This code works fine for other browsers Except Firefox.
    Please help me to solve this problem.
    Last edited by jscheuer1; 10-13-2013 at 01:10 PM. Reason: Format

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    All browsers are generating that error. Of the browsers that report errors in a console, only Firefox preserves previous errors upon navigation by default. Chrome can be toggled but defaults to a fresh console window upon navigation. Other browsers may or may not be able to be toggled in this manner. Navigation occurs when the form is submitted.

    The form submits because the form has:

    Code:
     onsubmit="return displayMessage();"
    Which means it will submit unless displayMessage returns false.

    The displayMessage() function always returns undefined, as it has no return value set, except for this section:

    Code:
    					if(document.my_form.uri.value==0)
    					{
    						document.write("<font color='red'><i>Please enter URL/Domain Name/IP Address.</i></font>");
    						return false;
    					}
    Which only fires in older browsers that do not recognize the required attribute of the uri input element. Modern browsers will not submit the form because of that if the uri field is not filled in.

    If that field is filled out, then this happens:

    Code:
    					document.write("<DIV id='loading'><BR><BR><font color='#FF6600'><i>Please wait... The ports are being scanned...</i></font></DIV>");
    					document.getElementById("my_form").submit();
    When you document.write after a page is already loaded (which this page must be, because otherwise the form wouldn't be there), it wipes out the existing page and replaces it with whatever is written. So, although in some browsers the form submits because the form is in memory and this function returns undefined, by the time the code gets to:

    Code:
    document.getElementById("my_form").submit();
    "my_form" is not there any more, it has been overwritten. Hence the error you are seeing. In Firefox though the form is not in memory so does not submit, so all you see is the loading message and the error.

    I think I see what you want it to do though. When I have some more time I may try rewriting it to do that. Or you could just use a standard form validation script. There are many available around the web.

    Just to get it working, replace the script with:

    Code:
    	    <script type="text/javascript">
    			function displayMessage()
    			{
    					if(document.forms.my_form.uri.value==0)
    					{
    						alert("Please enter URL/Domain Name/IP Address.");
    						document.forms.my_form.uri.focus();
    						return false;
    					}
    					alert("The ports will be scanned when you dismiss this alert.");
    					return true;				
    			}
    		</script>
    The browser cache may need to be cleared and/or the page refreshed to see changes.
    Last edited by jscheuer1; 10-13-2013 at 02:12 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    This error TypeError: document.getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag.

Similar Threads

  1. document.getElementById
    By locbtran in forum JavaScript
    Replies: 5
    Last Post: 05-28-2012, 09:10 AM
  2. Resolved document.getElementByID is null... with dropdown tabs
    By mikeusru in forum Dynamic Drive scripts help
    Replies: 7
    Last Post: 01-06-2009, 02:42 AM
  3. document.getElementById problem
    By yoyoyo in forum JavaScript
    Replies: 5
    Last Post: 12-23-2008, 07:00 AM
  4. document.getElementById(variable)
    By city_coder in forum JavaScript
    Replies: 2
    Last Post: 06-17-2008, 01:28 PM
  5. getElementById return null in FF
    By onurtpl in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-08-2006, 06:54 AM

Bookmarks

Posting Permissions

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