include javascript in CI4 view |
11-11-2024, 09:15 AM
(This post was last modified: 11-11-2024, 09:50 AM by jcarvalho. Edit Reason: added some code sample to improve problem )
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!
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
@xxxx[{::::::::::::::::::::::::::::::::>
11-11-2024, 11:06 AM
(This post was last modified: 11-11-2024, 11:07 AM by jcarvalho. Edit Reason: spelling errors ) (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();
11-11-2024, 12:02 PM
(This post was last modified: 11-11-2024, 12:04 PM by captain-sensible. Edit Reason: forgot something )
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 PHP Code: // Get shared instance with config function What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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:
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"> What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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>
Glad you got it working.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |