>
 



Recently, I have written a C# wrapper for Google Closure Compiler to use with ASP.NET for on-the-fly JavaScript compression. For those who want to use the Closure Compiler with PHP, Bohuco.net has an article showing how to use the Google Closure Compiler over its RESTful API with PHP5.  

$script = file_get_contents('http://www.domain.com/scripts/script.js');
$ch = curl_init('http://closure-compiler.appspot.com/compile');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($script));
$output = curl_exec($ch);
curl_close($ch);

You can find more details on using Closure Compiler with PHP 5 here.

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



On November 5th, Google released Closure Compiler for Javascript optimization. The strongest part of the Closure Compiler is that it does not only remove whitespace but also rewrites Javascript Code to make it smaller. It also optimizes your Javascript code to perform better. You can find more details and all of its features on my previous article: Closure Compiler – Javascript Compiler by Google.

During my tests, Closure Compiler reduced Javascripts more than 60%. Considering, the size of Javascript files that modern websites have and the reduction before HTTP compression, Closure Compiler is really useful. 

Closure Compiler comes with a Java based command line tool and a RESTful API.  RESTful API works great and very fast. API can be used with a MSBuild or NAnt script to automatically compress Javascript files.

I wrote a C# wrapper for the Google Closure Compiler:

Google's Javascript compressor - C# wrapper
C# wrapper for the Google Closure Compiler

 

More...

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



The build in Page.ClientScript object allows you to place JavaScript inside an ASP.NET page. We cannot use this object to place JavaScript code or JavaScript include file into the head section of the page.

With ASP.NET 2.0, Visual Studio 2005 automatically places the runat="server" attribute  in the head tag of each page which allows us to interact with the page header.

You may add JavaScript code inside the page header with the following code block:

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.InnerHtml = "alert('Hello World');";
this.Page.Header.Controls.Add(include);

And, you may add JavaScript include file inside the page header with the following code block:

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src", "/jsInclude.js");
this.Page.Header.Controls.Add(include);

Fortunately, Simone B created a library, to include/register scripts and style sheets into the head portion of an ASP.NET page.

HeadScriptManager Class Diagram
HeadScriptManager Class Diagram


In order to use the class library, reference the assembly, create an instance of HeadScriptManager and call its methods.

HeadScriptManager hm = HeadScriptManager.Current;
hm.RegisterHeadScriptResource(typeof(jTip), "jTip.jTip.js");

Source and binaries are available at sourceforge.net.

HeadScriptManager Sample Code
HeadScriptManager Sample Code

 

Currently rated 4.5 by 4 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5