Adding a javascript to all pages in OpenCart

邮差的信 提交于 2020-01-02 05:04:07

问题


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

// 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?


回答1:


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

Then add

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

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

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

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




回答2:


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.




回答3:


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.

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.

Tried and tested on OpenCart 3




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/12787378/adding-a-javascript-to-all-pages-in-opencart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!