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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null or not an object 1

Status
Not open for further replies.

Delameko

Programmer
Oct 14, 2003
37
GB
I'm getting this error:
'document.getElementsByTagName(...).0' is null or not an object - but only when its on the server. It works fine when I'm running it off the harddisk.

I found lots of other posts about this but none of their solutions worked for me.

When you click the button it parses an xml file, formats it with an xsl then places it into the Popup.html. Like I said it works when loading off the harddrive.

Code:
        <script language="JScript" defer="true">
        
	
            function verify()
				{
  					if (xml.readyState != 4)
  					{
      				return false;
  					}
				}

            
            xml.load("Cinema.xml");
            
            function showCinema(venueID)
            {
            	var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
            	xml.async = false;
            	xml.onreadystatechange=verify;
				var xsl = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
				xsl.async = false;
   				xsl.load("Cinema.xsl");
				var temp = new ActiveXObject("MSXML2.XSLTemplate");
    			temp.stylesheet = xsl;
    			var proc = temp.createProcessor();
    			proc.addParameter("venueID", venueID);
    			proc.input = xml;
    			proc.transform();
    			var str = proc.output;
    			var mypop=window.open('Popup.html','newwin','scrollbars=yes,menubar=no,height=600,width=450,resizable=no,toolbar=no,location=no,status=no');
    			mypop.document.getElementsByTagName('body')[0].innerHTML = str;
			}
           
        </script>

The Popup.html has only html, head and body tags in.
 
Your problem is to do with timing. You are trying to use getElementsByTagName before the file has loaded, I'll wager.

Use a callback function instead to avoid this.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hmm.. in case you do not know what a callback function is... on your page, above, have a function:

Code:
function myCallBackFunc() {
   mypop.document.getElementsByTagName('body')[0].innerHTML = str;
}

and in the body of popup.html, utilise the onload event handler:

Code:
<body onload="window.opener.myCallBackFunc();">

You'll have to make "str" and "mypop" global variables for that to work, or modify your code in other ways.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for pointing me in the right direction, Dan. I've got it sorted now (with the help of another programmer).


For completions sake (incase anyone is reading this from the search) the final code looks like this:

Code:
        <script language="JScript" defer="true">
            
            function verify()
				{
  					if (xml.readyState != 4)
  					{
      				return false;
  					}
				}

            var mypop;
            var str;
            var xml = new ActiveXObject("Msxml2.DOMDocument.3.0");
            xml.async = false;
            xml.onreadystatechange=verify;
            xml.load("Cinema.xml");

            function showCinema(venueID)
            {
			    var xsl = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
			    xsl.async = false;
   				xsl.load("Cinema.xsl");
			    var temp = new ActiveXObject("MSXML2.XSLTemplate");
    			temp.stylesheet = xsl;
    			var proc = temp.createProcessor();
    			proc.addParameter("venueID", venueID);
    			proc.input = xml;
    			proc.transform();
    			str = proc.output;
    			mypop=window.open('Popup.html','newwin','scrollbars=yes,menubar=no,height=600,width=450,resizable=no,toolbar=no,location=no,status=no');
			}

            function myCallBackFunc()
            {
				return str;
			}
			
        </script>

And in Popup.html:
Code:
<script language="javascript">
function getContent()
{
	var str1;
	str1 = window.opener.myCallBackFunc();
	var oVDiv = document.getElementById("oDiv1");
	oVDiv.innerHTML = str1;
}            
</script>
</head>

<body onload="getContent();">

<div id=oDiv1>

</div>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top