0

My functions, it work:

<script>
function createCookie(name,value,sec) {
if (sec) {
    console.log('createCookie: '+name+', '+value+', '+sec);
    var date = new Date();
    date.setTime(date.getTime()+(sec*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) 
       {    console.log('readCookie: '+name+',value: '+c.substring(nameEQ.length,c.length));
           return c.substring(nameEQ.length,c.length);
       }
    }
    console.log('readCookie: '+name+', NULL');
    return null;
}
</script>

call to the bouth work (as I see in console)

<?
echo '<script type="text/javascript">createCookie("'.$cookie_random_photo.'","'.$randf.'",60);</script>';

echo '<script type="text/javascript">readCookie("'.$cookie_random_photo.'");</script>';
?>

but when I probe get output, this make Uncaught SyntaxError: Unexpected token ILLEGAL

<?
ob_start();
echo '<script type="text/javascript">readCookie("'.$cookie_random_photo.'");</script>';
$returned_value = ob_get_contents();
ob_end_clean();
?>

or

<?
ob_start();
echo '<script type="text/javascript">readCookie("'.$cookie_random_photo.'");</script>';
$returned_value = ob_get_clean();
?>

This generate error and screen ouput "' );", readCookie() is not called.

How to properly get $returned_value?

EDIT full js error

VM119792:1 Uncaught SyntaxError: 
Unexpected token ILLEGAL
c.extend.globalEval @  jquery.js:29
Qa @ jquery.js:16
c.extend.each @ jquery.js:30
c.fn.extend.domManip @ jquery.js:110
c.fn.extend.append @ jquery.js:103
c.fn.extend.html @ jquery.js:107c.ajax.complete @ jquery.js:120
d @ jquery.js:124
x.onreadystatechange @ jquery.js:129
theWalker
  • 2,022
  • 2
  • 18
  • 27

1 Answers1

0

Most likely this is something to do with the charset of the server and client. Can you add charset="utf-8" to the script tag like below and see if that solves the problem?

 echo '<script type="text/javascript" charset="utf-8">readCookie("'.$cookie_random_photo.'");</script>';

Also remember to actually print the output

<?
  ob_start();
  echo '<script type="text/javascript">readCookie("'.$cookie_random_photo.'");</script>';
  $returned_value = ob_get_contents();
  ob_end_clean();
  echo $returned_value; // <-- Only this will echo the contents back to the client. 
?>
dors
  • 1,152
  • 11
  • 19