čtvrtek 3. prosince 2015

How to check if cookies are enabled

The navigator object has the attribute cookieEnabled that returns true if the cookies are enabled in the browser. Find a simple example bellow.

<HTML>
<HEAD>
  <SCRIPT>
    function checkCookies(){
    window.alert("Are cookies enabled? "+(navigator.cookieEnabled?"Yes":"No"));
    }
  </SCRIPT>
</HEAD>
<BODY>
  <BUTTON onclick="checkCookies()">Check cookies</BUTTON>
</BODY>
</HTML>



středa 2. prosince 2015

Periodical Call of Javascript Function using SetTimeout

The following example shows how to set up a periodical function call of JavaScript function inside an html page.




<html>
<head>

<script>
var imageRefs = ["pict1.png","pict2.png","pict3.png"];
var counter = 0;

(function changeImage(){
 
 image = document.getElementById("image1");
 if(image != null) {
   image.src = imageRefs[counter];
   counter = (counter + 1) % 3;
   }

 setTimeout(changeImage,2000);

})();
</script>

</head>
<body>

<img id="image1" src="pict1.png" />

</body>

</html>
Here is the working example:

Change picture on click

A following simple example shows how to write the html page with pictures that rotate on mouse click event.

<html>
<head>

<script>
function changeImageOnClick(){
    image=document.getElementById("image1");
    
        if(image.src.indexOf("pict1.png")<0)
            image.src="pict1.jpg";
        else
            image.src="pict2.jpg";
}
</script>
</head>
<body>

<img src="pict1.png" id="image1" onclick="changeImageOnClick()"/>

</body>
Here is the working example: