![]() |
include javascript in CI4 view - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: include javascript in CI4 view (/showthread.php?tid=91934) |
include javascript in CI4 view - jcarvalho - 11-11-2024 Hi guys, I am struggling with add a javascript file into my view in CI4 in CI3 I just did $this->load->view('js/dtArtigos.js'); to load a datatable definition with some php in the mix like <?php $settings = new \Config\Conffile(); ?> . I have tried inside script tag <? = $this->include('js/dtArtigos.js');?> with no luck <?=file_get_contents( __DIR__ . '/js/dtArtigos.js'); ?> with no luck for organization purposes I like the javascript files inside views, folder view controller like for example products and a a folder js to hold my js for example --/app/Views/products/js What is the correct way to have this working? I prefer nor ro do <script src="xxxxxx"></script> but echoing the file contect in view file inside script tag Many thanks! RE: include javascript in CI4 view - warcooft - 11-11-2024 js works with <script src="path/to/file"></script> tags, if your js file is inside a view just add a script tag to work around that. example <script src="<? = $this->include('js/dtArtigos.js');?>"></script> To manage files better you might consider using the following library https://github.com/michalsn/minifier RE: include javascript in CI4 view - jcarvalho - 11-11-2024 (11-11-2024, 10:46 AM)That didnt worked, got 400 400 Bad Requestbecause the url generated was https://varziela.novoscanais.com/%3C? = $this->include('js/dtArtigos.js');?>inside the js file I have something like:<?php Wrote: $settingsxxx = new \Config\Ncweb(); RE: include javascript in CI4 view - captain-sensible - 11-11-2024 not quite surevwhat your after but i put js files in public/js then in view : Code: <script src="<?php echo base_url('js/popper.js');?>"></script> As you can see i palce it bottom of page ; script i use is actually Jquery RE: include javascript in CI4 view - InsiteFX - 11-11-2024 PHP Code: // Get shared instance with config function RE: include javascript in CI4 view - jcarvalho - 11-12-2024 (11-11-2024, 11:33 PM)Hi Sir, thank you for your input! What I have done, added a script tag in view file like Wrote: RE: include javascript in CI4 view - InsiteFX - 11-12-2024 All of your resources should be under root. there is a index.html file in all folders that stop access to them look at the app/Views/index.html file. root -- app -- system -- public ( All resource's under here ) -- assets -- css -- js -- images -- etc; // Then you can load like this Code: <link href="<?= base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" rel="stylesheet"> RE: include javascript in CI4 view - jcarvalho - 11-12-2024 Yes, I normally respect the tree architecture, bt this view is a very complex one so I changed it a little bit for the views of this specific controller. I solved it, the problem was that I wasnt giving the correct path now it is working with this code inside the view: PHP Code: <script> RE: include javascript in CI4 view - InsiteFX - 11-12-2024 Glad you got it working. |