Paolo Bergomi
Paolo Bergomi

Reputation: 25

Jquery script in html page do not work

I do have a php page in my WP site, and I am trying to add a Jquery script directly in the php. basically something like this , the structure...**

<?php
/*
Template Name: Gallery Page
*/
?>
<?php get_header(); ?>


<?php
     //here i have my working php code
?>               
      //here some HTML           




  <script  language="JavaScript" type="text/javascript"    src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> 



                           alert("hi");


 </script>


        <?php
                endwhile;

                    else :

                    // no rows found

                    endif;



        ?>


 <div class="spacing"></div>

<?php get_footer(); ?>

Anyone knows why my javascript code dont work if i set like I set in the script,. while it works if I only type

<script>

 alert("Hi");

</script>

Also: if I link jquery in the footer, and call a external script in my site with this code above, it works. The alert starts. Anyone knows why? it seems correct what i did and i am struggling to get over this.

thanks in advance

Upvotes: -1

Views: 623

Answers (3)

Chrillewoodz
Chrillewoodz

Reputation: 28368

Remove language from script tag:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

And also make sure that you close the script tag before you call the alert. Actually just remove the alert altogether.

Important note: Since you're using WordPress you don't even have to include jQuery on your page since this comes with WordPress from the start.

To include a main.js script file in WordPress;

  • Create a functions.php file
  • Paste the following snippet into this file:
  • <?php
        function include_my_scripts() {
          wp_enqueue_script('main', 'scripts/main.js', array('jquery'), '', true);
        }
        add_action('wp_enqueue_scripts', 'include_my_scripts');
    ?>
    

    This will name the script to main, and it's located at scripts/main.js, it depends on jQuery, it has no version, and it should be included at the bottom of the page.

    Check the WordPress codex for additional information.

    Also script including should be done from functions.php and not from your templates.

    Upvotes: 1

    user4999841
    user4999841

    Reputation:

    Since your question is WordPress related, you must use enqueue_scripts function to load JS.

    In case of general JS, as mentioned in the previous answers, you may use <script type="text/javascript" src="...url..."></script> to load a script from a given URL or <script type="text/javascript">...code...</script> to execute JS.

    Reference: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    Upvotes: 0

    Tsahi Asher
    Tsahi Asher

    Reputation: 1812

    Like @Chrillewoodz said, language attribute is deprecated. Also, you can't write code in a <script> tag that has a src attribute. You must choose between the two. You should move the alert() to a separate <script> tag. You can also self-close the tag that loads the external script: <script src=... />.

    Upvotes: 0

    Related Questions