Search code examples
javascripthtmlnavigator

Javascript Naviagtor Cookie Testing Not working


Im trying to use Javascript Navigator object to test if cookies are enabled in the browser.Im using the following code,the button shows up,but the message is not displayed after clicking.

  <!DOCTYPE html>
    <html>
    <body>
    <p> Are cookies enabled ? </p>
    <button onclick="myFunction()"> Button1 </button>
    <p id="para1"></p>
    <script>
    function myFunction()
    {
    document.getElementbyId("para1").innerHTML="Cookies enabled is "+ navigator.cookieEnabled;
    }
    </script>
    </body>
    </html>

Solution

  • The method is .getElementById(), not .getElementbyId(). If you open your browser's console you would see something like:

    TypeError: document.getElementbyId is not a function

    http://jsbin.com/kixatozafu/1/edit

      <!DOCTYPE html>
        <html>
        <body>
        <p> Are cookies enabled ? </p>
        <button onclick="myFunction()"> Button1 </button>
        <p id="para1"></p>
        <script>
        function myFunction()
        {
        document.getElementById("para1").innerHTML="Cookies enabled is "+ navigator.cookieEnabled;
        }
        </script>
        </body>
        </html>