javascript files with php code - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: javascript files with php code (/showthread.php?tid=63170) |
javascript files with php code - Lykos22 - 10-02-2015 Hi guys, I'd like to ask for some generic information, if possible. I usually try to put all my javascript stuff in a single file, and not keep them inside <skript> tags in my views, so it would be a little bit cleaner and tided up. However there might be some cases where I have ti embed some PHP code inside my javascript, for instance: PHP Code: <script type="text/javascript"> So in this case I'm just leaving these scripts in my views. I was wondering whether there's a more efficient way to do this, or another better way to organize all javascripts. Would you recommend another way, or in that case, or keep going this way ? RE: javascript files with php code - davicotico - 10-02-2015 (10-02-2015, 09:02 AM)Lykos22 Wrote: Hi guys, I'd like to ask for some generic information, if possible. You can use global variables in JS . Example: Code: <script type="text/javascript"> RE: javascript files with php code - freddy - 10-02-2015 yeah me too I love too use my js file with single file js not mixing them in php, based on experience i have done like this in view Code: <script type="text/javascript"> then in my js i use this Code: url : product, RE: javascript files with php code - JayAdra - 10-02-2015 There are a few different ways to handle this - each have their pros and cons. It depends on preference and your specific requirements really. This is a great post to refer to for information on each: http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript I generally do what others have suggested. I have a single <script> element in my <head> which contains all variables I need echoed into JS, which I can then reference anywhere in my other JS files. I use this mostly for language lines, but it has other uses too. RE: javascript files with php code - Lykos22 - 10-15-2015 So basically, you do something like this: Code: <head> Am I right ? but what you do in cases like this, where you need to loop to set dynamically some js data ? See this example: PHP Code: 'columns': [ |