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

Only works if I use alert() - timing problem??? 1

Status
Not open for further replies.

arst06d

Programmer
Joined
Nov 29, 2002
Messages
324

Hi

I need to print one or more access report in snapshot (.snp) format. To do this I build the Object and write it to the document. I I set the height & width then it displays fine.

When I come to print it, I write the Object, grab the newly written object and try to call its print method. I get a javascript error saying "Missing or Invalid snapshot file". Now we know this is not true, as we can see the report displayed in the viewer.

I think it's possibly a timing issue - if I put an alert in after I grab the new object and before I call the print method then it works fine. Without the alert - error.

I've tried putting in pauses using the function shown but still the same.

This is driving me mad - can anyone enlighten me?



Code:
function printReport(strReport)
{
	var strObj;

	//alert(strReport);

	strObj = "<OBJECT id=SnapshotViewer style='LEFT: 0px; TOP: 0px' height='0%' width='0%' classid='CLSID:F0E42D60-368C-11D0-AD81-00A0C90DC8D9' VIEWASTEXT>";
	strObj = strObj + "<PARAM NAME='_ExtentX' VALUE='16933'>";
	strObj = strObj + "<PARAM NAME='_ExtentY' VALUE='12700'>";
	strObj = strObj + "<PARAM NAME='_Version' VALUE='65536'>";
	strObj = strObj + "<PARAM NAME='SnapshotPath' VALUE='" + strReport + "'>";
	strObj = strObj + "<PARAM NAME='Zoom' VALUE='4'>";
	strObj = strObj + "<PARAM NAME='AllowContextMenu' VALUE='-1'>";
	strObj = strObj + "<PARAM NAME='ShowNavigationButtons' VALUE='-1'></OBJECT>";
	
	var snpDiv = document.getElementById("snapshotDiv");
	snpDiv.innerHTML = strObj;
	//pausecomp(2000);
	var snp = document.getElementById("SnapshotViewer");
	//pausecomp(1000);
	//alert(snp);

	snp.PrintSnapshot(false);
}

function pausecomp(millis) 
{
	date = new Date();
	var curDate = null;

	do { var curDate = new Date(); } 
	while(curDate-date < millis);
} 
</script>
 
Thanks BabyJeffy

Had to alter it a bit 'cos was getting "snp is undefined"

Code:
	setTimeout('printit()',500);
	
}

function printit(snp)
{
	var snp = document.getElementById("SnapshotViewer");
	snp.PrintSnapshot(false);
}
 
well it is working - for individual reports. However, when I want to print a series of reports it only ever prints the last one x times.

Reports are in a <SELECT> list, and I cycle through all the option values if "ALL" reports are required.

I've tried using the setTimeout() after each call to print, calling a function to reset the innerHTML of the div - no joy.

Code:
<script language="javascript">

 function showReport(strReport, blnPrint)
 {
	var cbo = document.getElementById(strReport);
			
	//get the folder
	var dt = document.getElementById("cboDate").value;
	var fldr = strReport.substr(3);
	var strF = '[URL unfurl="true"]http://apollo04.shb.co.uk/david/snapshots/Reports/'+[/URL] dt + '/' + fldr + '/';
	var strF2="";
	
	//Open all reports or a specific one?
	if(cbo.value=="All")
	{
		//loop through the <options> of the <select>, open/print each report.
		//start looping at i=1 cos the first option will be "All" reports
		var opts = cbo.options;
		for(var i=1; i<opts.length;i++)
		{
			strF2 = strF + opts[i].value + '.snp';
			if(blnPrint)
			{
				printReport(strF2);				
			}
			else
			{			
				window.open('snapshot.asp?rpt=' + strF2,"_blank");
			}
		}
	}
	else  //Open/print the specific report
	{
		strF2 = strF + cbo.value + '.snp';
		if(blnPrint)
		{
			printReport(strF2);			
		}
		else
		{
			window.open('snapshot.asp?rpt=' + strF2,"_blank");
		}
	}
 
 }


function printReport(strReport)
{
	var strObj;

	//alert(strReport);

	strObj = "<OBJECT id='SnapshotViewer' style='LEFT: 0px; TOP: 0px' height='0%' width='0%' classid='CLSID:F0E42D60-368C-11D0-AD81-00A0C90DC8D9' VIEWASTEXT>";
	strObj = strObj + "<PARAM NAME='_ExtentX' VALUE='16933'>";
	strObj = strObj + "<PARAM NAME='_ExtentY' VALUE='12700'>";
	strObj = strObj + "<PARAM NAME='_Version' VALUE='65536'>";
	strObj = strObj + "<PARAM NAME='SnapshotPath' VALUE='" + strReport + "'>";
	strObj = strObj + "<PARAM NAME='Zoom' VALUE='4'>";
	strObj = strObj + "<PARAM NAME='AllowContextMenu' VALUE='-1'>";
	strObj = strObj + "<PARAM NAME='ShowNavigationButtons' VALUE='-1'></OBJECT>";
	
	var snpDiv = document.getElementById("snapshotDiv");
	snpDiv.innerHTML = strObj;
	
	//if I try to grab the newly created viewer object and call its print method
	// it fails - timing issue - has to catch up with itself.
	//tried pauses etc but dont work if within this same function
	//therefore:
	setTimeout('printit()',500);
}

function printit()
{
	var snp = document.getElementById("SnapshotViewer");
	snp.PrintSnapshot(false);
	
}

</script>
 
You'd need to wait until the report has finished printing before firing off the next... so you'd need to find out of the object you're using can call a function on the page, or have some other method (maybe a flag) that can indicate when it has finished printing.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I couldn't figure out a way to pause execution. Tried all sorts of pauses, setting and changing page variables and waiting for the value to change before proceeding etc. Just prompted a message saying a script was holding up execution - not something I want users to see.

Worked around this by looping through the reports, opening each in a new window. In that window, use blur() to send it to background, and setTimeout() to call a function to print, then another setTimeout to close the window after a short delay.

Not elegant, but works & I have a headache & wanna go home ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top