Detect if cookies are enabledTag(s): Javascript interaction


The detection is made by trying to write a cookie and reading it back. The value is passed to a Java Applet by create dynamically the APPLET tag.

[testcookie.html]

<HTML><HEAD></HEAD><BODY>
<SCRIPT>
  document.write("<APPLET CODE='MyApplet.class' HEIGHT=100 WIDTH=400>");
  
  // check if cookie are enabled
  cookieBackup = document.cookie
  document.cookie = "cookie=yep"
  cookieOk = document.cookie.indexOf("cookie=yep") > -1
  document.cookie = cookieBackup
  
  document.write(" <PARAM NAME='okToCookie'  VALUE=" + cookieOk + ">");
  document.write("</APPLET>");
</SCRIPT>
</BODY></HTML>

[MyApplet.java]

import java.applet.*;    
import java.awt.*;    

public class MyApplet extends Applet  {

 public void init() {
  add(new Label("Cookie enabled :"));
  add(new Label(getParameter("okToCookie")));
  }
}

blog comments powered by Disqus