question about javascript

  • Thread starter Thread starter Steve B.
  • Start date Start date
S

Steve B.

Hello everybody

In a webpage, I use JS display data from an xml file and a xsl file:

var data = new ActiveXObject("Microsoft.XMLDOM");
data.async = false;
var dataUrl = "data.aspx";
data.load(dataUrl) ;
var transform = new ActiveXObject("Microsoft.XMLDOM");
transform.async = false ;
transform.load("discussion.xslt");

result.innerHTML = data.transformNode(transform);

result is a div.

Somewhere in my xsl file I have a script that is repeated many times:

<xsl:for-each select="m:Messages">
....sometext...
<script language="javascript">
alert('<xsl:value-of select="m:MessageID" />');
</script>
</xsl:for-each>

My problem is that the Javascript (alert) is not fired. I don't think the
problem is from my xsl file since the "sometext"actually appears in the
page.

I wonder, if Javascript rendered on the fly is executed. If it is, how can I
do a "execute scripts" or anything like this ?

Thanks !

Steve
 
Steve B. said:
Hello everybody

In a webpage, I use JS display data from an xml file and a xsl file:

var data = new ActiveXObject("Microsoft.XMLDOM");
data.async = false;
var dataUrl = "data.aspx";
data.load(dataUrl) ;
var transform = new ActiveXObject("Microsoft.XMLDOM");
transform.async = false ;
transform.load("discussion.xslt");

result.innerHTML = data.transformNode(transform);

result is a div.

Somewhere in my xsl file I have a script that is repeated many times:

<xsl:for-each select="m:Messages">
...sometext...
<script language="javascript">
alert('<xsl:value-of select="m:MessageID" />');
</script>
</xsl:for-each>

My problem is that the Javascript (alert) is not fired. I don't think the
problem is from my xsl file since the "sometext"actually appears in the
page.

I wonder, if Javascript rendered on the fly is executed. If it is, how can I
do a "execute scripts" or anything like this ?

Thanks !

Steve
I think the problem is that when loading initially inline script is
recognised and run, not when added as innerHTML. Although this is an
untested hypothesis you may need to start the alert boxes yourself. Can you
post a small sample of your source and what you want to be alerted?
 
I'm tring to build a little chat in asp.net.
Since postbacks are awfull for such scenarios, I want to downloads messages
and transforms them in html form, instead of reloading the whole page.

The asp.net codebehind only generates a xml file from a dataset (that's why
my data xml file is a .aspx page).
Here is my current version (I removed useless lines for easyier read)
----------------------------------------------------------------------------
-
default.aspx:
__________

<HTML>
<HEAD>
<title>default</title>
<script language="javascript">
<!--
function DoTransform()
{
var data = new ActiveXObject("Microsoft.XMLDOM");
data.async = false;
var dataUrl = "data.aspx";
var last = document.getElementById("last");
data.load(dataUrl) ;

var transform = new ActiveXObject("Microsoft.XMLDOM");
transform.async = false ;
transform.load("discussion.xslt");

result.innerHTML = data.transformNode(transform);
}

// -->
</script>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"
EnableViewState="False"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

<div id="result">
</div>

<script language="javascript">
<!--
DoTransform();
// -->
</script>
</form>
</body>
</HTML>
----------------------------------------------------------------------------
-
data.aspx (result sent to the client using dataset.writexml):
----------------------------------------------------------------------------
-
<?xml version="1.0" standalone="yes"?>
<MessagesDataSet xmlns="http://www.tempuri.org/MessagesDataSet.xsd">
<Messages>
<MessageID>9</MessageID>
<Author>steve</Author>
<Body>Bonjour</Body>
<TimeStamp>2004-08-11T16:46:52.2150393+02:00</TimeStamp>
</Messages>
......................................
......................................
</MessagesDataSet>
----------------------------------------------------------------------------
-
and my xslt file
----------------------------------------------------------------------------
-
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.tempuri.org/MessagesDataSet.xsd">
<xsl:template match="/m:MessagesDataSet">
<table border="1">
<tr>
<td>auto</td>
<td>body</td>
</tr>
<xsl:apply-templates />
</table>
<xsl:for-each select="m:Messages">
<script language="javascript">
alert('<xsl:value-of select="m:MessageID" />');
</script>
</xsl:for-each>
</xsl:template>
<xsl:template match="m:Messages">
<tr>
<td>
<b>
<xsl:call-template name="format-date">
<xsl:with-param name="date" select="m:TimeStamp" />
</xsl:call-template>
</b>
<xsl:value-of select="m:Author" />
</td>
<td>
<xsl:value-of select="m:Body" />
</td>
</tr>
</xsl:template>
<xsl:template name="format-date">
<xsl:param name="date">
<xsl:value-of select="." />
</xsl:param>
<xsl:variable name="YYYY" select='substring($date, 1, 4)' />
<xsl:variable name="YY" select='substring($date, 3, 2)' />
<xsl:variable name="MM" select='substring($date, 6, 2)' />
<xsl:variable name="hh" select='substring($date, 12, 2)' />
<xsl:variable name="mm" select='substring($date, 15, 2)' />
<xsl:variable name="ss" select='substring($date, 18, 2)' />
<xsl:value-of select="concat($hh, ':', $mm)" />
</xsl:template>
<xsl:template match="*" />
</xsl:stylesheet>
----------------------------------------------------------------------------
-


I do not want to keep the "alert" method, it was just for testing (it should
call another metod) if the JS is launched.


Thanks,
Steve
 
Steve B. said:
I'm tring to build a little chat in asp.net.
Since postbacks are awfull for such scenarios, I want to downloads messages
and transforms them in html form, instead of reloading the whole page.

The asp.net codebehind only generates a xml file from a dataset (that's why
my data xml file is a .aspx page).
Here is my current version (I removed useless lines for easyier read)
-------------------------------------------------------------------------- --
-
default.aspx:
__________

<HTML>
<HEAD>
<title>default</title>
<script language="javascript">
<!--
function DoTransform()
{
var data = new ActiveXObject("Microsoft.XMLDOM");
data.async = false;
var dataUrl = "data.aspx";
var last = document.getElementById("last");
data.load(dataUrl) ;

var transform = new ActiveXObject("Microsoft.XMLDOM");
transform.async = false ;
transform.load("discussion.xslt");

result.innerHTML = data.transformNode(transform);
}

// -->
</script>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" runat="server"
EnableViewState="False"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>

<div id="result">
</div>

<script language="javascript">
<!--
DoTransform();
// -->
</script>
</form>
</body>
</HTML>
-------------------------------------------------------------------------- --
-
data.aspx (result sent to the client using dataset.writexml):
-------------------------------------------------------------------------- --
-
<?xml version="1.0" standalone="yes"?>
<MessagesDataSet xmlns="http://www.tempuri.org/MessagesDataSet.xsd">
<Messages>
<MessageID>9</MessageID>
<Author>steve</Author>
<Body>Bonjour</Body>
<TimeStamp>2004-08-11T16:46:52.2150393+02:00</TimeStamp>
</Messages>
.....................................
.....................................
</MessagesDataSet>
-------------------------------------------------------------------------- --
-
and my xslt file
-------------------------------------------------------------------------- --
-
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.tempuri.org/MessagesDataSet.xsd">
<xsl:template match="/m:MessagesDataSet">
<table border="1">
<tr>
<td>auto</td>
<td>body</td>
</tr>
<xsl:apply-templates />
</table>
<xsl:for-each select="m:Messages">
<script language="javascript">
alert('<xsl:value-of select="m:MessageID" />');
</script>
</xsl:for-each>
</xsl:template>
<xsl:template match="m:Messages">
<tr>
<td>
<b>
<xsl:call-template name="format-date">
<xsl:with-param name="date" select="m:TimeStamp" />
</xsl:call-template>
</b>
<xsl:value-of select="m:Author" />
</td>
<td>
<xsl:value-of select="m:Body" />
</td>
</tr>
</xsl:template>
<xsl:template name="format-date">
<xsl:param name="date">
<xsl:value-of select="." />
</xsl:param>
<xsl:variable name="YYYY" select='substring($date, 1, 4)' />
<xsl:variable name="YY" select='substring($date, 3, 2)' />
<xsl:variable name="MM" select='substring($date, 6, 2)' />
<xsl:variable name="hh" select='substring($date, 12, 2)' />
<xsl:variable name="mm" select='substring($date, 15, 2)' />
<xsl:variable name="ss" select='substring($date, 18, 2)' />
<xsl:value-of select="concat($hh, ':', $mm)" />
</xsl:template>
<xsl:template match="*" />
</xsl:stylesheet>
-------------------------------------------------------------------------- --
-


I do not want to keep the "alert" method, it was just for testing (it should
call another metod) if the JS is launched.


Thanks,
Steve

Well you could try Martin's suggestion of adding the defer attribute but I
don't think this is intended for this purpose, why not call the method
manually after doing the transform:

DoTransform()
DoOtherMethod()
 
I need to set some variable using the script that is generated...

That's why calling another method is not possible.

The other way could be to generate <input type="hidden" id="field_N"
value="myValue" > and to use documet.getElementById and a while...

Thanks,
Steve
 
Not sure if these will help but here's what I've done ...

1. You can fire an event by - oHint.fireEvent("onclick");

2. Or the problem could be in innerHTML

My kickoof page looks like this:

<%@ LANGUAGE = Jscript %>
<%

var pageid = Request.QueryString("id") + "";
var courseid = Request.QueryString("course") + "";
var learnerIP = Request.ServerVariables("REMOTE_ADDR") + "";
var themeID = Request.QueryString("theme") + "";

if (Request.QueryString("snd").Count ==0) {
var withSound = 1;
}else{
var withSound = Request.QueryString("snd") + "";
}

var coursename = courseid + ".xml"
var sourcefile = Server.MapPath(coursename);
var stylefile = Server.MapPath("GPxml.xsl");

var xmlDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.3.0");
var xslSheet = new ActiveXObject("MSXML2.FreeThreadedDOMDocument.3.0");
var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate.3.0");
xmlDoc.async = false;
xmlDoc.load(sourcefile);

//Check for a successful load of the XML Document.

xslSheet.load(stylefile);
xslSheet.async = false;


xslTemplate.stylesheet = xslSheet;
var xslProcessor = xslTemplate.createProcessor();
xslProcessor.input = xmlDoc;

//Check for hi-contrast theme
var bgColor
if (themeID == 2) {
bgColor = "FFFFFF";
}else{
bgColor = "FFFFEE";
}

//see if we passed a theme to override the default course one
if (themeID > 0) {
themeID = "Theme" + themeID;
}else{
themeID = "Theme1"
}


xslProcessor.addParameter("id", pageid);
xslProcessor.addParameter("course", courseid);
xslProcessor.addParameter("ip", learnerIP);
xslProcessor.addParameter("themeID", themeID);
xslProcessor.addParameter("bgColorID", bgColor);
xslProcessor.addParameter("withSound", withSound);

xslProcessor.transform();
Response.Write(xslProcessor.output);

--------------------
and in the XSLT:

<xsl:variable name="Exception" select="concat('EXCEPTION(S): ', .)"/>

<div id="sndExceptions"
onactivate="document.oException.src='{$imgExceptionFocus}'"
ondeactivate="document.oException.src='{$imgException}'"
onClick="javascript:alert('{$Exception}');" >

.... div content ...

</div>
---------------------

3. The DEFER is a good idea see this example if within the XSLT file ...

<!--*******************TEST DISPLAY********************-->
<!--Use of DEFER forces client side processing-->
<SCRIPT LANGUAGE="javascript" DEFER="true">
<xsl:comment>
<![CDATA[

function msgTest() {
alert("TEST");
}
]]>
</xsl:comment>
</SCRIPT>


Hope it helps.
ed
 
I am not sure how you can achieve what you want to achieve your way. But why
not write a few more lines of script to access the xml dom 'data' in
DoTransform()?
 
Back
Top