Welcome Guest, Not a member yet? Register   Sign In
include javascript in CI4 view
#1

(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!
Reply
#2

(This post was last modified: 11-11-2024, 10:51 AM by warcooft.)

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[{::::::::::::::::::::::::::::::::>
Reply
#3

(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();
?>
and by the others methods I tried I got error, it looks like the js file cant do the settingsxxx
I use those settings to show/hide columns and format values... In CI3 was $this->config->item('xxxxx') and worked perfect


warcooftjs 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
Reply
#4

(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>
<script src="<?php echo base_url('js/bootstrap.bundle.js');?>"></script>
<script src="<?php echo base_url('js/jquery-3.5.1.min.js');?>"></script>
<script src="<?php echo base_url('js/jquery-migrate-3.3.0.js');?>"></script>

<script>
$( window ).load(function() {
$("button#2").hide();
$("p#toggle").hide();
$( "button#1" ).click(function( event ) {
$("button#2").show();
$("button#1").hide();
$("p#toggle").show();
});

}); 
 
</script>
</body>
</html>

As you can see i palce it bottom of page ; script i use is actually Jquery
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Reply
#5

PHP Code:
// Get shared instance with config function
$settings config('configFileName'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(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:
Code:
<script src="<?=base_url()?>js/encomendas/dtArtigos.js"></script>


In the js file just for testing I wrote

PHP Code:
<?php 
 $c 
config('Ncweb');
?>

var testvar = 'Hi';
alert(testvar); 

And got javascript error in the dev tools of browser Uncaught SyntaxError: expected expression, got '<'

Funny enought, the $c = config('Ncweb') just works in the view file, but not in the js file. In CI3 I had no problem in accessing $this-config->item('ccc') and produce javascript according



InsiteFX
PHP Code:
// Get shared instance with config function
$settings config('configFileName'); 
Reply
#7

(This post was last modified: 11-12-2024, 04:03 AM by InsiteFX.)

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">
<script src="<?= base_url('assets/js/encomendas/dtArtigos.js'); ?>"></script>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

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>
    //loading assets
    <?=view('encomendas/js/dtArtigos.js');?>
</script> 
Reply
#9

Glad you got it working.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB