向 OpenCart 中的所有页面添加 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12787378/
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-08-23 11:16:06  来源:igfitidea点击:

Adding a javascript to all pages in OpenCart

phpjavascriptopencart

提问by B. S.

I'm new to OpenCart and don't have any experience with PHP, so I have a question. I want to add the following JavaScript to hide the url bar on mobile browsers

我是 OpenCart 的新手,对 PHP 没有任何经验,所以我有一个问题。我想添加以下 JavaScript 来隐藏移动浏览器上的 url 栏

// When ready...
 window.addEventListener("load",function() {
 // Set a timeout...
 setTimeout(function(){
 // Hide the address bar!
 window.scrollTo(0, 1);
 }, 0);
});

However, I can't find a way to insert this so this code will be executed on all pages in OpenCart. Where should I put this code?

但是,我找不到插入此代码的方法,因此此代码将在 OpenCart 中的所有页面上执行。我应该把这段代码放在哪里?

回答by B-and-P

save your script to a file, say 'catalog/view/javascript/myscript.js'

将您的脚本保存到一个文件中,比如“catalog/view/javascript/myscript.js”

Then add

然后加

$this->document->addScript('catalog/view/javascript/myscript.js');

to the catalog/controller/common/header.phpsome place before this line:

到此行之前某个位置的目录/控制器/common/header.php

$this->data['scripts'] = $this->document->getScripts();

You could also just place your script inline into catalog/view/theme/{theme name}/template/common/header.tplusing normal html markup.

您也可以使用普通的 html 标记将脚本内嵌到catalog/view/theme/{theme name}/template/common/header.tpl 中

回答by deizel

Looking at the theme documentation, I believe you want to edit the following file:

查看主题文档,我相信您要编辑以下文件:

catalog/view/theme/{your-theme}/template/common/header.tpl

These templates (header, footer, etc) should appear on all pages.

这些模板(页眉、页脚等)应该出现在所有页面上。

回答by cweitat

You don't need to go through this trouble especially if you don't have access to FTP. All you need to do is just go to admin panel > design > theme editor > and choose the respective parts to insert the codes. If is footer then just choose footer.twig.

您不需要经历这个麻烦,尤其是在您无法访问 FTP 的情况下。您需要做的就是转到管理面板> 设计> 主题编辑器> 并选择相应的部分来插入代码。如果是页脚,则选择footer.twig。

After adding the codes, click Save and you will see the changes immediately. If add the codes directly to the file on FTP, it won't work.

添加代码后,单击“保存”,您将立即看到更改。如果将代码直接添加到 FTP 上的文件中,它将不起作用。

Tried and tested on OpenCart 3

在 OpenCart 3 上尝试和测试

回答by highpass

To add a script like that, just go to admin panel > design > theme editor > select "common" > footer and in the end of the file (after </html>) add the script.

要添加这样的脚本,只需转到管理面板 > 设计 > 主题编辑器 > 选择“通用” > 页脚,然后在文件末尾(之后</html>)添加脚本。

回答by K. B.

If you want script to all your pages of the OC just add it before footer tag in footer.tpl or footer.twig

如果你想为 OC 的所有页面添加脚本,只需在 footer.tpl 或 footer.twig 中的页脚标记之前添加它