简体   繁体   English

Shopify 部分在添加部分时不执行脚本js代码

[英]Shopify section does not execute script js code when section is added

this might be a quick question but I cannot find the answer.这可能是一个快速的问题,但我找不到答案。 Im trying to build a section with a <script> tag in it.我正在尝试构建一个带有<script>标记的部分。 The script works fine once the section is added and saved, but my problem is that this section js code is not executed when it is added on the customiser on first instance.添加并保存该部分后,该脚本工作正常,但我的问题是该部分 js 代码在第一个实例上添加到定制器时未执行。 This leads to the section looking ugly cause the scripts needs to run.这导致该部分看起来很丑,因为脚本需要运行。 For more context, this section has a slider script in the section which is not executed when the section is added in the customiser.有关更多上下文,此部分在该部分中有一个 slider 脚本,当在定制器中添加该部分时不会执行该脚本。 Only after the section has been saved, then the slider starts working.只有保存该部分后,slider 才开始工作。 Anyone has had this issue?有人遇到过这个问题吗? Or how to solve it?或者如何解决? I know I can extract the script to a js file and run it from there and take the data from the section html properties and use the event shopify:section:load .我知道我可以将脚本提取到一个 js 文件并从那里运行它并从 html 属性部分获取数据并使用事件shopify:section:load I would like to know if its possible keeping the code in the section, that way I only work the code from one file.我想知道是否可以将代码保留在该部分中,这样我只能从一个文件中处理代码。 Also {% javascript %} tag has the same issue. {% javascript %}标签也有同样的问题。

To further the explanation, I have the next script tag inside a section为了进一步解释,我在一个部分中有下一个脚本标签

<script>console.log("this is not executed")</script>

If I add this section in the customiser, that script tag is not executed.如果我在定制器中添加此部分,则不会执行该脚本标记。 So I get no message on the console log.所以我在控制台日志上没有收到任何消息。

Use schema checkbox for enabling or disabling, the setting values from the user will be stored in settings_data.json使用模式复选框启用或禁用,用户的设置值将存储在settings_data.json

{% if section.settings.script_enabled %}
  <script>
    $(document).ready(function(){
      console.log('{{ section.settings.script_text }}');
    });
  </script>
{% endif %}



{% schema %}
  {
    "name": "section pages",
    "settings": [
      {
        "type": "checkbox",
        "id" : "script_enabled",
        "label" : "enable script",
        "default": true
      },
      {
        "type": "text",
        "id": "script_text",
        "label": "script text",
        "default": "hello from schema"
      }
    ]
  }
{% endschema %}

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

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