CodeIgniter Forums
trying to call javascript - 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: trying to call javascript (/showthread.php?tid=73547)

Pages: 1 2


RE: trying to call javascript - InsiteFX - 05-10-2019

If your web browser is running then just hit F12 key it will open the browsers development tools.

You can see what is happening with javascript and ajax calls in the console.


RE: trying to call javascript - Wouter60 - 05-10-2019

Typically, Javascript is not called from the controller (unless it's an AJAX call), but inside a view.
To load an external .js file, you need to use the base_url() function.
E.g., if your js files are in the /assets/js folder, you load it like this (in your view):
Code:
<script src="<?= base_url('assets/js/example.js');?>" />
After this, the functions in your js file are available.
You can save yourself a lot of trouble if you load the jQuery library. It can do a lot of dynamic html actions for you, like showing or hiding parts of your page. It can also simplify AJAX calls.

Good luck.


RE: trying to call javascript - includebeer - 05-10-2019

(05-10-2019, 06:04 AM)richb201 Wrote: There seems to be something BIG I am missing here? It would seem that I would need to tell the PHP program that register_extension() is a valid javascript function? 

Yes you are missing somthing big. You will have a hard time doing this if you don't start from the begining: learn how JavaScript works with plain HTML, how it interact with the browser and the server. Then, learn how to integrate your JavaScript code in a PHP application.


RE: trying to call javascript - richb201 - 05-10-2019

(05-10-2019, 11:02 AM)includebeer Wrote:
(05-10-2019, 06:04 AM)richb201 Wrote: There seems to be something BIG I am missing here? It would seem that I would need to tell the PHP program that register_extension() is a valid javascript function? 

Yes you are missing somthing big. You will have a hard time doing this if you don't start from the begining: learn how JavaScript works with plain HTML, how it interact with the browser and the server. Then, learn how to integrate your JavaScript code in a PHP application.

Thanks. This is the part I am currently missing:
>>Then, learn how to integrate your JavaScript code in a PHP application.

This is the part I am asking about.  Your comment reminds me of some comments I have seen about playing jazz guitar. They go like this: the person asks how do I add some jazz licks to my blues playing. The answer goes like this:

1) first learn all the notes on the neck
2) then learn all the scales
3) then learn all the chords in all the positions
4) then learn the arpeggios of all chords in 3 positions
5) learn to sight read

I know there are some guitar players up here who have seen this type of comment on the guitar websites. As a "functional" jazz guitarist I can tell you that it would take years and years to cover all that stuff. I have been playing for 45 years and know some of them, not all of them. Could I tell a newbie guitar player how to do it? No but I could point them in the right direction. I would tell them about "playing the changes". That is the crux of it. It is not the answer, but it is the "path" to success. I'd tell them about articles or books that cover that topic. 

If I am a little short, it is just that I am getting frustrated. Sorry. 

I am getting this response: Uncaught ReferenceError: register_extension is not defined

register_extension() calls my sendMessage()If I could get past the CI interaction with JS, I am sure I could get the messaging working on my own. I am pretty good with a debugger.  


RE: trying to call javascript - includebeer - 05-11-2019

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> 



RE: trying to call javascript - InsiteFX - 05-11-2019

A Beginner’s Guide to AJAX With jQuery

Webucator's Free Ajax Tutorial

jQuery and Ajax (not for the dummies)


RE: trying to call javascript - includebeer - 05-11-2019

(05-11-2019, 08:18 AM)InsiteFX Wrote: A Beginner’s Guide to AJAX With jQuery

Webucator's Free Ajax Tutorial

jQuery and Ajax (not for the dummies)

Did I missed something? Where did he say he wants to do an ajax request?