I am trying to add simple themes to my website. The script is supposed to create a theme cookie to see what theme is used and then apply the style. It used to work but now it gets set to httpOnly(meaning it cant be changed by JS even if it gets created by JS). It gets set to http only true even if I specifficaly try to set it to false which prevents me from changing it. Here is the code:
// Themes
var numOfThemes = 2;
var theme = 0;
// Standart getCookie function copied from w3schools
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// This function sets the theme depending on the value inside the theme cookie (eg.: "theme=3")
function applyTheme() {
var cookie = parseInt(getCookie("theme"));
var hs = document.getElementsByTagName('style');
if(hs.length > 1);
for (var i=0, max=hs.length; i < max; i++) {
hs[i].parentNode.removeChild(hs[i]);
}
switch(cookie) {
case 0:
theme = 0;
break;
case 1:
theme = 1;
var style = document.createElement('style');
style.innerHTML = ``;
document.head.appendChild(style);
break;
//...
}
}
// This is supposed to set the cookie inside the browser. I tried adding HttpOnly=false; at the end but it doesn't change anything
function setTheme(theme) {
const d = new Date();
d.setTime(d.getTime() + (365*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = "theme=" + theme + ";" + expires + ";path=/;SameSite=Lax;";
}
// this is the function for switching the theme on a button click
function themeSwitch() {
var theme = + parseInt(getCookie("theme")) + 1;
if(theme > 4) {
theme = 0;
}
setTheme(theme);
}
applyTheme();
Related
1Page.js
function TableRow() {
let cells = document.querySelectorAll('#recieve-info td');
cells.forEach(cell => cell.onclick = function () {
let prevcell = cell.previousElementSibling;
if (prevcell) {
let LSItems;
let AddValue = '/images/back_arrow.png.png'
if (localStorage.getItem('passvalue') === null) {
LSItems = [];
} else {
LSItems = JSON.parse(localStorage.getItem('passvalue'));
}
LSItems.push([AddValue]);
localStorage.setItem('passvalue', JSON.stringify(LSItems));
let prev = prevcell.innerHTML;
console.log(prev);
}
});
}
I am trying to pass values by onclick, like if i click on the first one which is 'Save1' i want to save my value only in the first one.
2Page.js
function ParaG() {
document.querySelector('.Second-Para').innerHTML = JSON.parse(localStorage.getItem('passvalue'));
}
Here i want the values.
You can use cookies.
You can save the cookie:
document.cookie = "option=Option1";
And get the cookie:
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
console.log(getCookie("option"))
https://w3schools.com/js/js_cookies.asp
When I am trying to get the Cookie "Gold" which is a currency for my game, it returns NaN. right now the cookie is named "gold" and has a value of 7238 (for me obviously as I saved that as a cookie on my comp)
function getCookie(cname){
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i= 0; i < ca.length; i++){
var c = ca[i];
while(c.charAt(0) == ' '){
c = c.substring(1);
}
if(c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
var gold;
function getGold() {
if(getCookie("gold") == null) {
gold = 0;
} else {
var sup = getCookie("gold");
var co = parseInt(sup, 10);
gold += co;
}
}
getGold();
This results is NaN which is not helping as it isn't saying where I went wrong, I can also give the function I use to save cookies which is as followed:
function saveCookies(cname, value) {
var d = new Date();
d.setTime(d.getTime() + (365 * 24 * 60 * 60 *1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + value + ";" + expires + ";path=/";
}
Then when some hooligan clicks the "Save" button one of the functions it performs is
saveCookies("gold", gold")
That concludes all the information I could possibly give unless you need the whole program, which can be located at matrix-hub.com/CodeCoins.php
This results is NaN which is not helping as it isn't saying where I went wrong
First of all change this line saveCookies("gold", gold") to saveCookies("gold",7238) here your first argument is cookie name that is gold and second one is cookie value that should be 7238 not "gold"
Secondly, when you set var gold; the value of gold is undefined. So in your code block when you go to the else block because getCookie("gold") == null condition is false, value of gold is still undefined
var gold;
function getGold() {
if(getCookie("gold") == null) {
gold = 0;
} else {
var sup = getCookie("gold");
var co = parseInt(sup, 10); //7238
// this is where you messed up, undefined = undefined + 7238
gold += co;
}
return gold; // also missed this return line
}
console.log(getGold()); // it is returning NaN
Actually with this line gold += co; what do you want to accomplish ?
if you set cookie value gold=7238 then this line gold += co; should return 14476?
I'm trying to get the browser cookies using:
browser.cookies.getAll() but I always get this error in the console log instead:
Uncaught ReferenceError: browser is not defined
here's my code:
var gettingAll = browser.cookies.getAll({
url: "url"
});
console.log(gettingAll);
to get the url value u can use this solution :
var cookiesMap = document.cookie.split(";").map( value => {
var val =value.split("=")
var obj = { "key" : val[0], "value" : val[1] }
return obj;
});
for( var i = 0 ; i < cookiesMap.length ; i++ ){
if( cookiesMap[i].key==="url"){
console.log(cookiesMap[i].value);
}
}
hope it helps :)
Have a look at the following...
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
Edit: fixed code block.
browser is indeed undefined. It's not a native JavaScript object.
You should use document.cookie (see here) instead.
I'm rewriting the scripts for a site in regular Javascript to speed up the color picker, however if you view it in Firefox and IE, it doesn't work (But it works fine in Chrome). I was just really hoping to get some help with this:
https://www.sinister.ly/index.php
<div id="theme_styler">
<div class="option default" id="red"></div>
<div class="option" id="green"></div>
<div class="option" id="blue"></div>
</div>
The specific code that seems to be the problem:
var optionalStylesheet = document.getElementsByClassName("stylesheet_optional");
var blueStylesheet = document.getElementById("stylesheet_blue");
var greenStylesheet = document.getElementById("stylesheet_green");
var storedThemeColor = readCookie('themeColor');
var currentFiconPath;
function changeColor(path) {
var all = document.getElementsByClassName("ficon");
for (var i=0, max=all.length; i < max; i++) {
all[i].src = all[i].src.replace(/ficons\/((green|blue)\/)?/, 'ficons/' + path + '/');
}
}
This is in the header:
// AD JS
document.getElementsByClassName = function(cl) {
var retnode = [];
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
if((' ' + elem[i].className + ' ').indexOf(' ' + cl + ' ') > -1) retnode.push(elem[i]);
}
return retnode;
};
function readCookie(name) {
var ca = document.cookie.split(';');
var nameEQ = name + "=";
for(var i=0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
var optionalStylesheet = document.getElementsByClassName("stylesheet_optional");
var blueStylesheet = document.getElementById("stylesheet_blue");
var greenStylesheet = document.getElementById("stylesheet_green");
var storedThemeColor = readCookie('themeColor');
var currentFiconPath;
function changeColor(path) {
var all = document.getElementsByClassName("ficon");
for (var i=0, max=all.length; i < max; i++) {
all[i].src = all[i].src.replace(/ficons\/((green|blue)\/)?/, 'ficons/' + path + '/');
}
}
var gs = document.createElement("link");
gs.type = "text/css";
gs.className = "stylesheet_optional";
gs.id = "stylesheet_blue";
gs.rel = "stylesheet";
gs.title = "mystyle";
gs.href = "images/sinisterly/color_blue.css";
var bs = document.createElement("link");
bs.type = "text/css";
bs.className = "stylesheet_optional";
bs.id = "stylesheet_green";
bs.rel = "stylesheet";
bs.title = "mystyle";
bs.href = "images/sinisterly/color_green.css";
var head = document.getElementsByTagName("head")[0];
var links = head.getElementsByTagName("link");
for(var x=0; x<links.length; x++) {
var href = links[x].href;
if(href.indexOf('/color_green.css') >0 || href.indexOf('/color_blue.css') >0){
head.removeChild(links[x]);
}
}
if (storedThemeColor == "green"){
head.appendChild(bs);
} else if (storedThemeColor == "blue") {
head.appendChild(gs);
}
// End AD JS
And this in the footer
if (storedThemeColor == null) {
setCookie('themeColor', 'default', 7);
currentFiconPath = '';
return currentFiconPath;
} else if (storedThemeColor != null) {
if (storedThemeColor == 'default') {
currentFiconPath = '';
optionalStylesheet.disabled = true;
return currentFiconPath;
} else if (storedThemeColor == 'blue') {
currentFiconPath = "blue";
changeColor(currentFiconPath);
optionalStylesheet.disabled = true;
blueStylesheet.disabled = false;
return currentFiconPath;
} else if (storedThemeColor == 'green') {
currentFiconPath = "green";
changeColor(currentFiconPath);
optionalStylesheet.disabled = true;
greenStylesheet.disabled = false;
return currentFiconPath;
}
}
Change:
var blueStylesheet = document.getElementById("#stylesheet_blue");
var greenStylesheet = document.getElementById("#stylesheet_green");
to:
var blueStylesheet = document.getElementById("stylesheet_blue");
var greenStylesheet = document.getElementById("stylesheet_green");
There's no # at the beginning of the stylesheet IDs.
I don't know why it's working in Chrome -- when I try it I get undefined in Chrome.
i want to make a cokkie which can save my selected region on netsoltech.com whenever user select the region on map it can save the cookie and when the next time user come on domain the automatic go to 1st time click region page i make this code
but the problem is when the user come on next time then 1st region selector page come then it can refresh and go to the region page.. not the automatic...
<script>
/*
Cookie script - Scott Andrew
Popup script, Copyright 2005, Sandeep Gangadharan
*/
function newCookie(name,value,days) {
var days = 10; // the number at the left reflects the number of days for the cookie to last
// modify it according to your needs
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString(); }
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/"; }
function readCookie(name) {
var nameSG = name + "=";
var nuller = '';
if (document.cookie.indexOf(nameSG) == -1)
return nuller;
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
return null; }
function eraseCookie(name) {
newCookie(name,"",1); }
function toMem(region) {
newCookie('region', region);
window.location= "http://www.netsoltech.com/"+region+"/index";
}
function remCookie() {
window.location= "http://www.netsoltech.com/"+readCookie("region")+"/index";
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
remCookie();
});
</script>
Try this
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
//redirect here
}
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function checkCookie() {
var region = getCookie("region");
if (region != null && region != "") {
alert("redirect to " + region); //replace this with redirect
}
}
checkCookie(); //check this on page load
and the HTML
europe
JSFiddle
http://jsfiddle.net/YtF2B/6/