2014-02-14 30 views
0

我有一个基于websockets的SPA应用程序。每次按F5键时,该应用程序都会注销用户。我想要阻止这种行为,正如其中一个人所说:“每次按F5时都不要放弃我的用户”。JQuery:没有方法'cookie'

我尝试:我想保存的WebSocket到饼干上join方法(请参见下面的代码)

问题: JQuery的说,我没有cookie时join方法调用:

TypeError: Object function (selector, context) { 
    // The jQuery object is actually just the init constructor 'enhanced' 
    return new jQuery.fn.init(selector, context, rootjQuery); 
} has no method 'cookie' 

问题:我该怎么办

代码:

var le = { 
    ws: null, 

    join: function() { 
     if (!$.cookie('ws')) { //HERE PROBLEM COMES!!! 
      var location = document.location.toString().replace('https://', 'wss://')/* + 'levirs'*/; 
      this.ws = new WebSocket(location); 
      this.ws.onopen = this.onOpen; 
      this.ws.onmessage = this.onMessage; 
      this.ws.onclose = this.onClose; 
      this.ws.onerror = this.onError; 
      $.cookie('ws', this.ws, {expires: 1}); 
     } else { 
      this.ws = $.cookie('ws'); 
     } 
    }, 

    //another methods 
} 
+0

你需要包含cookie插件https://github.com/carhartl/jquery-cookie –

+0

删除{expires:1},我有同样的问题 – ThermalCube

回答