JavaScript - How do I enable and disable browser cookies using javascript?

Asked By saqib jahangir on 19-Dec-06 11:50 PM

Hi,

 I want to check the client browser whether he set the cookies enable or disable if he disable the cookies then my javascript should enable it.

I want to do this because i am using cookies on my web pages.

please guide me.

Thanks,

Bye.

cookies

sundar k replied to saqib jahangir on 20-Dec-06 12:11 AM

I think you can only enable/disable cookies manually(not really sure), the below script checks to see if the user has cookies enabled in his browser. If not, it displays an alert window letting them know your site is using cookies.

if (navigator.cookieEnabled == 0) {
  alert("You need to enable cookies for this site to load properly!");
}

i hope you know how to do it manually,check this link in case if you want manually http://www.musecurity.com/company/enable.html

Directions on how to check if Cookies are Enabled with Javascript.

K Pravin Kumar Reddy replied to saqib jahangir on 20-Dec-06 12:15 AM
Directions on how to check if Cookies are Enabled with Javascript.

You will be placing the following code into your webpage, it can run anywhere. For our purposes we are setting the cookie with name 'test', value 'none', set to expire in no days, which will set the expires feature to ''. '' means that the cookie is what is known as a session cookie, it will expire as soon as the browser session ends. If you have any questions on this you can post them on our http://techpatterns.com/forums/forum-11.html.

First you would have placed your cookie script into a library file which is called in the head of your html document like this (please note that this is just an example, if you don't have a devepment server setup, you'll want to replace the '/' absolute to root path with a relative path, like this '../js/javascript_cookies.js'.

<script type="text/javascript" src="/js/javascript_cookies.js"></script>

Then you place the following code anywhere on the page AFTER the library file has been loaded. If you are outputting something, you'll want it after the <body> tag of course. If you are just loading a variable, you can put this code anywhere, including in the library file, then just check the value of the cookie set variable.

<script type="text/javascript">
// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'none', '', '/', '', '' );
// if Get_Cookie succeeds, cookies are enabled, since 
//the cookie was successfully created.
if ( Get_Cookie( 'test' ) )
{
	document.write( 'cookies are currently enabled.' );
	/* 
	this is an example of a set cookie variable, if 
	you want to use this on the page or on another script 
	instead of writing to the page you would just check that value
	for true or false and then do what you need to do.
	/*
	cookie_set = true;
	// and these are the parameters for Delete_Cookie:
	// name, path, domain
	// make sure you use the same parameters in Set and Delete Cookie.
	Delete_Cookie('test', '/', '');
}
// if the Get_Cookie test fails, cookies 
//are not enabled for this session.
else
{
	document.write( 'cookies are not currently enabled.' );
	cookie_set = false;
}
</script>
			

Obviously Javascript must be enabled for this test, and the Cookies, to work. There is as far as we know no way to check this server side without using a page reload, which is not a very good idea in general.

reference

http://techpatterns.com/downloads/javascript_check_cookies.php

You can check only

Mohammad Ajmal Yazdani replied to saqib jahangir on 20-Dec-06 12:21 AM

hello,

Determining JavaScript cookie support in client's browser

If your script relies on JavaScript cookies to persist and store information for retrieval later, it's good practice to always make sure that the user's browser supports cookies first. This includes whether the browser has cookies enabled (an option in most browsers). Depending on your situation you can then either remind the user to enable cookies, or create code to handle these cases silently.

So, to answer this common question, use the following technique to detect whether the client's browser has cookies enabled: 

<script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false
//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}
//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever
</script>

The above script is actually two techniques rolled into one:

  • Firstly, we use the official property for the task, navigator.cookieEnabled, to help determine whether the browser has cookies enabled. However, we immediately run into a limitation- this property is supported only in IE4+ and NS6+. So, in both cases where the property returns false or does not exist (in other browsers), we set the variable cookieEnabled to false instead for continued investigation.
  • Moving on, it's now time to probe all other browsers for cookie availability. The age-old "give it a try" technique works nicely. By setting a dummy value to document.cookie and immediately probing it to see if the value is retained, we can tell if cookies is enabled on the client's end.

The variable cookieEnabled now contains a Boolean value indicating the client's cookie support. Nice and crunchy!

we can enable or disable cookie using javascript
K Pravin Kumar Reddy replied to saqib jahangir on 20-Dec-06 12:34 AM

Reading and Writing Browser Preferences

http://www.java2s.com/Code/JavaScriptDemo/ReadingandWritingBrowserPreferences.htm

code

/*
JavaScript Bible, Fourth Edition

by Danny Goodman 

Publisher: John Wiley & Sons CopyRight 2001
ISBN: 0764533428
*/

<HTML>
<HEAD>
<TITLE>Reading/Writing Browser Preferences</TITLE>
<SCRIPT LANGUAGE="JavaScript1.2">
function setPreference(pref, value) {
    netscape.security.PrivilegeManager.enablePrivilege(
       "UniversalPreferencesWrite")
    navigator.preference(pref, value)
    netscape.security.PrivilegeManager.revertPrivilege(
       "UniversalPreferencesWrite")
    showPreferences()
}
function showPreferences() {
    var form = document.forms[0]
    netscape.security.PrivilegeManager.enablePrivilege(
        "UniversalPreferencesRead")
    form.imgLoad.checked = navigator.preference("general.always_load_images")
    form.cacheSize.value = navigator.preference("browser.cache.disk_cache_size")
    form.ssEnable.checked = navigator.preference("browser.enable_style_sheets")
    form.autoIEnable.checked = navigator.preference("autoupdate.enabled")
    var cookieSetting = navigator.preference("network.cookie.cookieBehavior")
    for (var i = 0; i < 3; i++) {
        form.elements["cookie" + i].checked = (i == cookieSetting) ? true : false
    }

form.cookieWarn.checked = navigator.preference("network.cookie.warnAboutCookies")
    netscape.security.PrivilegeManager.revertPrivilege(
        "UniversalPreferencesRead")
}
</SCRIPT>
</HEAD>
<BODY onLoad="showPreferences()">
<H1>Browser Preferences Settings Sampler</H1>
<HR>
<FORM>
<INPUT TYPE="checkbox" NAME="imgLoad" 
onClick="setPreference('general.always_load_images',this.checked)">
Automatically Load Images<BR>
<INPUT TYPE="checkbox" NAME="ssEnable" 
onClick="setPreference('browser.enable_style_sheets',this.checked)">
Style Sheets Enabled<BR>
<INPUT TYPE="checkbox" NAME="autoIEnable" 
onClick="setPreference('autoupdate.enabled',this.checked)">
AutoInstall Enabled<BR>
<INPUT TYPE="checkbox" NAME="cookie0" 
onClick="setPreference('network.cookie.cookieBehavior',0)">
Accept All Cookies<BR>
<INPUT TYPE="checkbox" NAME="cookie1" 
onClick="setPreference('network.cookie.cookieBehavior',1)">
Accept Only Cookies Sent Back to Server<BR>
<INPUT TYPE="checkbox" NAME="cookie2" 
onClick="setPreference('network.cookie.cookieBehavior',2)">
Disable Cookies<BR>
<INPUT TYPE="checkbox" NAME="cookieWarn" 
onClick="setPreference('network.cookie.warnAboutCookies',this.checked)">
Warn Before Accepting Cookies<BR>
Disk cache is <INPUT TYPE="text" NAME="cacheSize" SIZE=10> KB <BR>
</FORM>
</BODY>
</HTML>

reference

http://www.java2s.com/Code/JavaScript/Window-Browser/ReadingandWritingBrowserPreferences.htm

other reference

http://research.nihonsoft.org/javascript/jsref/brow1.htm

http://docs.sun.com/source/816-6408-10/nav.htm

http://www.devguru.com/Technologies/ecmaScript/quickref/preference.html

http://www.northernsecurity.net/articles/peas.html

how to set the scripting enable of browser in javascript through code
vineet kharbanda replied to saqib jahangir on 03-Aug-09 05:59 AM
how to set the scripting enable of browser in javascript through code