Jump to content

Problem Converting Client-side to Server-side Code


kwilliams

Recommended Posts

I'm trying to convert code that was originally client-side into server-side code. It pulls data from several XML feeds, and then uses variables from that data in a news scroller. When client-side with static variables, the data scrolls every 5 seconds. But the server-side example doesn't scroll at all.Almost everything works fine, except for the setTimeout. When I load the code listed below, I receive this error:Microsoft JScript runtime error '800a1391''window' is undefined/scroller.asp, line 173I realize that 'window' is a browser object that cannot be used in server-side code, but I'm not sure how to adjust my code to make it work. If anyone could let me know what I'm doing wrong, that would be great. Thanks.

<%var pausecontent=new Array()		var strMessage1 = "This is message 1";		var strMessage2 = "This is message 2";		var strMessage3 = "This is message 3";		pausecontent[0]='<strong>Title 1: ' + strMessage1 + '</strong>'		pausecontent[1]='<strong>Title 2: ' + strMessage2 + '</strong>'		pausecontent[2]='<strong>Title 3: ' + strMessage3 + '</strong>'		/***********************************************		* Pausing up-down scroller- © Dynamic Drive (www.dynamicdrive.com)		* This notice MUST stay intact for legal use		* Visit http://www.dynamicdrive.com/ for this script and 100s more.		***********************************************/				function pausescroller(content, divId, divClass, delay){		this.content=content //message array content		this.tickerid=divId //ID of ticker div to display information		this.delay=delay //Delay between msg change, in miliseconds.		this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)		this.hiddendivpointer=1 //index of message array for hidden div		Response.Write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>')		var scrollerinstance=this		//*****************THIS IS WHERE I'M HAVING THE PROBLEM****************************		if (window.addEventListener) //run onload in DOM2 browsers		window.addEventListener("load", function(){scrollerinstance.initialize()}, false)		else if (window.attachEvent) //run onload in IE5.5+		window.attachEvent("onload", function(){scrollerinstance.initialize()})		else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec		setTimeout(function(){scrollerinstance.initialize()}, 500)		}				// -------------------------------------------------------------------		// initialize()- Initialize scroller method.		// -Get div objects, set initial positions, start up down animation		// -------------------------------------------------------------------				pausescroller.prototype.initialize=function(){		this.tickerdiv=document.getElementById(this.tickerid)		this.visiblediv=document.getElementById(this.tickerid+"1")		this.hiddendiv=document.getElementById(this.tickerid+"2")		this.visibledivtop=parseInt(pausescroller.getCSSpadding(this.tickerdiv))		//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)		this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"		this.getinline(this.visiblediv, this.hiddendiv)		this.hiddendiv.style.visibility="visible"		var scrollerinstance=this		document.getElementById(this.tickerid).onmouseover=function(){scrollerinstance.mouseoverBol=1}		document.getElementById(this.tickerid).onmouseout=function(){scrollerinstance.mouseoverBol=0}		if (window.attachEvent) //Clean up loose references in IE		window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})		setTimeout(function(){scrollerinstance.animateup()}, this.delay)		}						// -------------------------------------------------------------------		// animateup()- Move the two inner divs of the scroller up and in sync		// -------------------------------------------------------------------				pausescroller.prototype.animateup=function(){		var scrollerinstance=this		if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){		this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"		this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"		setTimeout(function(){scrollerinstance.animateup()}, 50)		}		else{		this.getinline(this.hiddendiv, this.visiblediv)		this.swapdivs()		setTimeout(function(){scrollerinstance.setmessage()}, this.delay)		}		}				// -------------------------------------------------------------------		// swapdivs()- Swap between which is the visible and which is the hidden div		// -------------------------------------------------------------------				pausescroller.prototype.swapdivs=function(){		var tempcontainer=this.visiblediv		this.visiblediv=this.hiddendiv		this.hiddendiv=tempcontainer		}				pausescroller.prototype.getinline=function(div1, div2){		div1.style.top=this.visibledivtop+"px"		div2.style.top=Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"		}				// -------------------------------------------------------------------		// setmessage()- Populate the hidden div with the next message before it's visible		// -------------------------------------------------------------------				pausescroller.prototype.setmessage=function(){		var scrollerinstance=this		if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)		setTimeout(function(){scrollerinstance.setmessage()}, 100)		else{		var i=this.hiddendivpointer		var ceiling=this.content.length		this.hiddendivpointer=(i+1>ceiling-1)? 0 : i+1		this.hiddendiv.innerHTML=this.content[this.hiddendivpointer]		this.animateup()		}		}				pausescroller.getCSSpadding=function(tickerobj){ //get CSS padding value, if any		if (tickerobj.currentStyle)		return tickerobj.currentStyle["paddingTop"]		else if (window.getComputedStyle) //if DOM2		return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")		else		return 0		}%>

Link to comment
Share on other sites

I'm trying to convert code that was originally client-side into server-side code. It pulls data from several XML feeds, and then uses variables from that data in a news scroller. When client-side with static variables, the data scrolls every 5 seconds. But the server-side example doesn't scroll at all.Almost everything works fine, except for the setTimeout. When I load the code listed below, I receive this error:Microsoft JScript runtime error '800a1391''window' is undefined/scroller.asp, line 173I realize that 'window' is a browser object that cannot be used in server-side code, but I'm not sure how to adjust my code to make it work. If anyone could let me know what I'm doing wrong, that would be great. Thanks....
I think you'll need to leave that part as client-side script. :) Basically you are not going to be able to do that in server-side script. The browser content is only going to change in response to user actions, client script actions, and server responses to browser requests.Have you looked at RSS at all? That might be very relevant since you want to publish xml news feeds.
Link to comment
Share on other sites

I think you'll need to leave that part as client-side script. :) Basically you are not going to be able to do that in server-side script. The browser content is only going to change in response to user actions, client script actions, and server responses to browser requests.Have you looked at RSS at all? That might be very relevant since you want to publish xml news feeds.
All the feeds I'm using are XML feeds vs. RSS feeds, and I'm also wanting to use some internal XML docs for those feeds. I tried to find a way to parse an XML doc client-side, but I can't find any source code. I can only find server-side solutions. Do you have any suggestions of where I could find them?
Link to comment
Share on other sites

JavaScript has XML functions - there is a good-looking tutorial at http://www.devarticles.com/c/a/JavaScript/...Script-and-XML/
I copied and pasted the code from this article, but nothing happened. Is there some simple example code that shows how this can work?
I like this code, as it has a crossbrowser example. It works great at loading an internal XML doc, but it doesn't work on an external XML doc. Is there a way to make this code work with an external XML feed?
Link to comment
Share on other sites

A common omission when writing server side XML DOM processing code that retreives external XML sources is to leave out the ServerHTTPRequest property. I don't know if this is the case as you didn't show your code. But if it works with a local file but not a remote one, it could be xmlDoc.setProperty("ServerHTTPRequest", true) <!-- You need this to get a remote XML feed -->

Link to comment
Share on other sites

A common omission when writing server side XML DOM processing code that retreives external XML sources is to leave out the ServerHTTPRequest property. I don't know if this is the case as you didn't show your code. But if it works with a local file but not a remote one, it could be xmlDoc.setProperty("ServerHTTPRequest", true) <!-- You need this to get a remote XML feed -->
I added your referenced line of code to my code, but it's still not working. I noticed that you mentioned server-side code, so I'm assuming that's probably why. Using the example from http://www.w3schools.com/dom/dom_examples.asp and your referenced line of code, I created the following code that works great on a local XML doc, but not on a remote XML feed. Here is my complete code:
<%@LANGUAGE="JAVASCRIPT"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">	<title>XML  Feed Test</title>	<script language="javascript">		var xmlDoc;		function loadXML()		{		//load xml file		// code for IE		if (window.ActiveXObject)		{			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");			xmlDoc.setProperty("ServerHTTPRequest", true) <!-- You need this to get a remote XML feed -->			xmlDoc.async=false;			xmlDoc.load("http://codeamber.org/a1xl04act/amberalert.xml");			getmessage();		}		// code for Mozilla, Firefox, Opera, etc.		else if (document.implementation && document.implementation.createDocument)		{			xmlDoc=document.implementation.createDocument("","",null);			xmlDoc.load("http://codeamber.org/a1xl04act/amberalert.xml");			xmlDoc.onload=getmessage;		}		else		{			alert('Your browser cannot handle this script');		}		}				function getmessage()		{			document.getElementById("Alertstatus").innerHTML=xmlDoc.getElementsByTagName("Alertstatus")[0].childNodes[0].nodeValue;			document.getElementById("FullText").innerHTML=xmlDoc.getElementsByTagName("FullText")[0].childNodes[0].nodeValue;			document.getElementById("Alertinfo").innerHTML=xmlDoc.getElementsByTagName("Alertinfo")[0].childNodes[0].nodeValue;			document.getElementById("AlertState").innerHTML=Alertinfo.getElementsByTagName("states")[0].childNodes[0].nodeValue;			document.getElementById("Victim").innerHTML=xmlDoc.getElementsByTagName("Victim")[0].childNodes[0].nodeValue;		}			</script></head><body onload="loadXML()"><h1>Pull XML Feed</h1><p><b>Alertstatus:</b> <span id="Alertstatus"></span><br /><b>FullText:</b> <span id="FullText"></span><br /><b>Alertinfo:</b> <span id="Alertinfo"></span><br /><b>AlertState:</b> <span id="AlertState"></span><br /><b>Victim:</b> <span id="Victim"></span></p></body></html>

Did I place that line of code in the correct place? If not, where should it be placed?This code needs to be client-side, as I need to use the results in another client-side script. I found a quick article on parsing XML w/JavaScript at http://www.captain.at/howto-ajax-xml-javascript.php, and it works great when using the example XML string. But how can I use this code with an external XML file instead of a string? Thanks for any help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...