Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random error in onload javascript functions

Status
Not open for further replies.

leearach2004

Technical User
Dec 8, 2005
86
GB
Hi there all, I have this problem that I have scoured the internet for but cannot fined any answers for.

I have created a web page that uses javascript to query a ms access database and return the required data to a web form (this is hidden).

I then have an embedded flash movie on the page. I have another javascript function that sends and recieves variables from the flash movie file.

The data from the hidden form is sent to a variable in the flash movie and displayed. This all works fine but occasionaly when it runs it displays an error saying

Document.getElementByID() is null or not an object

but if you close the browser and reopen it works fine and will carryon working fine for ages till randomly you open the .html file and it shows the error again

the code is shown below
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
<head>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<META HTTP-EQUIV="imagetoolbar" CONTENT="NO">

	<SCRIPT LANGUAGE=JavaScript>
	//load variable from html textarea to flash
	<!--
	function doPassVar(){
	   var sendText = form1.sendText.value;
	   window.document.myFlash.SetVariable("myVar", sendText);
	}
	//send a variable to flash
	function ReceiveDataFromFlashMovie()
	{
		var mess=myFlash.GetVariable("/:mess");
		window.document.form1.title.value=mess;
	}
	
	//-->
	</SCRIPT> 
 <script type="text/javascript">
  <!--
  
  //create databse connection and variables
  var adOpenDynamic = 2;
  var adLockOptimistic = 3;

  var conn_str = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=\\test\\fyptext.mdb";
  var conn = new   ActiveXObject("ADODB.Connection");
  conn.open(conn_str, "",   "");

  //open information from database
  function rs2arr(rs, arr) {
    for(var i=0; i!= rs.fields.count; ++i)
      arr[rs.fields(i).name] = rs.fields(i).value
  }

  //display main selection from database in a form
  function show_fyp() {
    // write the HTML for the form
    var html = ''
    html += '<p><div> <input type="hidden"  id="fyp_subject" READONLY class="fyp_title_desinfo"/></p>'
	document.getElementById("main").innerHTML = html
	//run database information collection function
	get_fyp();
  }
  
	
  
  //function to call information from database acording to selection required
   function get_fyp() {
	
      var data = new Array()
	  //select record number
   	  id = form1.title.value;
	  
	  //select record from database
      var rs = new ActiveXObject("ADODB.Recordset")
	  //' added for test not needed for numbers
      rs.open ("SELECT * FROM fyptext WHERE going ='" + id + "'",conn)

      rs2arr(rs, data)
      rs.close()
    

    // Read the resulting transaction state into the form
    document.getElementById("fyp_subject").value = data["subject"]
    document.getElementById("fyp_description").value = data["description"]
  }

  //-->
  </script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<title>javascript_to_flash</title>
  <link href="file:///D|/test/fyp.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
	background-color: #FF9900;
}
-->
</style></head>
<body oncontextmenu="return false;" onload="ReceiveDataFromFlashMovie(); show_fyp(); doPassVar(); " MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" RIGHTMARGIN="0" TOPMARGIN="0" BOTTOMMARGIN="0">
<form name="form1" id="form1" method="post" action="" >
  <input type="hidden" name="title" value-"" class="flash_vis">
  <input type="hidden" name="sendText" id="fyp_description" class="flash_vis">
</form>

<!--url's used in the movie-->
<!--text used in the movie-->
<p align="center">
  <!-- load flash embbeded object -->
  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="[URL unfurl="true"]http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"[/URL] id="myFlash" width="100%" height="100%" align="middle">
    <param name="allowScriptAccess" value="always" />
    <param name="movie" value="javascript_to_flash.swf" />
    <param name="quality" value="high" />
	<param name="menu" value="false" />
    <embed name="myFlash" src="javascript_to_flash.swf" quality="high" height="100%" width="100%" scale="exactfit" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="[URL unfurl="true"]http://www.macromedia.com/go/getflashplayer"[/URL] swLiveConnect="true"/>
  </object>
</p>

<div id="main"/>
<!-- activate flash script -->
<script type="text/javascript" src="flashfix.js"></script>
</body >
</html>

maybee i have done somthi g wrong with the order they initalise or somthing

hope somone can help me its so frustrating and i can not distribute if it falls to run one in every ten or so times

lee
 
Try putting an alert before the call to "get_fyp()" - it will tell you whether this is a timing issue or not (trying to access elements before they've been written to the page).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi there tanks for the reply

I think you are right the function somtimes loads quicker than the contents div tag MAIN, dont understand why as the <DIC Onload=""> is surposed to load functions after everything else on the page has but hey.

Ill try to rearange hoe the functions run and see if that helps.

lee
 
Hi I have tried re aranging when the functions run but didnt work

The element is created most of the time but occasionaly it does not work

anyone have any ideas see code in previous post

lee
 
hi dan yes i put the alert in where you said to return when an object was present.

That worked, but when the error shows up the alert was blank so there was no object when the function was run.

But i cant understand why this only happens occasionaly not all the time.

lee
 
Hi there i think i may have just hit on a patern the error is thrown on the first time it is run when you first turn the computer on, after that it works fine untill you restsrt the computer then it throughs the error again the first time and is fine after.

lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top