开发者

Can I start up javascript from XML in the browser without HTML?

I've been tinkering with XML on the web and can use javascript if I add开发者_如何学JAVA HTML's script element but was interested in knowing if javascript can be started up for scripting the DOM without using anything from HTML at all?


It sounds to me like you're interested in using JavaScript in the browser while displaying XML content without reformatting the XML as HTML? If this is correct, you can add an XML stylesheet declaration to your XML file to declare an XSLT for the XML document. This declaration can remain unused in any other context but in a context where the client understands the attribute (ie. a Web browser) the XSLT will be used to transform the XML before displaying it. (Note: Some plder Web browsers do not support XSLT.) In this way you can inject JavaScript for a Web browser client.

See wikipedia for an overview of XSLT. There are many lessons available online to help you get started with XSLT.


Firstly, you're not technically adding HTML's script element, you're adding XHTML's script element - you can't add HTML to an XML document. XHTML is XML, so it's still a pure xml document.

You could theoretically run javascript in any xml file, as long as you have some way to tell the browser "this is a script - please run it". As of now, the script tag is the only way to tell browsers to do this - and the script tag is present in two different XML languages - XHTML and SVG.

You could do:


<root xmlns="http://non.existe.ent/namespace">
  <script xmlns="http://www.w3.org/2000/svg"><![CDATA[
    alert('hello');
  ]]></script>
</root>

Which has no xhtml at all in it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜