简体   繁体   English

jQuery通知栏

[英]jQuery Notification Bar

I'm trying to install Noty (a jQuery Notification plugin) on a checkout page (FoxyCart template). 我正在尝试在结帐页面(FoxyCart模板)上安装Noty (一个jQuery Notification插件)。 I installed the following code in the section of my checkout template: 我在结帐模板的部分中安装了以下代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

<script type="text/javascript" src="http://lavajavamaui.com/js/noty/jquery.noty.js"></script>

<script type="text/javascript" src="http://lavajavamaui.com/js/noty/layouts/top.js"></script>
<script type="text/javascript" src="http://lavajavamaui.com/js/noty/layouts/topLeft.js"></script>
<script type="text/javascript" src="http://lavajavamaui.com/js/noty/layouts/topRight.js"></script>
<!-- You can add more layouts if you want -->

<script type="text/javascript" src="http://lavajavamaui.com/js/noty/themes/default.js">
</script>
<script type="text/javascript">
var noty = noty({text: 'noty - a jquery notification library!'});
</script>

If I understand correctly, this is all that needs to be done to get it to display properly. 如果我理解正确,那么就需要完成所有操作才能使其正确显示。 For some reason, the notification is not popping up. 由于某种原因,通知不会弹出。 Is there something wrong with this line? 这条线有什么问题吗?

<script type="text/javascript">
var noty = noty({text: 'noty - a jquery notification library!'});
</script>

Here is the page that should have Noty installed. 这是应该安装Noty 的页面

You are reproducing a bug in the documentation. 您正在复制文档中的错误。 The line 线

var noty = noty({text: 'noty - a jquery notification library!'});

if run in a global context, redefines noty , replacing the function noty with the result of the noty function. 如果在全球范围内运行,重新定义noty ,更换功能noty与结果noty功能。 In theory this first call should work but any subsequent calls to noty will fail because they are trying to call an object as a function. 理论上,第一个调用应该可以工作,但是随后对noty任何调用都将失败,因为它们试图将对象作为函数进行调用。 I'm not sure why the first one fails, but it is possible it is related. 我不确定第一个失败的原因,但有可能是相关的。 Try changing the name of the object you are assigning the result to: 尝试更改将结果分配给的对象的名称:

var notyObj = noty({text: 'noty - a jquery notification library!'});

That might fix it. 那可能会解决它。

If you don't need to manipulate the notification after you create it you should also be able to just call it without assigning the result to a variable: 如果您不需要在创建通知后对其进行操作,则还可以直接调用它,而无需将结果分配给变量:

noty({text: 'noty - a jquery notification library!'});

Update 更新资料

If you execute that statement inside a non-global context (such as jQuery's ready event) it will not work because of variable hoisting . 如果您在非全局上下文(例如jQuery的ready事件)中执行该语句,则由于变量提升 ,该语句将无法工作。 Hoisting takes any var statement and moves it to the top of the statement. 提升采用任何var语句并将其移至该语句的顶部。 The statement: 该声明:

    (function () {
        var noty = noty({text: 'noty - a jquery notification library!'});
    }());

Is turned into: 变成:

    (function () {
        var noty; // inside this function noty now refers
                  // to a local variable set to "undefined"

        // JavaScript looks for local variables before it looks at globals,
        // so it will use the new local "noty" when it tries to execute the
        // right side of this line. This will fail because you
        // can't invoke undefined
        noty = noty({text: 'noty - a jquery notification library!'});
    }());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM