Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress: load jQuery as a dependency w/ local fallback question

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #35743
    Jon Yablonski
    Participant

    I am trying to load jQuery from the Google cdn with a local jQuery fallback, while also loading an additional script file for my own js. My questions are:

    — How would I load the local fallback file in case the Google cdn fails, in addition to the following code?
    — Is the following code efficient and well written?
    — And finally, how would I load multiple script files (like how the theme.js file is being loaded)?

    Thanks in advance!
    – j

    /* Add jQuery as a dependency */

    function my_scripts_method() { // Better than init hook

    wp_deregister_script(‘jquery’); // Remove the inbuilt jquery
    wp_register_script(‘jquery’, ‘http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js’, false, ‘1.3.2’, true); // Include the google cdn jQuery
    wp_enqueue_script(‘jquery’); // Load jQuery

    // Load a JS file from my theme: js/theme.js
    wp_enqueue_script(
    ‘my_script’, // Name of the script
    get_bloginfo(‘template_url’) . ‘/js/theme.js’, // Location
    array(‘jquery’), //Dependencies
    ‘1.0’, // Script version
    true); // Load in footer.( wp_footer() must be present!)
    }
    add_action(‘wp_enqueue_scripts’, ‘my_scripts_method’);

Viewing 1 post (of 1 total)
  • The forum ‘Other’ is closed to new topics and replies.