Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include Javascript in xml-document?

Tags:

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?

like image 850
snitko Avatar asked Dec 21 '08 16:12

snitko


People also ask

Can you add JavaScript to XML?

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.

Does JavaScript work with XML?

XML parsing in JavaScript is defined as it is one kind of package or library of the software which provides an interface for the client applications to work with an XML document and it is used in JavaScript to transform the XML document into readable form, nowadays in many browsers, the XML parser is already available ...

How do you add JavaScript to a file?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Where do you insert a JavaScript?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.


2 Answers

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/

like image 55
Tim Babych Avatar answered Sep 28 '22 02:09

Tim Babych


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.

like image 31
Ady Avatar answered Sep 28 '22 02:09

Ady