0

I want to save some info in session/cookie through Javascript.

I am trying to pull some content from mainpage and assign it to session/cookie variables and then use it inside my iframe

I think this is the best way to get values from mainpage to an iframe

pleas let me know if this idea is a better one and guide me how to assign values to the session/cookie variables through javascript

eg:

URL ="sitename.com/mypage/1.php?test=1&mode=0"  //just an example url

to session variable 'ifra_url' = "sitename.com/mypage/1.php?test=1&mode=0"

I go thorught some websites which says its not possible to create session with javascript as session is server side scripted and I update my question how to create COOKIE with javascript as it saves data in browser not anywhere else

3
  • u can use sime div rather iframe for this...after building the URL, get the content using Ajax and set div.innerHTML to this..you may need to remove meta,html tags which is not that difficult.. Commented Aug 6, 2012 at 6:38
  • I am using iframe to show a popup form,... its a different context I am dealing with I think, I want to pull some content into this iframe from the main page(parent page which generates iframe) Commented Aug 6, 2012 at 6:42
  • that's correct, you can't create a session but you can create a cookie Commented Aug 6, 2012 at 6:43

1 Answer 1

1

I think I got my answer

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);
}

more reference http://www.quirksmode.org/js/cookies.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.