2012-12-04 85 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