CodeIgniter Forums
javascript is being called twice - 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: javascript is being called twice (/showthread.php?tid=73655)



javascript is being called twice - richb201 - 05-18-2019

I managed to get my javascript routine to run. I put the code to call it in in the _construct

$this->load->js("/js/javascript_funcs_extensionloaded.js");

The code is:

[font=DejaVu Sans Mono]   var editorExtensionId = "lamacgnkfoiknjpleghfknfigbmhdaei";


   if (chrome && chrome.runtime && chrome.runtime.sendMessage) {
   chrome.runtime.sendMessage(extensionId, 'snapshot_mode');
}
I am finding that this code is running twice. The way I know this is that the Extension is getting the message "snapshot_mode" twice. Should I be wrapping this js code in 
[/font]

[font=DejaVu Sans Mono]$(document).ready(function(){[/font]
[font=DejaVu Sans Mono]? Or is the problem that I am calling it in __construct()? I want the js code to run just once per initialization of my code. [/font]


RE: javascript is being called twice - php_rocs - 05-18-2019

@richb201,

There are multiple ways to do it. I would not put it in the construct. You could try it in the actual page method/function or directly in the view. Your choice.


RE: javascript is being called twice - richb201 - 05-18-2019

I put it in the first screen in the controller. But it still gets called twice. I do want to wait until after a successful login. Is there someplace to put the call to only let it happen once?


RE: javascript is being called twice - php_rocs - 05-19-2019

@richb201,

You could put it in the view. The header or footer part of the view.


RE: javascript is being called twice - richb201 - 05-21-2019

I got it going and I am posting this for the next guy trying to use .js with ci.

I created a view that has:

script language="javascript">

   $(document).ready( function() {
       var editorExtensionId = "lamacgnkfoiknjpleghfknxxxbmhdaei";

       // Make a simple request:
       chrome.runtime.sendMessage(editorExtensionId, "snapshot_modeb",
           function (response) {
               if (!response.success)
                   handleError(url);

           }
       )
   })

</script>

I am calling it in my controller when I display the first page. Here is what I use to call it:

$data = array(
               'command' => 'My Title',
               'userid' => 'My Heading',
               'campaign' => 'My Message',
               'param_1' => 'My Title'

           );

           $this->load->view("javascript_funcs_extensionloaded", $data);

The view has a .php extension. I haven't gotten the data passing working yet, but that is next. I my case I am passing the message to an extension also running in the same copy of Chrome.