Welcome Guest, Not a member yet? Register   Sign In
trying to call javascript
#15

Alright, I don't know what your experience is with CodeIgniter but I'll try to guide you in the right direction. 

You're trying to load a JavaScript file and call a function. Since JavaScript code is executed by the browser, you need to do this in the View. 

It is a best practice to separate logic and presentation. So what I like to do, is build an array in the Controller of all the JS and CSS file I will need, and output these in the view. Here is a really simple example of that:
PHP Code:
$data = array(
 
 'js' => array('jQuery.js''myApp.js'),
 
 'css' => array('bootstrap.css''myApp.css',
 
 'page_title' => 'JS Example')
);
$this->load->view('myView'$data); 


In the View, you read these arrays and output the filenames to the final HTML document so the browser can load them. Then you can execute any JS function present in these scripts.
PHP Code:
<?php foreach($js as $filename): ?>
<script type="text/javascript" src="<?php echo $filename?>"></script>
<?php endforeach; ?>

<script type="text/javascript">
  doSomething();
</script>

<h1><?php echo $page_title?></h1> 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply


Messages In This Thread
trying to call javascript - by richb201 - 05-07-2019, 03:05 PM
RE: trying to call javascript - by php_rocs - 05-07-2019, 07:03 PM
RE: trying to call javascript - by richb201 - 05-07-2019, 10:37 PM
RE: trying to call javascript - by richb201 - 05-08-2019, 05:34 AM
RE: trying to call javascript - by InsiteFX - 05-08-2019, 08:33 AM
RE: trying to call javascript - by richb201 - 05-09-2019, 09:32 AM
RE: trying to call javascript - by InsiteFX - 05-09-2019, 02:28 PM
RE: trying to call javascript - by richb201 - 05-09-2019, 04:11 PM
RE: trying to call javascript - by InsiteFX - 05-10-2019, 03:31 AM
RE: trying to call javascript - by richb201 - 05-10-2019, 06:04 AM
RE: trying to call javascript - by includebeer - 05-10-2019, 11:02 AM
RE: trying to call javascript - by richb201 - 05-10-2019, 03:56 PM
RE: trying to call javascript - by InsiteFX - 05-10-2019, 08:23 AM
RE: trying to call javascript - by Wouter60 - 05-10-2019, 10:46 AM
RE: trying to call javascript - by includebeer - 05-11-2019, 05:49 AM
RE: trying to call javascript - by InsiteFX - 05-11-2019, 08:18 AM
RE: trying to call javascript - by includebeer - 05-11-2019, 02:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB