简体   繁体   English

脚本适用于其他浏览器但不适用于Firefox

[英]Script works in other browsers but not in Firefox

I am using a script to add products to my cart in my online shop. 我正在使用脚本将产品添加到我的在线商店的购物车中。 In other browsers the function works properly but in Firefox the products are never added to the cart and I don't know why. 在其他浏览器中,该功能正常工作但在Firefox中,产品从未添加到购物车中,我不知道为什么。

I have read about something that Firefox doesn't provide a global event object but I don't know how to fix that / where I'd implement the object. 我已经阅读过Firefox没有提供全局事件对象的内容,但我不知道如何解决这个/我实现该对象的位置。

https://mylily.eu/pages/1-box-gratis-im-slipeinlagen-abo https://mylily.eu/pages/1-box-gratis-im-slipeinlagen-abo

This is one of the product pages from which i want to add the products to the cart by clicking "In den Warenkorb". 这是我想通过点击“In den Warenkorb”将产品添加到购物车的产品页面之一。

Here are some code snippets, the first one is the EventListener which onClick executes the actual addItemToCart function with the values of the variables. 下面是一些代码片段,第一个是EventListener,onClick使用变量值执行实际的addItemToCart函数。 The variable amount_slipeinlagen is a global variable that is changeable, the value is the passed on the addItemtoCart function: 变量amount_slipeinlagen是一个可更改的全局变量,该值是在addItemtoCart函数上传递的:

<script>
document.getElementById("add-to-cart-button").addEventListener("click", function(){

     addItemToCart(variantid_slipeinlagen, amount_slipeinlagen, frequency_slipeinlagen , "Weeks", "199014")
})
</script>



<script>
function addItemToCart (variant_id, quantity, shipping_interval_frequency, shipping_interval_unit_type, subscription_id) {
  data = {
"quantity": quantity,
"id": variant_id,
"properties[shipping_interval_frequency]": shipping_interval_frequency, 
"properties[shipping_interval_unit_type]": shipping_interval_unit_type,            
"properties[subscription_id]": subscription_id
}
  jQuery.ajax({
  type: 'POST',
  url: '/cart/add.js',
  data: data,
  dataType: 'json',
  success: function() { 
      window.location.href = '/cart'; 
  }
  });
  window.location = '/checkout';

}

</script>

On Firefox the product is just not added to the cart at all. 在Firefox上,产品根本没有添加到购物车中。

The answer can only serve as a guide to the question because it is broad. 答案只能作为问题的指南,因为它很广泛。

Browsers have Javascript environment. 浏览器有Javascript环境。 Each of them runs specific version of Javascript. 他们每个人都运行特定版本的Javascript。 These are vendors (Google Chrome, Firefox, Safari, IE). 这些是供应商(谷歌浏览器,Firefox,Safari,IE)。 Infact their JS engine adopts separate standards of Ecmascript ( the standard body for Javascript). 事实上,他们的JS引擎采用了独立的Ecmascript标准(Javascript的标准体)。

It also depends on the version you're running for each browser. 它还取决于您为每个浏览器运行的版本。

So, it is very much possible that you are using some syntax which is not supported in Firefox's current version. 因此,您很可能使用Firefox当前版本不支持的某些语法。

What you can do is check the console for errors and then slowly narrow down the file or code in which the error is generated. 您可以做的是检查控制台是否有错误,然后慢慢缩小生成错误的文件或代码。 Once you have identified the exact code. 一旦确定了确切的代码。 Try to test run the code in different browsers. 尝试在不同的浏览器中测试运行代码。

Tools like Babel enables you to use all latest features without breaking any sweat and transpiles it to a uniform code across all browsers. Babel这样的工具使您可以使用所有最新功能而不会出汗,并将其转换为所有浏览器的统一代码。 You do have to configure it though once. 你必须配置一次。

For now, manually replacing the troublesome code should be enough. 现在,手动替换麻烦的代码就足够了。 Probably you're using some recent syntax which this browsers doesn't support. 可能你正在使用这种浏览器不支持的最新语法。 Take the help of pollyfill or replace it with Javascript code version supported by all target browsers your app needs to run. 借助pollyfill或将其替换为您的应用需要运行的所有目标浏览器支持的Javascript代码版本。

Hope it helps! 希望能帮助到你!

When you do location.href = "/checkout" , you are navigating away from the current page and in the mean time killing the page and all its scripts. 当你执行location.href = "/checkout" ,你将离开当前页面,同时杀死页面及其所有脚本。
The asynchronous XMLHttpRequest that jQuery.ajax did initiate may very well be also killed by the browser in the process. jQuery.ajax确实启动的异步XMLHttpRequest也可能在此过程中被浏览器杀死。

Simply getting rid of this line will make your code work in Firefox (tested by pasting this code in the Console): 简单地删除此行将使您的代码在Firefox中工作(通过在控制台中粘贴此代码进行测试):

function addItemToCart(variant_id, quantity, shipping_interval_frequency, shipping_interval_unit_type, subscription_id) {
  data = {
    "quantity": quantity,
    "id": variant_id,
    "properties[shipping_interval_frequency]": shipping_interval_frequency,
    "properties[shipping_interval_unit_type]": shipping_interval_unit_type,
    "properties[subscription_id]": subscription_id
  }
  jQuery.ajax({
    type: 'POST',
    url: '/cart/add.js',
    data: data,
    dataType: 'json',
    success: function() {
      // only when we succeed do we redirect
      window.location.href = '/cart';
    }
  });
// do not redirect now

}

暂无
暂无

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

相关问题 图片 <option>适用于Firefox,但不适用于其他浏览器 - Image for <option> works in firefox but not works in other browsers 该网站可以在Firefox中运行,但没有其他浏览器,为什么? - site works in firefox but no other browsers, why? 该网站可以在除Firefox之外的其他浏览器上正常运行 - Site works fine on other browsers except for Firefox Firefox无法识别Java代码,但可以在所有其他浏览器中使用 - Javascript code not understood by Firefox But works in all other browsers Firefox无法播放声音,但可以在其他浏览器中使用 - Firefox won't play sounds, but works in other browsers $ .post不适用于最新的Firefox,但可用于所有其他浏览器 - $.post not working in latest firefox but works in all other browsers 在Firefox中使用Hammer Pinch方向错误-在所有其他浏览器中均可使用 - Using Hammer Pinch in Firefox directed wrongly - works in all other browsers Facebook类似于按钮iframe的实现在Firefox中无法点击,可在其他浏览器中使用 - Facebook like button iframe implementation not clickable in Firefox, works in other browsers Javascript文档[“ someid”]。src在Firefox中有效,但在其他浏览器中无效 - Javascript document[“someid”].src works in Firefox, but not in other browsers 可以在firefox中使用但在所有其他浏览器中均无法使用的jquery.ajax代码 - jquery.ajax code that works in firefox but fails in all other browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM