Pages

Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, February 18, 2013

IE Array indexOf() - resolved

    /* this will work in all browsers(no jQuery needed) */
    if (!Array.indexOf) {
    Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
    if (this[i] == obj) {
    return i;
    }
    }
    return -1;
    }
    }
   
    //call
    var i = haystack_array.indexOf("needle")
   
   
    /* or you can use jquery instead */
   
    //call
    var i = jQuery.inArray("needle", haystack_array)

Monday, January 7, 2013

Javascript Detect iPhone device

<html>
    <head>
        <title>Mobile Detection</title>
        <script>
            function DetectIphone()
            {
               var uagent = navigator.userAgent.toLowerCase();
               if (uagent.search("iphone") > -1 || uagent.search("ipod") > -1 || uagent.search("android") > -1)
                  alert('true');
               else
                   alert('false');
            }
        </script>
    </head>
    <body>
        <input type="button" OnClick="DetectIphone()" value="Am I an Iphone?" />
    </body>
</html>

Saturday, April 9, 2011

Create cookie



<script type="text/javascript">
<!--

/* COOKIES */

var Cookies = {
init: function () {
var allCookies = document.cookie.split('; ');
for (var i=0;i<allCookies.length;i++) {
var cookiePair = allCookies[i].split('=');
this[cookiePair[0]] = cookiePair[1];
}
},
create: function (name,value,hours) {
if (hours) {
var date = new Date();
date.setTime(date.getTime()+(hours*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
this[name] = value;
},
erase: function (name) {
this.create(name,'',-1);
this[name] = undefined;
}
};
Cookies.init();

function saveIt(name) {
var x = document.forms['cookieform'].cookievalue.value;
if (!x)
alert('Please fill in a value in the input box.');
else {
Cookies.create(name,x,1);
alert('Cookie created');
}
}

function readIt(name) {
alert('The value of the cookie is ' + Cookies[name]);
}

function eraseIt(name) {
Cookies.erase(name);
alert('Cookie erased');
}


var x = Cookies['jaycookie'];
if (x) alert('Cookie jaycookie \nthat you set on a previous visit, is still active.\nIts value is ' + x);


// -->
</script>


</head>

<body>

<form name="cookieform" action="#"><p>
The value of the cookie should be <input name="cookievalue" />
</p></form>

<p><a href="javascript:saveIt('jaycookie')" class="page">Create cookie 1</a><br />
<a href="javascript:readIt('jaycookie')" class="page">Read cookie 1</a><br />
<a href="javascript:eraseIt('jaycookie')" class="page">Erase cookie 1</a>.</p>