tags:

views:

244

answers:

2

Does anyone know how can I assign window.open(url) into cookies array in javascript?

Below is the code that I used at the moment, but seem not really working well for me....

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

document.cookie === String

window.open === Object

Object !== String

therefore

document.cookie !== window.open

epascarello
A: 

It would be better to assign the uri string into the cookie array of the window you want to open then pull it out of the cookie when you want to call window.open. Inserting code or sensitive data into a cookie isn't good practice or secure.

Functions taken from: 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);
}

Then you could go:

createCookie('openUri', uriToOpen);

var openUri = readCookie('openUri');

if (openUri) {
    window.open(openUri, 'myWindow');
}

Or something like that.

Hope this helps.

Tres