Wed*_*Man 6 javascript overriding bigcartel
我试图从Bigcartel覆盖JS函数.我无法访问JS文件.
原文是:
updateCart: function(cart) {
$('aside .cart .count, .main header .cart').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
},
Run Code Online (Sandbox Code Playgroud)
而我正试图将其改为:
updateCart: function(cart) {
$('aside .cart .count, .sml .cart, .big .cart .count').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
},
Run Code Online (Sandbox Code Playgroud)
我知道其他人也提出了类似的问题,但在理解如何实现JS时我是一个完整的菜鸟(我只知道如何通过试验和错误进行调整)
如果有人能够帮助我,那就给我一个很棒的答案.
谢谢,
iWed-
编辑[10.10.13 :: 21:24hr]
为了澄清,我没有直接访问原始JS文件.我只能通过chrome查看它.我只能访问html文件.这是一个大卡特尔主题编辑.
这是使用chrome复制JS的链接.第216行是代码,如果这有帮助:http://jsfiddle.net/w9GTJ/
Tib*_*bos 12
编辑:你很幸运.从发布的代码中可以看到updateCart方法是在window.Store全局对象上导出的.解决方案是在加载原始脚本后添加此代码:
window.Store.updateCart = function(cart) {
$('aside .cart .count, .sml .cart, .big .cart .count').htmlHighlight(cart.item_count);
return $('aside .cart .total').htmlHighlight(Format.money(cart.total, true, true));
};
Run Code Online (Sandbox Code Playgroud)
一般情况说明:
在网页中加载的所有脚本都在相同的全局范围内运行,因此覆盖变量就像之后插入脚本一样简单:
<script>
var x = 5; // original script
</script>
<script>
x = 2; // your inserted script
</script>
Run Code Online (Sandbox Code Playgroud)
从它的外观来看,你的函数被定义为一个对象的属性:
var x = {
updateCart : function(cart) {
// stuff
}
}
Run Code Online (Sandbox Code Playgroud)
所以要覆盖它你需要做:
x.updateCart = function(cart) {
// your code
}
Run Code Online (Sandbox Code Playgroud)
最后,如果函数在原始代码中是私有的,则有一种情况是您根本无法覆盖它:
function() {
var x = {
updateCart: function(){}
}
}()
// No way to access x.updateCart here
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30702 次 |
| 最近记录: |