Can I include Javascript/jQuery in a page?

I’m extremely new to WordPress, and I’ve offered to help out with a project that uses it.

In a new page I’m creating, I need to use javascript/jQuery to write out the html dynamically – it doesn’t seem to be working? I believe I read there was something special that needed to be done, but I can’t figure out. I tried changing the $ to jQuery, but that didn’t work either.

Read More
<script type="text/javascript">
$(document).ready(function(){
     alert('hoi');
     jQuery.ajax({
        type: "GET",
        url: "link-here",
        dataType: "text",
        success: function(data) {alert(CSVToArray(data));}
        error: function(){ alert('error');}
     });
</script>

I have this at the top – the alert isn’t even called. How can I get this working?

Edit It looks like jQuery is loaded for all but the admin page – correct?

//jQuery Insert From Google
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
   wp_deregister_script('jquery');
   wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", false, null);
   wp_enqueue_script('jquery');
}

Edit Two It looks like the code I wrote is being wrapped in <p> tags. This must mean that the script doesn’t belong in the New Page. Where should the code go?

Related posts

Leave a Reply

2 comments

  1. You need to make sure that jQuery is loaded, so that you can use it.

    In functions.php:

    function namespace_enqueue_scripts() {
        if ( is_page( 'slug-of-new-page-you-are-creating' ) )
            wp_enqueue_script('jquery');
    }
    add_action( 'wp_enqueue_scripts', 'namespace_enqueue_scripts' );