如何从控制器添加内联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?
答案 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);
}