Opencart 3.0 - 如何使用事件系统从目录控制器添加内联JavaScript代码?

时间:2018-02-06 15:02:25

标签: javascript php opencart opencart-3 opencart-events

如何从控制器添加内联js代码?

管理模块控制器

$this->model_setting_event
     ->addEvent("one_two_three","catalog/view/common/header/before","extension/module/oone_two_three/inject_abc_javascript");

和目录模块控制器

public function inject_abc_javascript(&$route, &$data){
    $code = $this->config->get("module_one_two_three_code");
    $data["scripts"] = "<script>OneTwo.push(function() {
  OneTwo.init({
      appId: $code,
  });});</script>";
}

有没有办法在OpenCart标题中添加内联javascript?

1 个答案:

答案 0 :(得分:0)

是的,你可以做到。

首先,addEvent个参数中存在错误/拼写错误,将oone_two_three更改为one_two_three

然后你需要第三个函数参数:

public function inject_abc_javascript($route, &$data = false, &$output = false){
    // Before closing tag
    $hook = '</head>';
    $js = '<script>alert("Hello from one_two_three module!");</script></head>';
    $output = str_replace($hook, $js, $output);
}