# How do you force a script to fire on Page Not Found?

**URL:** <https://meta.discourse.org/t/how-do-you-force-a-script-to-fire-on-page-not-found/240362>\
**Category:** Development\
**Created:** [September 29, 2022, 4:33am UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-fire-on-page-not-found/240362 "2022-09-29T04:33:25Z")\
**Posts on this page:** 1\
**Page:** 1

## AI-generated summary

[markschmucker](/t/-/240362/1) seeks a method to force a script to fire specifically on the "Page Not Found" (404) error page in Discourse. Referencing a previous discussion on triggering scripts on every page load, the user notes that standard approaches like `api.onPageChange` and `$( document ).ready` fail to execute on 404 errors. The goal is to implement a redirection logic, similar to the provided example code for category redirects, but applicable to the 404 state.

<div class="post-metadata">

**Author:** ![markschmucker](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/markschmucker/32/141599_2.png) [@markschmucker](https://meta.discourse.org/u/markschmucker)\
**Post date:** [September 29, 2022, 4:33am UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-fire-on-page-not-found/240362/1 "2022-09-29T04:33:25Z")

</div>

Continuing the discussion from [How do you force a script to refire on every page load in Discourse?](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/28):

> [@How do you force a script to refire on every page load in Discourse?](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/28):
>
> I am currently attempting to use this API to trigger a redirection from a category to an external site…
> 
> ```plaintext
> <script type='text/discourse-plugin' version='0.8.19'>
> $( document ).ready(function() {
> if ( window.location.href === "https://omnifora.com/c/redirect-politifora" ) {
> window.location.replace( "https://politifora.com/" );
> }
> });
> </script>
> 
> ```

As discussed in the now-closed topic, the code above does fire when a **topic** is loaded. But I want to trigger a redirection when the **Page Not Found** page is loaded. Neither `api.onPageChange` nor `$( document ).ready` fire on a 404 error. How can I catch a 404 and redirect?
