Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a javascript to all pages in OpenCart

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?

like image 398
B. S. Avatar asked Oct 08 '12 18:10

B. S.


2 Answers

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.

like image 137
B-and-P Avatar answered Oct 10 '22 14:10

B-and-P


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.

like image 34
deizel Avatar answered Oct 10 '22 14:10

deizel