1

如何从控制器添加内联 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?

4

1 回答 1

0

是的,你可以做到。

首先,您的addEvent参数有错误/错字,请更改oone_two_threeone_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);
}
于 2018-02-08T13:56:29.287 回答