Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Adding javascript code after script is enqueued
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Milan Dinić  
View profile  
 More options Sep 14 2009, 11:37 pm
From: Milan Dinić <li...@srpski.biz>
Date: Tue, 15 Sep 2009 05:37:00 +0200
Subject: [wp-hackers] Adding javascript code after script is enqueued
Is there way to add javascript code immediately after script is enqueued? I
found function wp_localize_script, but it adds code before script enqueuing.

I am asking this because of following problem. jQuery file included in
WordPress is different from official one in that it has jQuery.noConflict at
it end. If we use jQuery from Google AJAX Libraries, there is noconflict so
we need to add it.

Author of plugin Use Google Libraries (which enables use of files from
Google’s servers) solved this by enqueuing file jQnc.js (which whole content
is jQuery.noConflict(); ) . It is lightweight but it needs another request
and headers could be up to kilobyte.

It would be better if noConflict code is added in html of page, it will not
increase page size, but it will remove one request. I asked Jason could he
enable this but he said that he is not aware of way to do this with WP API.

So, does anyone knows solution for this?

Thanks
_______________________________________________
wp-hackers mailing list
wp-hack...@lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tynan Colin Beatty  
View profile  
 More options Sep 15 2009, 3:04 pm
From: Tynan Colin Beatty <junsui...@gmail.com>
Date: Tue, 15 Sep 2009 14:04:55 -0500
Local: Tues, Sep 15 2009 3:04 pm
Subject: Re: [wp-hackers] Adding javascript code after script is enqueued
I just tested this approach successfully for the head situation:
{{{
function my_jquery_test() {
    global $wp_scripts;

    if ( is_a( $wp_scripts, 'WP_Scripts' ) && in_array( 'jquery',
$wp_scripts->queue ) ) {

        if ( $wp_scripts->registered['jquery']->extra['group'] == 1 ) {
            remove_action( 'wp_footer', 'wp_print_footer_scripts' );
            add_action( 'wp_footer', 'my_print_footer_scripts' );
        }
        else {
            remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
            add_action( 'wp_head', 'my_print_head_scripts', 9 );
        }

    }

}

add_action( 'wp', 'my_jquery_test', 0 );

    function my_print_head_scripts() {
        $rest = my_print_scripts_prep();

        wp_print_head_scripts();

        my_print_scripts_finish( $rest );

        wp_print_head_scripts();
    }

    function my_print_footer_scripts() {
        $rest = my_print_scripts_prep();

        wp_print_footer_scripts();

        my_print_scripts_finish( $rest );

        wp_print_footer_scripts();
    }

    function my_print_scripts_prep() {
        global $wp_scripts;

        $offset = array_search( 'jquery', $wp_scripts->queue );

        $rest = array_slice( $wp_scripts->queue, $offset );

        $wp_scripts->queue = array_slice( $wp_scripts->queue, 0, $offset + 1
);

        $wp_scripts->print_html = "<script
type='text/javascript'>jQuery.noConflict();</script>\n";

        return $rest;
    }

    function my_print_scripts_finish( $array ) {
        global $wp_scripts;

        $wp_scripts->queue = $array;
    }

}}}

This method is the easiest way i could think of to get the noconflict to
load directly after the jquery script (in case other scripts that depend
upon it are also enqueued for the same spot). The hooks provided in this
process do not allow for any other simpler approaches i could think up at
this time.

Another possible approach is to create your own version of the
wp-admin/load-scripts.php script concatenator.

peace~

_______________________________________________
wp-hackers mailing list
wp-hack...@lists.automattic.com
http://lists.automattic.com/mailman/listinfo/wp-hackers

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »