2

I want to pass the search query from one html page text box to another html page text box using cookies.

I have tried the following script but it is not working as expected:

Page 1

<input type="text" value="" name="s" id="s1" />
<input id="btnSave" type="button" value="Search" onclick="Redirect();"/>

<script type="text/javascript">
    function Redirect() {
        var x = document.getElementById("s1").value;
        document.cookie = x;
        window.location.href = 'Result.html';
    }
</script>

Page 2

<script>
    function getcookie() {
        document.getElementById("#s").value = document.cookie;
    }
</script>


<body onload="getcookie();">
<input id="s" type="text" />
</body>
Hussain Rauf
  • 278
  • 2
  • 15
  • `document.cookie` doesn't work like that. and thet work in a complex way. One way to simplify it is use this code from quirksmode http://www.quirksmode.org/js/cookies.html#script – Pankaj Phartiyal Sep 19 '15 at 10:47
  • You could use local storage for this. Just suggesting an alternative method. It can be Set and get. It's very simple. – Fintan Creaven Sep 19 '15 at 12:55

1 Answers1

1

You should set cookies with along with its expiry time (not important but usefull when you want to retrive even borwser closed and you need it again on browser open). One more thing , when your getting cookie value ,it gives string containing all cookies value , so customize it to get desired.

SET COOKIE

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

Now , get the value from cookie , function may be defined as

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

All together , to work within your existing solution

PAGE 1

<script type="text/javascript">
    function Redirect() {
        var x = document.getElementById("s1").value;
        setCookie("s",x,2);
        window.location.href = 'Result.html';
    }
</script>

PAGE 2

<body onload="document.getElementById('s').value =getCookie('s')">
<input id="s" type="text" />
</body>
Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16
  • values are getting saved in cookies but search value is not appearing in textbox of page 2. :/ – Hussain Rauf Sep 19 '15 at 11:55
  • i was missing correct syntax , may be that was the issue , else anything extra then let me know – Rohit Kumar Sep 19 '15 at 11:58
  • Mate you saved my life, finally it is working. One more thing, on page load after getting the search value from cookies, if I want to run this search value to find matching results, how is it possible? Can I use following code: ` **** **** ` – Hussain Rauf Sep 19 '15 at 12:26
  • yep , you could try as triggering click event on button . If not gives the required result , better to ask one fresh question with fresh headings.. Happy coding :) – Rohit Kumar Sep 19 '15 at 12:46
  • Okay. Thumbs Up mate :) – Hussain Rauf Sep 19 '15 at 13:09
  • I have changed the formatting of my question. Vote up if you can, – Hussain Rauf Jun 22 '16 at 15:02