Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/05/2009, 13:29
Avatar de lobo_php
lobo_php
 
Fecha de Ingreso: noviembre-2007
Ubicación: Cali-Colombia
Mensajes: 1.869
Antigüedad: 16 años, 6 meses
Puntos: 75
Respuesta: Como crear variables de sesion en JavaScript

este es el js:

Código javascript:
Ver original
  1. sessvars=function(){
  2.  
  3.     var x={};
  4.    
  5.     x.$={
  6.         prefs:{
  7.             memLimit:2000,
  8.             autoFlush:true,
  9.             crossDomain:false,
  10.             includeProtos:false,
  11.             includeFunctions:false
  12.         },
  13.         parent:x,
  14.         clearMem:function(){
  15.             for(var i in this.parent){if(i!="$"){this.parent[i]=undefined}};
  16.             this.flush();
  17.         },
  18.         usedMem:function(){
  19.             x={};
  20.             return Math.round(this.flush(x)/1024);
  21.         },
  22.         usedMemPercent:function(){
  23.             return Math.round(this.usedMem()/this.prefs.memLimit);
  24.         },
  25.         flush:function(x){
  26.             var y,o={},j=this.$$;
  27.             x=x||top;
  28.             for(var i in this.parent){o[i]=this.parent[i]};
  29.             o.$=this.prefs;
  30.             j.includeProtos=this.prefs.includeProtos;
  31.             j.includeFunctions=this.prefs.includeFunctions;
  32.             y=this.$$.make(o);
  33.             if(x!=top){return y.length};
  34.             if(y.length/1024>this.prefs.memLimit){return false}
  35.             x.name=y;
  36.             return true;
  37.         },
  38.         getDomain:function(){
  39.                 var l=location.href
  40.                 l=l.split("///").join("//");
  41.                 l=l.substring(l.indexOf("://")+3).split("/")[0];
  42.                 while(l.split(".").length>2){l=l.substring(l.indexOf(".")+1)};
  43.                 return l
  44.         },
  45.         finalizar2:function(t){
  46.             var t=t||this,a=arguments.callee;
  47.             if(!document.body){setTimeout(function(){a(t)},200);return};
  48.             t.flush();
  49.             var d=document.getElementById("cabecera");
  50.             if(!d){d=document.createElement("div");document.body.insertBefore(d,document.body.firstChild)};
  51.             d.id="cabecera";
  52.             if(sessvars.username){
  53.             d.innerHTML='<div align="right" style="font-size:11px;font-family:Verdana,Arial,Helvetica;>'+
  54.                         '<b style="font-family:Trebuchet MS;font-size:20px"></b>Bienvenido, '+sessvars.username+
  55.                         '<span style="cursor:pointer"><b>[Cerrar sesion]</b></span><br/>'+
  56.                         '</div>';
  57.             d.getElementsByTagName('span')[0].onclick=function(){t.clearMem();location.reload()}
  58.             }
  59.             else{
  60.                 d.innerHTML="<div align='right'><form <form id='iniciar' style='display:none;margin:0px;margin-bottom:12px' action='sessvarsTestPage2.html' method='get' onsubmit='setName(this);return false;'>"+
  61.                         "Nombre: <input style='height:22px;' widht='20' type='text' id='user'> Contrase&ntilde;a: <input style='height:22px' type='password' id='password'>"+
  62.                         "<input type='button' value='Iniciar' onClick='setName(\"user\");return false'>"+
  63.                         "</form>"+
  64.                         "</div>";
  65.             }
  66.         },finalizar:function(t){
  67.             var t=t||this,a=arguments.callee;
  68.             if(!document.body){setTimeout(function(){a(t)},200);return};
  69.             t.flush();
  70.             var d=document.getElementById("cabecera");
  71.             if(!d){d=document.createElement("div");document.body.insertBefore(d,document.body.firstChild)};
  72.             d.id="cabecera";
  73.             if(sessvars.username){
  74.             d.innerHTML='<div align="right" style="font-size:11px;font-family:Verdana,Arial,Helvetica;>'+
  75.                         '<b style="font-family:Trebuchet MS;font-size:20px"></b>Bienvenido, '+sessvars.username+
  76.                         '<span style="cursor:pointer"><b>[Cerrar sesion]</b></span><br/>'+
  77.                         '</div>';
  78.             d.getElementsByTagName('span')[0].onclick=function(){t.clearMem();location.reload()}
  79.             }
  80.             else{
  81.                 d.innerHTML="<div align='right'><form <form id='iniciar' style='display:none;margin:0px;margin-bottom:12px' action='sessvarsTestPage2.html' method='get' onsubmit='setName(this);return false;'>"+
  82.                         "Nombre: <input style='height:22px;' widht='20' type='text' id='user'> Contrase&ntilde;a: <input style='height:22px' type='password' id='password'>"+
  83.                         "<input type='button' value='Iniciar' onClick='setName(\"user\");return false'>"+
  84.                         "</form>"+
  85.                         "</div>";
  86.             }
  87.         },
  88.         debug:function(t){
  89.             var t=t||this,a=arguments.callee;
  90.             if(!document.body){setTimeout(function(){a(t)},200);return};
  91.             t.flush();
  92.             var d=document.getElementById("sessvarsDebugDiv");
  93.             if(!d){d=document.createElement("div");document.body.insertBefore(d,document.body.firstChild)};
  94.             d.id="sessvarsDebugDiv";
  95.             d.innerHTML='<div style="line-height:20px;padding:5px;font-size:11px;font-family:Verdana,Arial,Helvetica;'+
  96.                         'z-index:10000;background:#FFFFCC;border: 1px solid #333;margin-bottom:12px">'+
  97.                         '<b style="font-family:Trebuchet MS;font-size:20px">sessvars.js - debug info:</b><br/><br/>'+
  98.                         'Memory usage: '+t.usedMem()+' Kb ('+t.usedMemPercent()+'%)&nbsp;&nbsp;&nbsp;'+
  99.                         '<span style="cursor:pointer"><b>[Clear memory]</b></span><br/>'+
  100.                         top.name.split('\n').join('<br/>')+'</div>';
  101.             d.getElementsByTagName('span')[0].onclick=function(){t.clearMem();location.reload()}
  102.         },
  103.         init:function(){
  104.             var o={}, t=this;
  105.             try {o=this.$$.toObject(top.name)} catch(e){o={}};
  106.             this.prefs=o.$||t.prefs;
  107.             if(this.prefs.crossDomain || this.prefs.currentDomain==this.getDomain()){
  108.                 for(var i in o){this.parent[i]=o[i]};
  109.             }
  110.             else {
  111.                 this.prefs.currentDomain=this.getDomain();
  112.             };
  113.             this.parent.$=t;
  114.             t.flush();
  115.             var f=function(){if(t.prefs.autoFlush){t.flush()}};
  116.             if(window["addEventListener"]){addEventListener("unload",f,false)}
  117.             else if(window["attachEvent"]){window.attachEvent("onunload",f)}
  118.             else {this.prefs.autoFlush=false};
  119.         }
  120.     };
  121.    
  122.     x.$.$$={
  123.         compactOutput:false,       
  124.         includeProtos:false,    
  125.         includeFunctions: false,
  126.         detectCirculars:true,
  127.         restoreCirculars:true,
  128.         make:function(arg,restore) {
  129.             this.restore=restore;
  130.             this.mem=[];this.pathMem=[];
  131.             return this.toJsonStringArray(arg).join('');
  132.         },
  133.         toObject:function(x){
  134.             if(!this.cleaner){
  135.                 try{this.cleaner=new RegExp('^("(\\\\.|[^"\\\\\\n\\r])*?"|[,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t])+?$')}
  136.                 catch(a){this.cleaner=/^(true|false|null|\[.*\]|\{.*\}|".*"|\d+|\d+\.\d+)$/}
  137.             };
  138.             if(!this.cleaner.test(x)){return {}};
  139.             eval("this.myObj="+x);
  140.             if(!this.restoreCirculars || !alert){return this.myObj};
  141.             if(this.includeFunctions){
  142.                 var x=this.myObj;
  143.                 for(var i in x){if(typeof x[i]=="string" && !x[i].indexOf("JSONincludedFunc:")){
  144.                     x[i]=x[i].substring(17);
  145.                     eval("x[i]="+x[i])
  146.                 }}
  147.             };
  148.             this.restoreCode=[];
  149.             this.make(this.myObj,true);
  150.             var r=this.restoreCode.join(";")+";";
  151.             eval('r=r.replace(/\\W([0-9]{1,})(\\W)/g,"[$1]$2").replace(/\\.\\;/g,";")');
  152.             eval(r);
  153.             return this.myObj
  154.         },
  155.         toJsonStringArray:function(arg, out) {
  156.             if(!out){this.path=[]};
  157.             out = out || [];
  158.             var u; // undefined
  159.             switch (typeof arg) {
  160.             case 'object':
  161.                 this.lastObj=arg;
  162.                 if(this.detectCirculars){
  163.                     var m=this.mem; var n=this.pathMem;
  164.                     for(var i=0;i<m.length;i++){
  165.                         if(arg===m[i]){
  166.                             out.push('"JSONcircRef:'+n[i]+'"');return out
  167.                         }
  168.                     };
  169.                     m.push(arg); n.push(this.path.join("."));
  170.                 };
  171.                 if (arg) {
  172.                     if (arg.constructor == Array) {
  173.                         out.push('[');
  174.                         for (var i = 0; i < arg.length; ++i) {
  175.                             this.path.push(i);
  176.                             if (i > 0)
  177.                                 out.push(',\n');
  178.                             this.toJsonStringArray(arg[i], out);
  179.                             this.path.pop();
  180.                         }
  181.                         out.push(']');
  182.                         return out;
  183.                     } else if (typeof arg.toString != 'undefined') {
  184.                         out.push('{');
  185.                         var first = true;
  186.                         for (var i in arg) {
  187.                             if(!this.includeProtos && arg[i]===arg.constructor.prototype[i]){continue};
  188.                             this.path.push(i);
  189.                             var curr = out.length;
  190.                             if (!first)
  191.                                 out.push(this.compactOutput?',':',\n');
  192.                             this.toJsonStringArray(i, out);
  193.                             out.push(':');                    
  194.                             this.toJsonStringArray(arg[i], out);
  195.                             if (out[out.length - 1] == u)
  196.                                 out.splice(curr, out.length - curr);
  197.                             else
  198.                                 first = false;
  199.                             this.path.pop();
  200.                         }
  201.                         out.push('}');
  202.                         return out;
  203.                     }
  204.                     return out;
  205.                 }
  206.                 out.push('null');
  207.                 return out;
  208.             case 'unknown':
  209.             case 'undefined':
  210.             case 'function':
  211.                 if(!this.includeFunctions){out.push(u);return out};
  212.                 arg="JSONincludedFunc:"+arg;
  213.                 out.push('"');
  214.                 var a=['\n','\\n','\r','\\r','"','\\"'];
  215.                 arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])};
  216.                 out.push(arg);
  217.                 out.push('"');
  218.                 return out;
  219.             case 'string':
  220.                 if(this.restore && arg.indexOf("JSONcircRef:")==0){
  221.                     this.restoreCode.push('this.myObj.'+this.path.join(".")+"="+arg.split("JSONcircRef:").join("this.myObj."));
  222.                 };
  223.                 out.push('"');
  224.                 var a=['\n','\\n','\r','\\r','"','\\"'];
  225.                 arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])};
  226.                 out.push(arg);
  227.                 out.push('"');
  228.                 return out;
  229.             default:
  230.                 out.push(String(arg));
  231.                 return out;
  232.             }
  233.         }
  234.     };
  235.    
  236.     x.$.init();
  237.     return x;
  238. }()
__________________
Cabuntu.org - Difundiendo Software Libre en Cali, Colombia
Usuario Linux # 483175