Title: Executing Javascript on one page
Last modified: August 31, 2016

---

# Executing Javascript on one page

 *  [Mike Seiler](https://wordpress.org/support/users/mike-seiler/)
 * (@mike-seiler)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/executing-javascript-on-one-page/)
 * I have an alert function in an updatepage.js file:
    `function hello() {alert ('
   Hello world!');}` On my post page (set to ‘text’ view) I have
 *     ```
       <script type="text/javascript" src="/updatepage.js" </script>
       <script type="text/javascript"> <!-- hello(); --></script>
       ```
   
 * When I refresh the page, I don’t get the alert.
    I’m working up to having the
   alert message show with every WordPress heartbeat, but one step at a time.

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 11 months ago](https://wordpress.org/support/topic/executing-javascript-on-one-page/#post-7453766)
 * The relative path to your JS file is likely incorrect. You should stay away from
   relative paths. Use PHP functions like `plugin_dir_url( __FILE__ ) or get_stylesheet_directory_uri()`
   to get the absolute URI to your plugin or theme folder, concatenating any other
   path folders and the filename to the result.
 * To initiate server side code on the WP heartbeat, determine the ‘action’ value
   of the heartbeat AJAX request, then hook the AJAX action based on that value.
   Let’s assume the action value is ‘heartbeat’ (verify this is actually the case).
   Add an action callback to ‘wp_ajax_heartbeat’ and ‘wp_ajax_nopriv_heartbeat’ 
   that does what you want.
 * Client side, you’d need to determine what JS or jQuery code triggers the heartbeat
   AJAX request. Override this function with a version that includes your heartbeat
   code by declaring your version after the default version.
 * I’m assuming you don’t really want to have an alert box pop up on every heartbeat,
   that would be really annoying! Whatever you really want to do can be accomplished
   with one those approaches.

Viewing 1 replies (of 1 total)

The topic ‘Executing Javascript on one page’ is closed to new replies.

## Tags

 * [external file](https://wordpress.org/support/topic-tag/external-file/)
 * [heartbeat](https://wordpress.org/support/topic-tag/heartbeat/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [9 years, 11 months ago](https://wordpress.org/support/topic/executing-javascript-on-one-page/#post-7453766)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
