Skip to content

Commit 4980faa

Browse files
author
parisholley
committed
Merge branch 'master' of github.com:parisholley/wordpress-asynchronous-javascript
git-svn-id: http://plugins.svn.wordpress.org/asynchronous-javascript/trunk@654901 b8457f37-d9ea-0310-8a92-e5e31aec5664
2 parents cb5896a + 7241f0a commit 4980faa

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed

README.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== Asynchronous Javascript ===
2+
Contributors: parisholley
3+
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paris%40holleywoodproductions%2ecom&lc=US&item_name=Paris%20Holley&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest
4+
Tags: async,headjs,asynchronous,performance,javascript
5+
Requires at least: 3.5
6+
Tested up to: 3.5
7+
Stable tag: trunk
8+
9+
== Description ==
10+
11+
Improve page load performance by asynchronously loading javascript using head.js
12+
13+
14+
== Installation ==
15+
16+
1. Upload `asynchronous-javascript` folder to the `/wp-content/plugins/` directory
17+
2. Activate the plugin through the 'Plugins' menu in WordPress
18+
19+
== Frequently Asked Questions ==
20+
21+
= How do I include a script that I do not control but has a dependency on another script? =
22+
23+
In your theme, use the static AsynchronousJS::wp_enqueue_async_script() method, which is API compatible with wp_enqueue_script($handle, $src, $deps). Using this method will cause the $src to load ONLY when scripts with the handles defined in $deps have been loaded.
24+
25+
= Is there any potential problems with other plugins? =
26+
27+
As long as those plugins are using the built-in wordpress script queuing and not attempting to output scripts manually (such as invoking wp_print_scripts()), you should have no problems.
28+
29+
== Changelog ==
30+
31+
= 1.0 =
32+
* Initial release, seems to work. :)

asynchronous-javascript.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/*
3+
Plugin Name: Asynchronous Javascript
4+
Plugin URI: https://github.com/parisholley/wordpress-asynchronous-javascript
5+
Description: Improve page load performance by asynchronously loading javascript and files using head.js in your wordpress website.
6+
Version: 1.0
7+
Author: Paris Holley
8+
Author URI: http://www.linkedin.com/in/parisholley
9+
Author Email: mail@parisholley.com
10+
License:
11+
12+
Copyright 2013 Paris Holley (mail@parisholley.com)
13+
14+
This program is free software; you can redistribute it and/or modify
15+
it under the terms of the GNU General Public License, version 2, as
16+
published by the Free Software Foundation.
17+
18+
This program is distributed in the hope that it will be useful,
19+
but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
GNU General Public License for more details.
22+
23+
You should have received a copy of the GNU General Public License
24+
along with this program; if not, write to the Free Software
25+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26+
27+
*/
28+
class AsynchronousJS {
29+
private static $queue = array();
30+
private static $depends = array();
31+
private static $head_loaded = false;
32+
33+
function init() {
34+
if(!defined('WP_ADMIN') || !WP_ADMIN){
35+
add_action('wp_print_scripts', 'AsynchronousJS::action_prevent_script_output' );
36+
add_filter('script_loader_src', 'AsynchronousJS::filter_queue_script', 10, 2 );
37+
add_filter('print_footer_scripts', 'AsynchronousJS::filter_headjs' );
38+
add_filter('print_head_scripts', 'AsynchronousJS::filter_headjs' );
39+
}
40+
}
41+
42+
/**
43+
* Prevent wordpress from outputing scripts to page
44+
**/
45+
function action_prevent_script_output() {
46+
global $wp_scripts, $concatenate_scripts;
47+
48+
$concatenate_scripts = true;
49+
$wp_scripts->do_concat = true;
50+
}
51+
52+
53+
/**
54+
* Wordpress has no ability to hook into script queuing, so this is a work around
55+
**/
56+
function filter_queue_script($src, $handle) {
57+
self::$queue[$handle] = "{'{$handle}': '$src'}";
58+
}
59+
60+
/**
61+
* Outputs headjs code in header or footer
62+
**/
63+
function filter_headjs(){
64+
if(count(self::$queue) > 0){
65+
if(!self::$head_loaded){
66+
echo '<script type="text/javascript" src="' . plugins_url( '/js/head.load.min.js', __FILE__ ) . '"></script>';
67+
68+
self::$head_loaded = true;
69+
}
70+
71+
echo '<script type="text/javascript">head.js(' . implode(',', self::$queue) . ')</script>';
72+
73+
self::$queue = array();
74+
}
75+
76+
if(count(self::$depends) > 0){
77+
foreach(self::$depends as $handle => $depend){
78+
if(is_array($depend['deps'])){
79+
echo '<script type="text/javascript">head.ready("' . implode(',', $depend['deps']) . '", function(){head.js({"' . $handle . '": "' . $depend['src'] . '"})})</script>';
80+
}elseif(is_string($depend['deps'])){
81+
echo '<script type="text/javascript">head.ready("' . $depend['deps'] . '", function(){head.js({"' . $handle . '": "' . $depend['src'] . '"})})</script>';
82+
}else{
83+
echo '<script type="text/javascript">head.js({"' . $handle . '": "' . $depend['src'] . '"});</script>';
84+
}
85+
}
86+
87+
self::$depends = array();
88+
}
89+
90+
return false; // prevent printing of javascript
91+
}
92+
93+
function wp_enqueue_async_script($handle, $src, $deps){
94+
self::$depends[$handle] = array(
95+
'src' => $src,
96+
'deps' => $deps
97+
);
98+
}
99+
}
100+
101+
AsynchronousJS::init();
102+
?>

js/head.load.min.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)