2012-12-04 84 views
0

我试图在结帐页面(FoxyCart模板)上安装Noty(jQuery Notification插件)。我安装在我结账模板的部分添加以下代码:jQuery通知栏

<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> 

如果我理解正确的话,这是所有需要做得到它才能正常显示。出于某种原因,通知不会弹出。这条线有什么问题吗?

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

这里是应该安装了Noty的the page

+0

不,该行是正确的。 – Hubro

+0

该网页上有很多错误..和多个jQuery版本..你需要先解决这些问题。 –

+0

我只能控制那个jquery.min.js(1.7)文件。其他一切来自托管支付提供商。删除了我的jQuery包含行,仍然没有显示出来。 –

回答

1

您正在重现文档中的错误。该生产线

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

,如果在全球范围内运行,重新定义noty,与noty函数的结果替换功能noty。从理论上讲,第一次调用应该可以工作,但对noty的任何后续调用都将失败,因为它们试图将对象作为函数调用。我不确定为什么第一个失败,但它可能是相关的。尝试更改要将结果分配给的对象的名称:

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

这可能会修复它。

如果您不需要操纵你创建后通知你也应该能够叫它不分配的结果给一个变量:

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

更新

如果你在非全局上下文中执行该语句(例如jQuery的ready事件),它将不会工作,因为variable hoisting。吊装采取任何var语句并将其移至语句的顶部。声明:

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

变成:

(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!'}); 
    }()); 
+0

你也可以把这个调用包装在一个'$(document).ready()'中,这个''将移动匿名函数中的变量(这是作者在'js/script.js'中做的)。 – pete

+0

@pete由于变量提升,实际上不起作用,我将扩展我的答案以涵盖该问题。 –

+0

变量吊装与它无关。我正在讨论在'$(document).ready()'的匿名函数中放置'var notyObj ='语句*。通过这样做,'notyObj'变量被声明(并且作用于)匿名函数,以便它不在全局范围内声明。 – pete