简体   繁体   中英

Use cookie to set link?

If cookies are used to stor simple strings can I also use the cookie to set the link of a button.

the idea: I have a simple HTML which i use to create a help file (this is very simple and I already have it), but we also make use of heaps of relating MSDS documents, so instead of me finding the thousands of documents online, i was thinking that Id get my users to set this by setting a cookie and ussing the cookies string to set the link ready for use the next time?

I can set read delete and modyfy cookies using Javascript...

Granted this function ( http://www.w3schools.com/js/js_cookies.asp ) to get the cookie item works, this should do the job:

var getCookie = function(cname)
{
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) 
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}

var click = function () {
    window.location.href = getCookie('buttonUrl');
}

<button onclick="click()">See documentation</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM