在 Cocoon / XSLT 中包含外部.js

Include external .js in Cocoon / XSLT

本文关键字:包含外 js XSLT Cocoon      更新时间:2023-09-26

我正在尝试在我正在Cocoon/XSLT中构建的页面中包含外部.js文件。

目录结构:

/my_project
    /xsl/my_page.xsl
    /js/my_scripts.js
    sitemap.xmap

在站点地图中:

<map:match pattern="my_page.html">
    <map:generate src="cocoon:/my_data.xml" />
    <map:transform src="xsl/my_page.xsl" type="xslt2">
        <map:parameter name="baselink" value="{baselink:SitemapBaseLink}" />
    </map:transform>
    <map:serialize type="html5" />
</map:match>

在 my_page.xsl:

<xsl:param name="baselink" />
...
<script src="//code.jquery.com/jquery-1.11.1.min.js">&#160;</script>
<script src="../js/my_scripts.js" type="text/javascript">&#160;</script>

在my_scripts.js中,我只有一行用于测试:

console.log('Success');

当我在浏览器中查看源代码时,我看到包含脚本标签,但上面的 log 语句没有执行。

.HTML:

<!DOCTYPE html SYSTEM "about:legacy">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
        …
    </head>
    <body>
        …
        <script src="//code.jquery.com/jquery-1.11.1.min.js"> </script>
        <script src="../js/my_scripts.js" type="text/javascript"> </script>
    </body>
</html>

因此,尽管脚本标记在代码中,但我不确定它是否真的指向源 - 或者 log 语句会执行。

当我查看网络选项卡(Firebug)的JavaScript部分时,我看到:

GET my_scripts.js 200 OK

控制台中存在以下条目:

<?xml version="1.0" encoding="UTF-8"?> my_scripts.js (line 1)
为了

在 XSL 中包含外部.js,我还需要做其他什么/"特殊"的事情吗?

谢谢!

固定。

在站点地图中添加了以下内容:

<map:match pattern="**.js">
    <map:read src="js/{1}.js" mime-type="text/javascript" />
</map:match>

将 JavaScript 更改为:

<script src="my_scripts.js" type="text/javascript">&#160;</script>