如何在 Opencart 中动态添加 JavaScript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12297083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:50:48  来源:igfitidea点击:

How to add JavaScript dynamically in Opencart?

phpjavascriptjqueryopencart

提问by user6030

I want use a jQuery plugin in category.tpl. Put files in javascript/jquery directory. Now, how can use this plugin?

我想在 category.tpl 中使用 jQuery 插件。将文件放在 javascript/jquery 目录中。现在,如何使用这个插件?

回答by Martin Zeitler

/* one can load JS like that: */    
if(file_exists('catalog/view/javascript/'.$this->config->get('config_template').'/script.js')) {
    $this->document->addScript('catalog/view/javascript/'.$this->config->get('config_template').'/script.js');
}

It is rather "the proper way" to use existing functions, than to add scripts manually into header.tpl.

使用现有函数而不是手动将脚本添加到 header.tpl 中更像是“正确的方法”。

As a hint, based upon the answer below - one could loop an array of filenames, in order to keep control over the loading order, which is often relevant while they might depend on each other.

作为提示,基于下面的答案 - 可以循环一组文件名,以控制加载顺序,这通常是相关的,而它们可能相互依赖。

回答by Per Salbark

I've never used OpenCart, but a quick google session tells me that you should include the plugin scripts (just like any other js) in a file called header.tpl.

我从未使用过 OpenCart,但是一个快速的谷歌会话告诉我你应该将插件脚本(就像任何其他 js)包含在一个名为header.tpl.

Here is a part of an sample header.tpl-file I found:

这是header.tpl我找到的示例文件的一部分:

<script type="text/javascript" src="catalog/view/javascript/jquery/jquery-1.3.2.min.js"></script>

Just add a the following line below the jQuery include so it looks like this:

只需在 jQuery 包含下面添加以下行,它看起来像这样:

<script type="text/javascript" src="catalog/view/javascript/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="catalog/view/javascript/jquery/[PLUGIN FILE NAME].js"></script>

and you should be good to go.

你应该很高兴去。

回答by jeeva

First paste your jquery files, css files and images in catalog/view/javascript/yourpluginfolder. Then call the jquery plug in files in catalog/view/theme/yourtheme(default)/template/product/category.tpl file. For ex, YOur php code;.. ... ....

首先将您的 jquery 文件、css 文件和图像粘贴到catalog/view/javascript/yourplugin文件夹中。然后调用catalog/view/theme/yourtheme(default)/template/product/category.tpl文件中的jquery插件文件。例如,您的 php 代码;.. ... ....

<script src="catalog/view/javascript/jquery/jquery-ui-min.js"></script>

<script type="text/javascript" src="catalog/view/javascript/jquery/jquery.anythingslider.js"></script>

<link rel="stylesheet" href="catalog/view/theme/default/stylesheet/anythingslider.css">

<script>

    // DOM Ready

    $(function(){

       $('#slider').anythingSlider();

       $('#slider1').anythingSlider();

       $('#slider2').anythingSlider();


   });

</script>

its for slider.. you can do your action in php (above the script).

它用于滑块..您可以在 php 中执行您的操作(在脚本上方)。

回答by aceph ali

You'll need to include JS and CSS sources in Header View (/catalog/view/theme/[your theme]/template/common/header.tpl)

您需要在标题视图中包含 JS 和 CSS 源代码 (/catalog/view/theme/[your theme]/template/common/header.tpl)

回答by user1786647

in config.php     
define('DIR_JAVASCRIPT', 'D:\wamp\www\opencart/view/javascript/your_dir/');


in header.tpl        
<?php
    if (is_dir(DIR_JAVASCRIPT)):
        if ($handle = opendir(DIR_JAVASCRIPT)):
                while (false !== ($file = readdir($handle))):   
                    if (preg_match('@\.js$@', $file)):                      
?>
    <script type="text/javascript" src="<?php echo 'view/javascript/your_dir/'.$file; ?>"></script>
<?php               
                    endif;
                endwhile;
            closedir($handle);
        endif;
    endif;
?>