# My plugin codes runs just once!(My own script shouldn't be cached)

**URL:** https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301
**Category:** Development
**Created:** [October 9, 2016, 2:08pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301 "2016-10-09T14:08:49Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [October 9, 2016, 2:08pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/1 "2016-10-09T14:08:49Z")

</div>

Hi.  
I wrote a plugin and I run my code. my code run just for the first page of my website.even if it is a simple alert.for example if you open front page and go to categories page the alert doesn’t work on new page.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [October 9, 2016, 4:34pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/2 "2016-10-09T16:34:11Z")

</div>

It isn’t cached, What you are seeing is your script likely only runs on page load, and page load only happens once. The page gets repainted and you likely need to subscribe to a different event.

Can you paste your script here? Or a link to it on GitHub (if it is located there)?

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [October 10, 2016, 8:57am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/3 "2016-10-10T08:57:19Z")

</div>

It can be anything like:

```
export default {
  name: 'tag-resize',
  initialize() {
    alert("hello");
  }
};

```

this work just on first Page

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [October 10, 2016, 9:15am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/4 "2016-10-10T09:15:15Z")

</div>

this is my whole code:

> ```
> export default {
> name: 'dynamic-table',
> initialize() {
> $(document).ready(function() {
> var elements = document.querySelectorAll(".topic-list .main-link a.title");
> var elements2 = document.querySelectorAll(".badge-wrapper.box span.badge-category");
> var i = 0;
> var a1 = -1, a2 = -1, a3 = -1;
> for(i = 0 ;i < elements2.length ; i++) //Finding the position of wanted topics
> {
> if (elements2[i].innerHTML == "ورزشی")
> { if(a1 == -1)
> a1 = i;
> else if (a2 == -1)
> a2 = i;
> else if(a3 == -1)
> a3 = i;
> }
> }
> document.getElementById('dyn1').innerHTML = elements[a1].innerHTML;
> document.getElementById('dyn1').href = elements[a1].href;
> document.getElementById('dyn2').innerHTML = elements[a2].innerHTML;
> document.getElementById('dyn2').href = elements[a2].href;
> document.getElementById('dyn3').innerHTML = elements[a3].innerHTML;
> document.getElementById('dyn3').href = elements[a3].href;
> });
> 
> ```

> ```
> }
> };
> 
> ```

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [October 10, 2016, 8:10pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/5 "2016-10-10T20:10:31Z")

</div>

Yeah, $(document).ready will only fire once, as page repaints will not reload the DOM. You need to subscribe to a different event.

The API seems to offer a `onPageChange` event.

You would invoke it like so

```plaintext
import { withPluginApi } from 'discourse/lib/plugin-api';

function initializePlugin(api)
{
  api.onPageChange((url, title) => {
        // your code here
        console.log('the page changed to: ' + url + ' and title ' + title);
  });
}

export default {
  name: 'dynamic-table',
  initialize: function(container)
  {
    withPluginApi('0.1', api => initializePlugin(api));
  }
};

```

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [October 11, 2016, 10:07am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/6 "2016-10-11T10:07:07Z")

</div>

thank you very much. It helps me and was so useful

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [October 15, 2016, 6:51am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/7 "2016-10-15T06:51:02Z")

</div>

Thank you.Can you explain this code?  
I don’t understand exactly what this is?

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [October 15, 2016, 11:11am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/8 "2016-10-15T11:11:36Z")

</div>

In short, it is importing the plugin-api library and then using that API’s onPageChange event to execute code of your choosing.

So when the user navigates to a “different page”, it will invoke anything in the `//your code here` area.

---

<div class="post-metadata">

### Author: ![broz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/broz/32/61170_2.png) [@broz](https://meta.discourse.org/u/broz)
#### Post date: [November 11, 2016, 12:15am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/9 "2016-11-11T00:15:32Z")

</div>

Hi,

Based on your example is this the correct syntax?

```
import { withPluginApi } from 'discourse/lib/plugin-api';

function initializePlugin(api)
{
  api.onPageChange((url, title) => {
  name: 'optin',
  initialize() {
    alert('alert boxes are annoying!');
  }
    console.log('the page changed to: ' + url + ' and title ' + title);
  });
}

export default {
  name: 'optin',
  initialize: function(container)
  {
    withPluginApi('0.1', api => initializePlugin(api));
  }
};

```

[https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/discourse/initializers/alert.js.es6](https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/discourse/initializers/alert.js.es6)

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [November 11, 2016, 2:20am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/10 "2016-11-11T02:20:28Z")

</div>

You don’t need this

```plaintext
  name: 'optin',
  initialize() {
    alert('alert boxes are annoying!');
  }

```

That would simply be

```plaintext
    alert('alert boxes are annoying!');

```

Making the full script

```plaintext
import { withPluginApi } from 'discourse/lib/plugin-api';

function initializePlugin(api)
{
  api.onPageChange((url, title) => {
        // your code here
        alert('alert boxes are annoying!');
        console.log('the page changed to: ' + url + ' and title ' + title);
  });
}

export default {
  name: 'dynamic-table',
  initialize: function(container)
  {
    withPluginApi('0.1', api => initializePlugin(api));
  }
};

```

---

<div class="post-metadata">

### Author: ![broz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/broz/32/61170_2.png) [@broz](https://meta.discourse.org/u/broz)
#### Post date: [November 19, 2016, 11:04am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/11 "2016-11-19T11:04:06Z")

</div>

Hi, i’ve tried in every way but still the code isn’t loaded.  
I let you check the plugin:

> <https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/lib/discourse-markdown/discourse-opt-in.js.es6>

Do i miss something? Should i change the file name or the file extension to make the last part work?Because the first one work as it should.

Thank you very much for the help you gave me last time 💓

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [November 19, 2016, 12:27pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/12 "2016-11-19T12:27:15Z")

</div>

You put the code in the wrong file. That file is for whiteListing tags only.

You need to put the following

> <https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/lib/discourse-markdown/discourse-opt-in.js.es6#L21-L82>

in [https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/discourse/initializers/alert.js.es6](https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/discourse/initializers/alert.js.es6)

I would also rename alert.js.es6 to be discourse-opt-in.js.es6 so it more closely matches your plugin name.

---

<div class="post-metadata">

### Author: ![broz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/broz/32/61170_2.png) [@broz](https://meta.discourse.org/u/broz)
#### Post date: [November 19, 2016, 5:14pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/13 "2016-11-19T17:14:40Z")

</div>

I’ve tried this way, and i did that also now, you can try the plugin, and as you will see the code isn’t loaded.  
Maybe you can try to achieve this result with a simple alert, but actually i’ve tried it and still don’t work.

😓

[https://github.com/BRoz/discourseoptin](https://github.com/BRoz/discourseoptin)

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [November 19, 2016, 5:33pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/14 "2016-11-19T17:33:22Z")

</div>

I hadn’t looked at the actual code you had in there, but now after looking at it, it seems you may want to place the majority of this code in a separate JS file and in your plugin.rb you would do a `register_asset "path to js file", :server_side`

You can see something similar at

> <https://github.com/discourse/discourse-pirate-speak/blob/main/plugin.rb>

Then you would call your external javascript function in the plugin api onPageChange.

Right now, you have a lot of code in there that likely doesn’t belong. It isn’t meant to be housing functions, but more for calling them.

---

<div class="post-metadata">

### Author: ![broz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/broz/32/61170_2.png) [@broz](https://meta.discourse.org/u/broz)
#### Post date: [November 19, 2016, 5:43pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/15 "2016-11-19T17:43:31Z")

</div>

> [@cpradio](#):
>
> Then you would call your external javascript function in the plugin api onPageChange.

How to do that?  
I created the js file  
[https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/discourse/initializers/discourse-opt-in.js](https://github.com/BRoz/discourseoptin/blob/master/assets/javascripts/discourse/initializers/discourse-opt-in.js)

And included it in the plugin.rb

> <https://github.com/BRoz/discourseoptin/blob/master/plugin.rb>

Thanks

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [November 19, 2016, 5:55pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/16 "2016-11-19T17:55:03Z")

</div>

I’m pretty well guessing at this point, but I think you have to change `! function` to be `var myFunction = function`

Then remove `(document, "script", "omapi-script")`

And in your plugin api, do something like  
`myFunction(document, "script", "omapi-script");`

I’m not even sure this is fully correct, but your javascript doesn’t like to be embeded in such an API, and it seems you haven’t a clue as to what it is doing… so we’re sort of at an impasse here because unless you know what it is doing, it is hard to tell you how to change it so it can be executed.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [November 20, 2016, 12:53am UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/17 "2016-11-20T00:53:13Z")

</div>

Note that “register\_asset” starts from the asset folder.

so it should be more like  
`register_asset "javascripts/discourse/....`  
instead of  
`register_asset "assets/javascripts/discourse/....`

Also, AFAIK if you name the files with the extension “.js.es6” it lets magic happen and you don’t need to register them.

> [@cpradio](#):
>
> haven’t a clue as to what it is doing

Using more descriptive naming would be a help. Names like “r”, “t”, “e” and “s” don’t make it any easier to read the code.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [November 21, 2016, 10:10pm UTC](https://meta.discourse.org/t/my-plugin-codes-runs-just-once-my-own-script-shouldnt-be-cached/51301/18 "2016-11-21T22:10:29Z")

</div>

@broz

If you can figure out how to make your javascript a function and not execute it immediately, then you can simply put that function statement at the bottom of your initializer and call it from within the onPageChange invocation.

Similar to what the poll plugin is doing here

> [@Existing JS module import into ES6 plugin](https://meta.discourse.org/t/existing-js-module-import-into-es6-plugin/52713/2):
>
> This is an example of plugin that modifies post cooking: [https://github.com/discourse/discourse/blob/master/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6](https://github.com/discourse/discourse/blob/master/plugins/poll/assets/javascripts/lib/discourse-markdown/poll.js.es6)
