Open Source Web Development Tutorials - Dev Shed
GoogleのAjaxライブラリAPIの利用時にソースファイルを圧縮しない
(2009/03/09公開)
圧縮を使用してJavaScriptファイルのサービスを行う
Google APIを利用して、圧縮していないJavaScriptファイルの配布方法を説明する前に、Prototypeライブラリの圧縮バージョンを使用する簡単なAjaxプログラムを作成したいと思う。
そのために、このシリーズの最初に作成した実際的な例を再び使用することにする。この例は明快で簡単に理解できるからである。この例を構成しているソースファイルのセットは次のようになる。
(definition for 'Ajax_file_reader.htm' file)
<!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
google.load("prototype", "1.6");
// 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>
(definition for 'read_file.php' file)
<?php
if(!$contents=file_get_contents('data.txt')){
trigger_error('Error reading file contents',E_USER_ERROR);
}
echo $contents;
?>
(definition for 'data.txt' file)
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
These are contents of sample file. These are contents of sample file.
このプログラムの機能については、前回のチュートリアルで詳しく説明したので、その説明に長い時間を費やすことはしない。 ただ、このサンプルアプリケーションは、Prototypeパッケージの圧縮バージョンのサービスを行っていることを知ることが大切だ。
その機能が、このサンプルWebアプリケーションのどこで有効に設定されているのかと問われたら、答えは簡単だ、どこでもない。実際には、google.load()メソッドを、3番目の引数を使用せずに呼び出すと、リクエストしたライブラリは圧縮形式で配布される。
ただし、このメソッドに、3番目のパラメータを渡すことにより、この圧縮プロセスを無効にすることができる。 次のセクションで、圧縮されていないJavaScriptファイルをGoogle APIを利用して配布する方法を詳しく説明しよう。
その方法に興味があるなら、下のリンクをクリックして、この先を読んでほしい。圧縮を使用せずにJavaScriptファイルのサービスを行う。
Copyright © 2008 Ziff Davis Enterprise, Inc.
Originally appearing in the U.S. Edition of Dev Shed. All Rights Reserved.








