# How to use api.onPageChange with api.createWidget?

**URL:** https://meta.discourse.org/t/how-to-use-api-onpagechange-with-api-createwidget/242744
**Category:** Development
**Tags:** ember
**Created:** [October 24, 2022, 2:55am UTC](https://meta.discourse.org/t/how-to-use-api-onpagechange-with-api-createwidget/242744 "2022-10-24T02:55:24Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![rahim123](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@rahim123](https://meta.discourse.org/u/rahim123)
#### Post date: [November 6, 2022, 2:29am UTC](https://meta.discourse.org/t/how-to-use-api-onpagechange-with-api-createwidget/242744/10 "2022-11-06T02:29:10Z")

</div>

Hi again, I’m trying to figure out the best way to only make the widget show on a specific URL, such as the homepage.

The easy way is simply to use a plugin outlet that only exists on the homepage, which works for what I need for now (specifically, the `discovery-navigation-bar-above`). But I’m still curious about how to do this programmatically in a way that is sensitive to the specific page URL.

I found this very helpful topic, also by @awesomerobot :

> [@Add custom content that only appears on your homepage](https://meta.discourse.org/t/add-custom-content-that-only-appears-on-your-homepage/131415):
>
> A very common situation you’ll find yourself in as a theme developer is the need to create content that only shows on the homepage of your community. You might add some HTML to the “After Header” section of your theme, which will then appear on every page. You can jump through some hoops in CSS to hide this everywhere except the homepage… but instead let’s use a Discourse theme to create a component with content that is only visible on your homepage. If you’re unfamiliar with Discourse themes …

I tried to adapt this into the solution from earlier in this post:

```plaintext
        api.onPageChange((url) => {
            if (url === "/" || url === homeRoute ){
               this.scheduleRerender();
            }
        });

```

But this still makes the image appear on all pages. I also tried to put my variables and random selection code inside the `if` clause, but that doesn’t work at all.

There’s also the `<script type="text/x-handlebars" ...` section of the example, but it seems to only allow for HTML, and I don’t know how to get the variables into it from the previous script.

---

_[View the full topic](https://meta.discourse.org/t/how-to-use-api-onpagechange-with-api-createwidget/242744)._
