• 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

    (@bcworkz)

    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.