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

Javascript not working within XSLT

Status
Not open for further replies.

holycow21

Programmer
Aug 30, 2005
2
US
I have an XSLT doc that is used to transform an XML doc through a transform call from an ASP page, with the output being written to client with Response.Write. The Javascript has a function with a loop in it (using "<"); so I have to wrap that script block inside a CDATA to make XSLT happy.

At the bottom of the XSLT doc, that function is called (after writing out a bunch of HTML); but nothing happens. It's as if Javascript can't find the function embedded in the CDATA and just gives up.

Any ideas on how I can get this to work? Your help would be greatly appreciated.

Brian
 
Without seeing your code, all I can suggest is that you are trying to run the javascript before the page has finished loading. Instead of running the javascript as you load the page, run the javascript using an onload event on the document.

One way to do this is to add the following in your block of javascript:
Code:
<script type="text/javascript">
...
function runMeOnceThePageIsLoaded() {
  // this is a function
  alert('Test');
}

window.onload = runMeOnceThePageIsLoaded;
...
</script>
Or you could write it in the body tag:
Code:
<body onload="runMeOnceThePageIsLoaded()">
Just a hunch.

Assuming that is no use to you... try running your page in Firefox and look for a javascript error in the javascript console.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
You could put your Javascript code in an external file, then you won't have to worry about < and > conflicts anymore.
 
Thanks for the responses. I put the script in an external file (as suggested) to resolve my XSLT problem (it was interpreting the '<' as part of an XML tag, causing it to think the document was not well formed).

My page still does not load properly (acts like it isn't executing my function).

Here is a more extensive description to my problem.

ASP code:
Code:
Response.Write(source.transformNode(style))

note:source is XML loaded into DOM, style is XSLT loaded into DOM

XSLT includes javascript function that interacts with Microsoft Office Web Components (chart object). This is the javascript that I've moved to an external file.

Following the javascript, the XSLT file includes alot of HTML, including the following used to define a chart object:

Code:
<td width="35%" height="32"><div align="center">chart heading1<br />chart heading2</div><OBJECT CLASSID="clsid:0002E546-0000-0000-C000-000000000046" height="175" width="100%" id="chart1" border="0" style="border:0px;"><SPAN STYLE="color:red">ActiveX control failed to load! -- Please check browser security settings.</SPAN></OBJECT><div align="center">chart heading 3<br />chart heading 4</div></td>

Finally, following the HTML code but still within the body, another script block is included in the XSLT to call the prior defined javascript function (one located in the external file) so that the chart is drawn before the page displays.

What appears to be happening is that the HTML renders fine, but the final script block does not execute; and as a result, I just get a Microsoft OWC image located where my chart should be.

One suggestion, from above, is to add an onload event to body. I tried doing that - having it call the function at load time. Unfortunately, that didn't work either. I'm guessing that is due to the chart object not being created until after HTML is rendered (or the external javascript file not yet being loaded).

Any other suggestions? Thanks again.
Brian
 
You cannot perform (reliable) interaction with an HTML page until the entire HTML page has finished loading. If you have javascript enabled then this will trigger an onload event. When that event has been triggered, the browser has determined that all the content has rendered (as delivered) and the DOM is ready for access. If you attempt to run any javascript (that may access the DOM or page content) before this event has fired, then you will get unexpected results.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top