Welcome Guest, Not a member yet? Register   Sign In
javascript is being called twice
#1

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:

   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 

$(document).ready(function(){
? 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. 
proof that an old dog can learn new tricks
Reply
#2

@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.
Reply
#3

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?
proof that an old dog can learn new tricks
Reply
#4

@richb201,

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

(This post was last modified: 05-21-2019, 07:10 AM by richb201.)

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.
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB