加载一个 javascript 库并且没有返回一个对象 [英] loading a javascript library and not getting an object returned

查看:31
本文介绍了加载一个 javascript 库并且没有返回一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 XPage 中加载一个 javascript 库.

I am trying to load a javascript library in XPages.

通常在 HTML 中,引用如下所示:

Normally in HTML the reference looks as followed:

<html>
<head>
<script src="https://hammerjs.github.io/dist/hammer.js"></script>
</head>
<body>
</body>
</html>

它在 DOM 中为我提供了一个 Hammer 对象,我可以进一步使用它.

which gives me a Hammer object in the DOM which I can work further with.

在 XPage 中,我进行了以下设置:

In XPages I have made the following setup:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" disableTheme="true"
    dojoForm="false" dojoTheme="false" dojoParseOnLoad="false"
    createForm="false">
    <xp:this.resources>
        <xp:script src="https://hammerjs.github.io/dist/hammer.js"
            clientSide="true">
        </xp:script>
    </xp:this.resources>
</xp:view>

或者:

<?xml version="1.0" encoding="UTF-8" ?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" disableTheme="true" dojoForm="false" dojoTheme="false" dojoParseOnLoad="false" createForm="false">
    <xp:this.resources>
        <xp:headTag tagName="script">
            <xp:this.attributes>
                <xp:parameter name="script" value="text/javascript" />
                <xp:parameter name="src" value="https://hammerjs.github.io/dist/hammer.js" />
            </xp:this.attributes>
        </xp:headTag>
    </xp:this.resources>
</xp:view>

但是 DOM 中不存在 Hammer 对象!

But the Hammer object is not present in the DOM!

我做错了什么?

推荐答案

hammer.js 使用 AMD.以下是使用 AMD 的hammer.js 源代码的片段:

hammer.js uses AMD. Here's a snippet from the hammer.js source code where AMD is used:

if (typeof define == TYPE_FUNCTION && define.amd) {
    define(function() {
        return Hammer;
    });
} else if (typeof module != 'undefined' && module.exports) {
    module.exports = Hammer;
} else {
    window[exportName] = Hammer;
}

不幸的是,AMD 加载与 XPage 中的 Dojo 发生冲突.请参阅有关如何删除 AMD 加载的答案.

Unfortunately AMD loading conflicts with Dojo in XPages. See this answer on how to remove AMD loading.

在您的情况下,您需要下载hammer.js,更改AMD加载部分,将其添加到您的nsf,然后从您的nsf加载脚本.

In your case you need to download hammer.js, change the AMD loading part, add it to your nsf and then load the script from your nsf instead.

您可以通过将hammer.js 中的代码更改为例如以下内容来删除AMD 加载部分:

You remove the AMD loading part by changing the code in hammer.js to for instance this:

//if (typeof define == TYPE_FUNCTION && define.amd) {
//    define(function() {
//        return Hammer;
//    });
//} else if (typeof module != 'undefined' && module.exports) {
if (typeof module != 'undefined' && module.exports) {
    module.exports = Hammer;
} else {
    window[exportName] = Hammer;
}

这篇关于加载一个 javascript 库并且没有返回一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆