Andrej Stas Posted October 29, 2016 Share Posted October 29, 2016 (edited) It seems to me that there's no jQuery library included in RC3 anymore. It doesn't show any error right after installation, but when I activated my new module, I got this error (screenshot) (index):923 Uncaught ReferenceError: $ is not defined(…) Any comments? Edited November 27, 2016 by Andrej Stas (see edit history) Link to comment Share on other sites More sharing options...
rocky Posted October 29, 2016 Share Posted October 29, 2016 I can reproduce this. It breaks all my modules in PrestaShop v1.7 RC3. /** * @deprecated 1.7 This function has no effect in PrestaShop 1.7 theme. jQuery2 is register by the core on every theme. * Have a look at the /themes/_core folder. */ public function addJquery($version = null, $folder = null, $minifier = true) { /* This is deprecated in PrestaShop 1.7 and has no effect in PrestaShop 1.7 theme. jQuery2 is register by the core on every theme. Have a look at the /themes/_core folder. */ } I'm trying to understand what "jQuery2 is register by the core on every theme. Have a look at the /themes/_core folder." means. Link to comment Share on other sites More sharing options...
rocky Posted October 29, 2016 Share Posted October 29, 2016 It seems PrestaShop has removed jQuery from inline JavaScript, but it's still available in JavaScript files that import it. I'm going to try to move all my inline JavaScript into a separate file to see if that fixes the JavaScript errors. 1 Link to comment Share on other sites More sharing options...
Andrej Stas Posted October 29, 2016 Author Share Posted October 29, 2016 It seems PrestaShop has removed jQuery from inline JavaScript, but it's still available in JavaScript files that import it. I'm going to try to move all my inline JavaScript into a separate file to see if that fixes the JavaScript errors Rocky, please let me know if you manage to solve it. I will do more tests also. Link to comment Share on other sites More sharing options...
Andrej Stas Posted October 30, 2016 Author Share Posted October 30, 2016 (edited) Ok, mystery solved! - Prestashop now inserts all JS files, including jQuery library, right before </body> - I use inline JS in my modules, so basically I try to access jQuery function, but the library has not been loaded yet, that's why I get errors SOLUTION: - I found this JS script: https://github.com/withjam/jqshim-head which might solve the problem. - Another option is to put all inline JS into JS files Edited October 30, 2016 by Andrej Stas (see edit history) Link to comment Share on other sites More sharing options...
rocky Posted October 30, 2016 Share Posted October 30, 2016 I found an easier solution (thanks to shokinro) that doesn't require moving the JavaScript to a separate file. Just add the following code around the <script>: {block name='javascript_bottom'} {$smarty.block.parent} // Script code here {/block} EDIT: This only works if you are extending a TPL file. 2 Link to comment Share on other sites More sharing options...
Andrej Stas Posted October 30, 2016 Author Share Posted October 30, 2016 Thank you Rocky! Link to comment Share on other sites More sharing options...
shacker Posted November 12, 2016 Share Posted November 12, 2016 ok, but i have this script code <script type="text/javascript"> infinite_scroll = {$options nofilter}; {if isset($pages_nb)} infinite_scroll.maxPage = {$pages_nb}; {/if} $( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll, function(newElements, data, url) { eval(infinite_scroll.callback); }); </script> the code loads variables, so cant add into a js file, and prestashop validation dont want html code anymore on the php of the module. The js fie infinitescroll is loaded before that code. Wich is the "official" way to do? Link to comment Share on other sites More sharing options...
Andrej Stas Posted November 27, 2016 Author Share Posted November 27, 2016 ok, but i have this script code <script type="text/javascript"> infinite_scroll = {$options nofilter}; {if isset($pages_nb)} infinite_scroll.maxPage = {$pages_nb}; {/if} $( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll, function(newElements, data, url) { eval(infinite_scroll.callback); }); </script> the code loads variables, so cant add into a js file, and prestashop validation dont want html code anymore on the php of the module. The js fie infinitescroll is loaded before that code. Wich is the "official" way to do? Use the hook "displayBeforeBodyClosingTag" in your module: public function hookDisplayBeforeBodyClosingTag($params) { ...load data... $this->context->smarty->assign([ ....set all your javascript variables... ]); return $this->display(__FILE__, 'javascript_footer.tpl'); } and then insert all your javascript into this TPL file using all variables you need. Link to comment Share on other sites More sharing options...
rocky Posted November 27, 2016 Share Posted November 27, 2016 Thanks Andrej. That makes more sense than the way I was doing it. Link to comment Share on other sites More sharing options...
shacker Posted November 27, 2016 Share Posted November 27, 2016 Use the hook "displayBeforeBodyClosingTag" in your module: public function hookDisplayBeforeBodyClosingTag($params) { ...load data... $this->context->smarty->assign([ ....set all your javascript variables... ]); return $this->display(__FILE__, 'javascript_footer.tpl'); } and then insert all your javascript into this TPL file using all variables you need. you are the master Link to comment Share on other sites More sharing options...
ChewieTheWookie Posted January 25, 2021 Share Posted January 25, 2021 On 10/30/2016 at 9:24 AM, rocky said: I found an easier solution (thanks to shokinro) that doesn't require moving the JavaScript to a separate file. Just add the following code around the <script>: {block name='javascript_bottom'} {$smarty.block.parent} // Script code here {/block} EDIT: This only works if you are extending a TPL file. Thank you a thousand times ! This solved my issue, too, I had a module made for Ps 1.6, and in 1.7x I had to add this part to make it work. Thank you !! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now