【问题标题】:Any way that I can assign window.open(url) into cookies array in javascript?我可以通过什么方式将 window.open(url) 分配到 javascript 中的 cookies 数组中?
【发布时间】:2009-08-14 13:21:01
【问题描述】:

有谁知道如何将 window.open(url) 分配到 javascript 中的 cookies 数组中?

下面是我目前使用的代码,但对我来说似乎不是很好......

var expiredays = 30
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie="childWindowHandles["+num+"] =" +window.open(url)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

【问题讨论】:

  • 你想达到什么效果?
  • 您可以将其作为字符串放入并评估返回给您的代码,但您到底为什么要这样做!
  • 在下面添加了另一种方法作为答案。

标签: javascript


【解决方案1】:

document.cookie === 字符串

window.open === 对象

对象 !== 字符串

因此

document.cookie !== window.open

【讨论】:

    【解决方案2】:

    最好将 uri 字符串分配到要打开的窗口的 cookie 数组中,然后在要调用 window.open 时将其从 cookie 中拉出。将代码或敏感数据插入 cookie 既不安全也不安全。

    函数取自:http://www.quirksmode.org/js/cookies.html

    function createCookie(name,value,days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    
    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    

    那么你可以去:

    createCookie('openUri', uriToOpen);
    
    var openUri = readCookie('openUri');
    
    if (openUri) {
        window.open(openUri, 'myWindow');
    }
    

    或者类似的东西。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-01-16
      • 2011-04-27
      • 2022-01-24
      • 1970-01-01
      • 2018-07-30
      • 2010-11-30
      • 2018-04-25
      • 2016-01-25
      • 2013-03-17
      相关资源
      最近更新 更多