How to display whether Cookie is just enabled or disabled? - javascript

I am using javascript code shown below for displaying Cookies either
Enabled
or
Disabled
Code is:
<script>
txt=navigator.cookieEnabled;
document.getElementById("example").innerHTML=txt;
</script>
But this code is just displaying
true
I have no idea how to go ahead.
Please help. Thanks in advance.

navigator.cookieEnabled return true or false. So you can just display what you want on the basis of these values
<script type="text/javascript">
txt=navigator.cookieEnabled;
document.getElementById("example").innerText=(txt==true ?"Enabled":"Disabled");
</script>

please find the below link for checking the cookies enabled state:
http://sveinbjorn.org/cookiecheck
function are_cookies_enabled()
{
var cookieEnabled = (navigator.cookieEnabled) ? true : false;
if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
{
document.cookie="testcookie";
cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
}
return (cookieEnabled);
}

<script>
txt=navigator.cookieEnabled;
document.getElementById("example").innerHTML=(txt==true?"Enabled":"Disabled";
</script>

Related

Issue while evaluating the condition in javascript

I am fetching one value from controller to jsp and trying to validate the value as follows,
<c:set var="healthWorkerOptions" value='${map["healthWorkerOptions"]}' />
<script>
validateSelectedOption();
function validateSelectedOption()
{
alert("test");
if(healthWorkerOptions != null)
{
alert("not null");
}
else{
alert("null");
}
}
</script>
Value is coming from the controller and able see the fetched value with following statement,
<p> ${healthWorkerOptions} </p>
But while evaluating the condition nothing is happening. What's wrong in my code? I am able to see only test alert but not not null or null alert.
Any suggestion
healthWorkerOptions is not defined in scope of your JS. A debugger should tell you this.
function validateSelectedOption() {
var healthWorkerOptions = '<c:out value="${healthWorkerOptions}" />';
[...]
}
You can directly use el in JavaScript also like below :
healthWorkerOptions = "${healthWorkerOptions}";
if(healthWorkerOptions != null)
{
alert("not null");
} else {
alert("null");
}

checkbox being checked on false - jquery

$('#checkyes').prop('checked', row['checkb']);
var check = row['checkb'];
alert(check);
$('#checkyes').checkboxradio('refresh');
The alert correctly shows the value inside row['checkb'] to be false, yet the checkbox gets ticked anyway. Am I missing some quotations somewhere or can I not use the row value?
Try
$('#checkyes').prop('checked', (row['checkb'] == 'true') ? true : false);
Or
$('#checkyes').prop('checked', (row['checkb'] == 'true') );
Or
var chk = (row['checkb'] == 'true') ? true : false; //Or var chk = (row['checkb'] == 'true');
$('#checkyes').prop('checked', chk );
Problem
row['checkb'] has value string true or false not Boolean value .
So $('#checkyes').prop('checked', row['checkb']); will evaluate to checked.
String value here prop('checked', String) makes it true for all cases .
From Tushar Gupta's answer, I'd suggest:
$('#checkyes').prop('checked', row['checkb'] == 'true');
var isResult = (row['checkb'] == 0) ? false : true;
$('#checkyes').prop('checked', isResult);
$('#checkyes').prop('checked', JSON.parse(row['checkb']));

Why does the order of Boolean values affect this program?

I created a basic program where user input is turned into an alert on submission. I can't figure out why the program only works as intended if I use false rather than true as the first condition in my if/else statement. I'm sure this is very basic but I've failed to find anything of relevance. After a long search I decided to post the question. Any answers will be greatly appreciated.
The HTML:
<form id="greetingForm">
<input type="text" name="userInput" id="userInput"/>
<input type="submit" value="click" id="submit"/>
</form>
The broken script:
function output(){
var input = document.getElementById('userInput').value;
if(input == true){
alert(input);
}else{
alert('Say something!');
}
}
function init(){
var greetingForm = document.getElementById('greetingForm');
greetingForm.onsubmit = output;
}
window.onload = init;
The working script:
function output(){
var input = document.getElementById('userInput').value;
if(input == false){
alert('Say something!');
}else{
alert(input);
}
}
function init(){
var greetingForm = document.getElementById('greetingForm');
greetingForm.onsubmit = output;
}
window.onload = init;
The variable input will never be equal to the boolean true because it is a string. Try changing it to:
function output(){
var input = document.getElementById('userInput').value;
if(input != ""){
alert(input);
}else{
alert('Say something!');
}
}
To clarify ferd tomale's answer, it's one of the "weird" type conversion cases where a check on equality to true does not behave in the same way as check on equality to false.
"" == false -> true
"a" == false -> false, but
"" == true -> false
"a" == true -> false
You can switch to using typesafe comparison operators (===, !==), which behave much more predictable, but then you'll have to convert values to the correct type yourself. Or you can learn the quirks of JS's automatic type conversion when you use == or !=.
Because your input is a string. And string == true will be false.
You can set breakpoints to check them.

javascript if fails

I do not see where my error is, If there is any
<!DOCTYPE html> <html> <head>
<script type="text/javascript">
//http://stackoverflow.com/questions/10251149/using-javascript-to-detect-google-chrome-to-switch-css
//provera brosera
function check_chrome_ua() {
var ua = navigator.userAgent.toLowerCase();
var is_chrome = /chrome/.test(ua);
alert("func check_chrome_ua() " + is_chrome);
return is_chrome;
}
//promena nadpisa
function check() {
//alert("check1");
var check = check_chrome_ua();
alert("var check " + check + "; func check_chrome_ua() " + check_chrome_ua());
if (check == "false") {
alert("change text");
document.getElementById("opomena").style.color = "red";
document.getElementById("opomena").innerHTML = 'Warning you are not using Google Chrome';
}
}
</script>
</head>
<body onmousemove="check()">
<div id="opomena">Thank you for using Google Chrome.</div>
</body>
</html>
Popups on Google Chrome popup says true, on Firefox says false
popup "change text" does not display in Firefox tho var check is false.
Thank you in advance for advice
Change
if (check == "false") {
to
if (!check) {
or
if (check == false) {
if you really want to check for the boolean value, false.
The regexp, test method which you call at /chrome/.test(ua) returns a boolean, but check == "false" is comparing that boolean to a string. The two are not equal.
In JavaScript, == is permissive when it comes to comparing strings to objects, but not as permissive as Perl's eq operator.
0 == false
false == false
"false" == [false]
false != "false"
0 != "false"
It's a good habit to try and use !== and === which are well-defined operators to compare primitive values.
You have to check for boolean value false, not "false" (it's a string).
Only if (!check) would be enough (or more verbose version if (check == false) ).

javascript function for different browsers

I am trying to work on some action based on the browser in rails. So I have this in my index.rhtml
<div class="function_tes">
<function whichBrwsr()
{
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("msie") != -1) return 'IE';
if (agt.indexOf("firefox") != -1) return 'Firefox';
}>
</div>
However, I do not see any result of "IE" nor "firefox" when I open index in those browser. Do I need to call whichBrwsr() somewhere in index.rhtml or?
Thank you for any guidance
Here you go.
<div id="myDiv" class="function_tes">
</div>
<script type="text\javascript">
function whichBrwsr()
{
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("msie") != -1) return 'IE';
if (agt.indexOf("firefox") != -1) return 'Firefox';
}
document.getElementById("myDiv").innerHtml = whichBrwsr();
</script>

Categories

Resources