Open Source Web Development Tutorials - Dev Shed
GoogleのAjaxライブラリAPIの利用時にソースファイルを圧縮しない
(2009/03/09公開)
圧縮されていないJavaScriptファイルを配布する
Google APIを利用して圧縮を使用せずにPrototypeライブラリのソースファイルをダウンロードする方法を示すために、前のセクションでお目にかけたAjaxベースのアプリケーションを修正してみよう。この場合、例のgoogle.load()メソッドに、ライブラリのサービスを圧縮しないで行うように指定するためのパラメータを追加する。
つまり、圧縮を使用せずにPrototypeパッケージをダウンロードするように修正したバージョンは次のようになる。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Reading file contents with Prototype library (uses Google API)</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: bold 18pt Arial, Helvetica, sans-serif;
color: #000;
}
#filecontents{
width: 600px;
padding: 10px;
border: 1px solid #999;
font: normal 10pt Arial, Helvetica, sans-serif;
color: #000;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
// load Prototype library with Google API (loads uncompressed source file)
google.load("prototype", "1.4",{uncompressed:true});
// read file contents with Ajax
function readFileContents(){
// send http request
var Ajaxobj=new Ajax.Request('read_file.php',{method: 'get',onComplete: displayFileContents,onFailure: displayError});
}
// display file contents
function displayFileContents(requestObj){
$('filecontents').innerHTML=requestObj.responseText;
}
// display error message
function displayError(requestObj){
$('filecontents').innerHTML='Error reading file contents!';
}
// initialize file reading application
function initializeApplication(){
// attach click handler to HTML button
Event.observe('btn','click',readFileContents);
}
google.setOnLoadCallback(initializeApplication);
</script>
</head>
<body>
<h1>Reading File Contents with Prototype library (uses Google API)</h1>
<p><input type="button" id="btn" value="Read File Now!" /></p>
<div id="filecontents"></div>
</body>
</html>
上の例でお分かりのように、パラメータ「uncompressed: true」を追加してgoogle.load()メソッドを呼び出す。 この値は、圧縮しないでPrototypeパッケージのサービスを行うように、APIに指示するためのものだ。
もちろん、通常は、圧縮を使用して指定したライブラリのソースファイルをダウンロードすることが望ましいが、「uncompressed」オプションを選択したい場合もあると思う。
さて、これで、HTTP圧縮を使用せずにライブラリをダウンロードするようにGoogle APIに指示する方法が理解できたと思う。ところで、前回のアプリケーションは、現状では実際には不完全であることに気付かれていると思う。残りのソースファイルをインクルードする必要がある。
これを考慮に入れ、このチュートリアルの最後のセクションでは、このAjaxサンプルプログラムの完全なソースコードを示すことにする。もっと簡単に試してみることができると思う。
先に進んで、読み続けてほしい。ほぼ完了、終わりは近い!
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








