I just want to create an alert box when a page with a post is reloaded.

<script type="text/javascript">

And here is the php code i wrote to load the js.

function alert_js(){
   wp_enqueue_script('alert_js', plugins_url( '/alert.js', __FILE__ ));
add_action('the_post', 'alert_js');

I'm sure that it successfully finds the script in right place(I checked the page source.). But somehow it acts like there is no javascript included. What I'm missing there?

  • This is more of a question, then an answer, however… Do you have script tags in your JavaScript file? – whakawaehere Apr 10 '17 at 21:47

I think you are missing the trigger... your javascript should be something like this in order for it to trigger when the page is loaded:

$( document ).ready(function() {
    alert( "ready!" );
| improve this answer | |
  • Also, you should use wp_enqueue_scripts hook to load stylesheets and scripts as best practice. Mark this answer as best answer if solved your problem. – Caio Mar Apr 10 '17 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.