Shiny: use shinyjs to fetch cookie data
<p>I want to fetch cookie data from my Shiny app using shinyjs. I have created a cookie, "samplecookie=testval"; and I want to be able to retrieve the value of samplecookie. I use the below javascript function (where I pass the cookieName and it returns the corresponding value).</p>

<pre><code>function fetchCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(";");
for(var i=0;i &lt; 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 "No such cookie";  
</code></pre>

<p>Below is the javascript code in the shiny app</p>

<pre><code>jsCode&lt;-'shinyjs.tstfunc=
    function (name) {
var nameEQ = name + "=";
var ca = document.cookie.split(";");
for(var i=0;i &lt; 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 "No such cookie";
}   
}' 

ui &lt;- shinyUI(fluidPage(mainPanel(
  useShinyjs(),
  extendShinyjs(text = jsCode)
)))

server &lt;- function(input, output)
  {
    observe({
    x=js$tstfunc("samplecookie")
    print(x)
    })
  }

shinyApp(ui=ui, server=server)
</code></pre>

<p>I am expecting that when I pass "samplecookie" as a parameter to the tstfunc() function, it should print "testval" on the console. But every time I keep getting a NULL value returned. Can someone help me understand what I am doing wrong? Appreciate any help. Thanks.</p>