č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:

pondělí 30. listopadu 2015

Browser language - navigator.language versus navigator.languages

I tried a simple test using the following html to explore the language information provided by Chrome, Firefox and IE.

<html>
<head>
</head>
<body>
<script>
var browserLanguage = navigator.language;
var preferredLanguageList = navigator.languages;
window.alert("Browser Language: "+browserLanguage+"\n"+"Preferred Languages: "+preferredLanguageList);
</script>
</body>
<html>

The results follow:

Chrome Version 46.0

navigator.language - returns the browser language (set by Display Google Chrome in this language button in Settings / Language and input settings)
navigator.languages - returns the list of preferred languages in order set in the Languages box (Settings / Language and input settings)

Firefox Version 38.4

navigator.language - returns the first item of list of Languages in the Options / Content / Languages
navigator.languages - returns the list of all items of Languages in the Options / Content / Languages (in order set by the Languages box)

Internet Explorer 11.0

navigator.language - returns the current system locales from Window Regional Settings
navigator.languages - is undefined in IE, I have not find how to get the list of preferred languages from Javascript. The only way is to parse the Accept-Language header field of http request on the server side