display.php-n=19-3&f=19-pobject.js.htm

来自「javascript 5 随书源码 javascript」· HTM 代码 · 共 418 行 · 第 1/2 页

HTM
418
字号
    // Just delegate to a private, implementation-defined $init() method.    this.$init(name, defaults, onload);} // Save the current state of this PObject for at least the specified # of days.PObject.prototype.save = function(lifetimeInDays) {    // First serialize the properties of the object into a single string    var s = "";                           // Start with empty string    for(var name in this) {               // Loop through properties        if (name.charAt(0) == "$") continue;  // Skip private $ properties        var value = this[name];               // Get property value        var type = typeof value;              // Get property type        // Skip properties whose type is object or function        if (type == "object" || type == "function") continue;        if (s.length &gt; 0) s += "&amp;";           // Separate properties with &amp;        // Add property name and encoded value        s += name + ':' + encodeURIComponent(value);    }    // Then delegate to a private implementation-defined method to actually    // save that serialized string.    this.$save(s, lifetimeInDays);};PObject.prototype.forget = function() {    // First, delete the serializable properties of this object using the    // same property selection criteria as the save() method.    for(var name in this) {        if (name.charAt(0) == '$') continue;        var value = this[name];        var type = typeof value;        if (type == "function" || type == "object") continue;        delete this[name];  // Delete the property    }    // Then erase and expire any previously saved data by saving the     // empty string and setting its lifetime to 0.    this.$save("", 0);};// Parse the string s into name/value pairs and set them as properties of this.// If the string is null or empty, copy properties from defaults instead// This private utility method is used by the implementations of $init() below.PObject.prototype.$parse = function(s, defaults) {    if (!s) {  // If there is no string, use default properties instead        if (defaults) for(var name in defaults) this[name] = defaults[name];        return;    }    // The name/value pairs are separated from each other by ampersands, and    // the individual names and values are separated from each other by colons.    // We use the split() method to parse everything.    var props = s.split('&amp;'); // Break it into an array of name/value pairs    for(var i = 0; i &lt; props.length; i++) { // Loop through name/value pairs        var p = props[i];        var a = p.split(':');     // Break each name:value pair at the colon        this[a[0]] = decodeURIComponent(a[1]); // Decode and store property    }};/*  * The implementation-specific portion of the module is below. * For each implementation, we define an $init() method that loads  * persistent data and a $save() method that saves it. */ // Determine if we're in IE, and, if not, whether we've got a Flash// plugin installed and whether it has a high enough version numbervar isIE = navigator.appName == "Microsoft Internet Explorer";var hasFlash7 = false;if (!isIE &amp;&amp; navigator.plugins) { // If we use the Netscape plugin architecture   var flashplayer = navigator.plugins["Shockwave Flash"];   if (flashplayer) {    // If we've got a Flash plugin       // Extract the version number       var flashversion = flashplayer.description;        var flashversion = flashversion.substring(flashversion.search("\\d"));       if (parseInt(flashversion) &gt;= 7) hasFlash7 = true;   }}if (isIE) {  // If we're in IE    // The PObject() constructor delegates to this initialization function    PObject.prototype.$init = function(name, defaults, onload) {        // Create a hidden element with the userData behavior to persist data        var div = document.createElement("div");  // Create a &lt;div&gt; tag        this.$div = div;                          // Remember it        div.id = "PObject" + name;                // Name it        div.style.display = "none";               // Make it invisible        // This is the IE-specific magic that makes persistence work.        // The "userData" behavior adds the getAttribute(), setAttribute(),        // load() and save() methods to this &lt;div&gt; element. We use them below.        div.style.behavior = "url('#default#userData')";          document.body.appendChild(div);  // Add the element to the document        // Now we retrieve any previously saved persistent data.        div.load(name);  // Load data stored under our name        // The data is a set of attributes.  We only care about one of these        // attributes.  We've arbitrarily chosen the name "data" for it.        var data = div.getAttribute("data");        // Parse the data we retrieved, breaking it into object properties        this.$parse(data, defaults);        // If there is an onload callback, arrange to call it asynchronously        // once the PObject() constructor has returned.        if (onload) {            var pobj = this;  // Can't use "this" in the nested function            setTimeout(function() { onload(pobj, name);}, 0);        }    }    // Persist the current state of the persistent object    PObject.prototype.$save = function(s, lifetimeInDays) {        if (lifetimeInDays) { // If lifetime specified, convert to expiration            var now = (new Date()).getTime();            var expires = now + lifetimeInDays * 24 * 60 * 60 * 1000;            // Set the expiration date as a string property of the &lt;div&gt;            this.$div.expires = (new Date(expires)).toUTCString();        }        // Now save the data persistently        this.$div.setAttribute("data", s); // Set text as attribute of the &lt;div&gt;        this.$div.save(this.$name);        // And make that attribute persistent    };}else if (hasFlash7) { // This is the Flash-based implementation    PObject.prototype.$init = function(name, defaults, onload) {        var moviename = "PObject_" + name;    // id of the &lt;embed&gt; tag        var url = "PObject.swf?name=" + name; // url of the movie file        // When the Flash player has started up and has our data ready,        // it notifies us with an FSCommand.  We must define a        // handler that is called when that happens        var pobj = this;  // for use by the nested function        // Flash requires that we name our function with this global symbol        window[moviename + "_DoFSCommand"] = function(command, args) {            // We know Flash is ready now, so query it for our persistent data            var data =  pobj.$flash.GetVariable("data")            pobj.$parse(data, defaults);    // Parse data or copy defaults            if (onload) onload(pobj, name); // Call onload handler, if any        };        // Create an &lt;embed&gt; tag to hold our Flash movie.  Using an &lt;object&gt;        // tag is more standards-compliant, but it seems to cause problems        // receiving the FSCommand.  Note that we'll never be using Flash with        // IE, which simplifies things quite a bit.        var movie = document.createElement("embed");  // element to hold movie        movie.setAttribute("id", moviename);          // element id        movie.setAttribute("name", moviename);        // and name        movie.setAttribute("type", "application/x-shockwave-flash");         movie.setAttribute("src", url);  // This is the URL of the movie        // Make the movie inconspicuous at the upper-right corner        movie.setAttribute("width", 1);  // If this is 0, it doesn't work.        movie.setAttribute("height", 1);        movie.setAttribute("style", "position:absolute; left:0px; top:0px;");        document.body.appendChild(movie);  // Add the movie to the document        this.$flash = movie;               // And remember it for later    };    PObject.prototype.$save = function(s, lifetimeInDays) {        // To make the data persistent, we simply set it as a variable on        // the Flash movie.  The ActionScript code in the movie persists it.        // Note that Flash persistence does not support lifetimes.        this.$flash.SetVariable("data", s); // Ask Flash to save the text    };}else { /* If we're not IE and don't have Flash 7, fall back on cookies */    PObject.prototype.$init = function(name, defaults, onload) {        var allcookies = document.cookie;             // Get all cookies        var data = null;                              // Assume no cookie data        var start = allcookies.indexOf(name + '=');   // Look for cookie start        if (start != -1) {                            // Found it            start += name.length + 1;                 // Skip cookie name            var end = allcookies.indexOf(';', start); // Find end of cookie            if (end == -1) end = allcookies.length;            data = allcookies.substring(start, end);  // Extract cookie data        }        this.$parse(data, defaults);  // Parse the cookie value to properties        if (onload) {                 // Invoke onload handler asynchronously            var pobj = this;            setTimeout(function() { onload(pobj, name); }, 0);        }    };    PObject.prototype.$save = function(s, lifetimeInDays) {        var cookie = this.$name + '=' + s;          // Cookie name and value        if (lifetimeInDays != null)                 // Add expiration            cookie += "; max-age=" + (lifetimeInDays*24*60*60);        document.cookie = cookie;                   // Save the cookie    };}</pre><div class="lefttitle" style="padding:0px; text-align: right"><a href="index.html" tppabs="http://www.davidflanagan.com/javascript5/index.html">Table of Examples</a></div><p><script type="text/javascript">amazon_ad_tag="davidflanagancom"; amazon_ad_width="300"; amazon_ad_height="250"; amazon_ad_logo="hide";</script><script type="text/javascript" src="../../www.assoc-amazon.com/s/ads.js" tppabs="http://www.assoc-amazon.com/s/ads.js"></script></div></body></html>

⌨️ 快捷键说明

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