tags:

views:

3192

answers:

4

The task seems to be pretty easy: how to include a Javascript file in xml-document so that at least Opera and Firefox could actually parse it and execute the code?

A: 

A function that should help you is the eval() function. This function will evaluate any string you pass to it as Javascript.

Also, it's possible to parse XML in Javascript. Just google "javascript xml parser".

Combine these two ideas, and you'll be on your way.


If you simply want to put javascript in the XML file:

<xml> <js script="1"> here is some javascript; here is more javascript; </js> <js script="2"> here is even more javascript; jere is even more javascript; </js> </xml>

stalepretzel
Please read the question again. I wasn't asking to how to parse javascript from javascript, or xml from javascript.
snitko
A reasonable answer given the poor quality of the original question.
AnthonyWJones
<js script="file.js"/> is not executed by the browser.
snitko
Could you please restate your question more clearly?
stalepretzel
Okay, let me try it this way: I want Firefox to load xml-file, then parse associated with it javascript file and execute it's code.
snitko
+4  A: 

If I get you, you want an XML document to run javascript when viewed in a browser?

This is not part of the XML standard, and as such will not be suppoted until it is (I assume this will never be supported because XML is not intended for display, but data). If you are talking about XHTML then this is a different matter.

--

Edit: just to clarify my answer.

XML was never intended to be a display markup like HTML, thats why XHTML was developed (HTML that conforms to XML standards). Browsers have been made to interpret XHTML in a certain way, but XML is simply raw data.

If you want your XML to run additions such as JavaScript you will want to consider using XSLT to transform your XML into XHTML and therefore take advantage of a browsers capabilities.

Ady
Yes, you got me right, I'm talking about xml, not xhtml. And I don't care about standards, just actual browser capabilities. They do support <?xml-stylesheet type="text/css" href="style.css"?>, I thought something similar should be for Javascript.
snitko
I think you need to consider what XML was intended for. You could use XSLT to transform the XML into XHTML and thefore open the page as an HTML document, with all the javascript you want to run.
Ady
xhtml is just a special case of xml. Now what makes xml bad for running javascript? Theoretically, js has nothing to do with what xml was designed for, I don't see any connection and I don't see any reasons to restrict js running for xml document.
snitko
@snitko - well the main would be that no browser would comprehend it as display markup, and to get something basic like events working you'd have to define so much extension that you may as well be using xhtml which handily already defines these things
annakata
A: 

Embed the XML and the Javascript in an XHTML document and then use the vast and well-documented capabilities of dynamic HTML.

You'll get something up and running much faster than you will by reasoning that since some browsers implement weak and poorly-documented CSS styling of XML documents, therefore they must support the execution of Javascript embedded in XML, as though there were any connection whatsoever between those two ideas.

Robert Rossney
+1  A: 

Just add a script stating XHTML namespace, it will run just fine.

<xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
                src="file.js"
                type="application/javascript"/>

See also http://www.ibm.com/developerworks/xml/library/x-ffox3/

Tim Babych