cache.js

来自「ajax patterns 这是关于ajax设计模式方面的原代码」· JavaScript 代码 · 共 79 行

JS
79
字号
/*    Copyright 2006 Christian Gross http://www.devspace.com    From the book Ajax Patterns and Best Practices   Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at       http://www.apache.org/licenses/LICENSE-2.0   Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.*/var CacheController = {    _cache : new Array(),    prefetch : function( url) { },    didNotFindETagError : function( url) { },        getCachedURL : function( url) {        var func = function( status, statusText, responseText, responseXML) { }        CacheController.getURL( url, func, true);    },            getURL : function( url, complete, calledFromCache) {        var asynchronous = new Asynchronous();        asynchronous.openCallback = function( xmlhttp) {            var obj = CacheController._cache[ url];            if( obj != null) {                xmlhttp.setRequestHeader( "If-None-Match", obj.ETag);            }        }        asynchronous.userComplete = complete;        asynchronous.complete = function( status, statusText, responseText, responseXML) {            if( status == 200) {                try {                    var foundetag = this._xmlhttp.getResponseHeader( "ETag");                    if( foundetag != null) {                        CacheController._cache[ url] = {                            ETag : foundetag,                            Status : status,                            StatusText : statusText,                            ResponseText : responseText,                            ResponseXML : responseXML                        };                    }                    else {                        CacheController.didNotFindETagError( url);                    }                }                catch( exception) {                    CacheController.didNotFindETagError( url);                }                this.userComplete( status, statusText, responseText, responseXML);            }            else if( status == 304) {                var obj = CacheController._cache[ url];                if( obj != null) {                    this.userComplete( 304, obj.StatusText,                         obj.ResponseText, obj.ResponseXML);                }                else {                    throw new Error( "Server indicated that this data is in the cache, but it does not seem to be");                }            }            else {                this.userComplete( status, statusText, responseText, responseXML);            }        }        asynchronous.get( url);        if( calledFromCache != true) {            CacheController.prefetch( url);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?