-1

I need to save a string inside the textbox, and to make it happen so, when the "Set as default" -box is checked and when the button is pressed, the input of the ID will be saved as a cookie to the user. IE. I'll type "TEST" and the next time I open the app, it will remember the input that was typed in & searched.

I will provide an image from the UI, so you will get basic understanding how it looks like;

enter image description here .

I have two sides on the project; (exclusing CSS) JS and HTML. Currently my code in cookie.js looks following;

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
}

function checkCookie() {
}

I havent really tried anything specific, I've tried to work with checkCookie and getCookie functions to get this work but so far without result.

In HTML, I've only coded following inside the body;

<body id="body" style="max-height: 100%;" onload="checkCookie()">

<form onsubmit="return false" id="searchform">
    <input type="text" id="field" placeholder="Search timetable" 
    maxlength="100" autocomplete="off">

    <label for="setDefault" id="mlabel"><input type="checkbox" 
    id="setDefault" data-role="none" />Set as default</label>
    <input type="button" value="Search" id="search" 
    class="ui-btn ui-input-btn 
    ui-corner-all ui-shadow" onclick="executeSearch()" />
</form>

</body>

So my question is; How can i store the string that I've typed inside textbox and how can I get the value when I open the page again?

Cod3r-b0ss
  • 205
  • 1
  • 10

2 Answers2

2

A Function to Set a Cookie

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

A Function to Get a Cookie

function getCookie(cname) {
    var name = cname + "=";
    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);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

When you do the search, set your search keyword to cookie

function executeSearch(){
    var setDefault = document.getElementById('setDefault').checked;
    if(setDefault){
        var keyword = document.getElementById('field').value;
        setCookie('search-keyword', keyword, 360);
    }

    //Do the search
}

Now when you load the page check cookie exists, and load previously search keyword

function checkCookie(){
    var previousKeyword = getCookie('search-keyword');
    document.getElementById('field').value = previousKeyword;
}

You can read more about cookies here

Nuwan.Niroshana
  • 407
  • 4
  • 15
1

So if your Question is how can you store a cookie in a browser with js then this is the anwser:

With JavaScript, a cookie can be created like this:

document.cookie = "username=John Doe";

You can also add an expiry date (in UTC time). By default, the cookie is deleted when the browser is closed:

document.cookie = "username=John Doe; expires=Thu, 18 Dec 2017 12:00:00 UTC";

With a path parameter, you can tell the browser what path the cookie belongs to. By default, the cookie belongs to the current page.

document.cookie = "username=John Doe; expires=Thu, 18 Dec 2017 12:00:00 UTC; path=/";

With JavaScript, cookies can be read like this:

var x = document.cookie;

And to get access to your input in the textfield you have to give the function a parameter when you call it like: myfunction(myinput);

Hope this helps you