localStorage boolean triggers weird behavior - javascript

I'm having really strange problems with my code using localStorage...
I would post code either here or in jsfiddle but for it to work I need a bunch of resources and for some reason won't display correctly on jsfiddle.
For an example, you can view the webpage I have it hosted at: http://spedwards.cz.cc/new.html
When you check one (can be any value but lets say 1 for this purpose) of the checkboxes for any hero, click Generate (in the last section) and hit refresh, all of the heroes have their activity checked even though only the one that was checked prior to the refresh should remain checked.
When checking localStorage in the console, only the checked one will have true and all the others will be on false as well which makes it weird. If someone can explain why it's doing this and/or explain an error that I've obviously missed.
Below I will post some of the functions.
Storing everything:
function storage() {
var username = $('#username').val();
var password = $('#password').val();
if (typeof(Storage) != "undefined") {
// Store user's data
window.localStorage.setItem("username", username);
window.localStorage.setItem("password", password);
$.each(heroes, function(index,value){
window.localStorage.setItem("heroActive" + index, $('input#isActive' + index).is(':checked') );
window.localStorage.setItem("heroLevel" + index, $('input#level' + index).val() );
window.localStorage.setItem("heroPrestige" + index, $('input#prestige' + index).val() );
});
} else {
// Browser doesn't support
alertify.alert('<b>Your browser does not support WebStorage</b>');
}
}
Loading the values:
var username, password;
username = localStorage.getItem('username');
$('#username').val(username);
$.each(heroes, function(index,value){
if( localStorage.getItem('heroActive' + index) ){
$('input#isActive' + index).attr('checked', localStorage.getItem('heroActive' + index) );
} else {
$('input#isActive' + index).removeAttr('checked');
}
$('input#level' + index).val( localStorage.getItem('heroLevel' + index) );
$('input#prestige' + index).val( localStorage.getItem('heroPrestige' + index) );
});
The list that is causing problems:
var heroes = ["Black Panther","Black Widow","Cable","Captain America","Colossus","Cyclops","Daredevil","Deadpool",/*"Doctor Strange",*/"Emma Frost",
"Gambit","Ghost Rider","Hawkeye","Hulk","Human Torch","Iron Man","Jean Grey",/*"Juggernaut",*/"Loki","Luke Cage",/*"Magneto","Moon Knight",*/"Ms Marvel",
"Nightcrawler",/*"Nova","Psylocke",*/"Punisher","Rocket Raccoon",/*"Silver Surfer",*/"Scarlet Witch","Spider-Man","Squirrel Girl",/*"Star-Lord",*/"Storm",
/*"Sue Storm",*/"Thing","Thor","Wolverine"/*,"Venom"*/];
Additionally, the last entry (Wolverine) doesn't seem to be functioning correctly. For a starter clicking the label for its activity doesn't trigger the checkbox whereas all the others do. Other problems with this entry:
Doesn't trigger my errors.js file at all
errors.js:
$.each(heroes, function(index,value){
$('input#level' + index).change(function() {
var numbers = /^[0-9]+$/;
var val = $('input#level' + index).val();
if(val > 60) {
alertify.log("Hero " + value + " cannot be above Level 60!", "", 0);
$('#level' + index).addClass('error');
} else if( isNumeric(val) ) {
if( $('#level' + index).hasClass('error') ) {
$('#level' + index).removeClass('error');
}
} else {
alertify.log("Only numbers are accepted.");
$('#level' + index).addClass('error');
}
});
});
function isNumeric(num){
return !isNaN(num);
}

Anything stored in local storage returns as a string.
So if you stored fooo = false in local storage,
if(!fooo){
bar();
}
will never execute bar();
if(fooo){
bar();
}
will always execute bar(), since a string always is true, since the variable is not empty or false.
I've banged my head against this once as well. Kinda stupid, but hey. :)

Related

ServiceNow: JavaScript TypeError: Cannot read property of undefined

I am writing an onChange Client Script in ServiceNow and having issues with a Javascript error on the front end client. I keep getting a TypeError: Cannot read property 'u_emp_name' of undefined. the variable seems to vary as at one point i was getting the u_pos_office undefined as well, however the data is pulling correctly and there are no performance impacts on my code functionality.
What could be causing the type error?
Script is below:
function onChange(control, oldValue, newValue, isLoading) {
var billNum = g_form.getReference('u_billet',findBilletInfo);
console.log('Emp Name: ' + billNum.u_emp_name);
console.log('OFfice: ' + billNum.u_pos_office);
console.log('Career Field: ' + billNum.u_pos_career_field);
if (isLoading || newValue == '') {
return;
}
if (oldValue != newValue){
findBilletInfo(billNum);
}
function findBilletInfo(billNum){
console.log('Bill Num' + billNum);
console.log('encumbent' + billNum.u_emp_name);
var empName = billNum.u_emp_name;
var empNameStr = empName.toString();
console.log(empName);
console.log(empNameStr);
g_form.setValue('u_organization_office',billNum.u_pos_office);
g_form.setValue('u_encumbent',billNum.u_emp_name);
g_form.setValue('u_old_career_field',billNum.u_pos_career_field);
g_form.setValue('u_old_career_specialty',billNum.u_pos_career_specialty);
g_form.setValue('u_old_occupational_series',billNum.u_pos_series);
g_form.setValue('u_old_grade',billNum.u_pos_grade);
g_form.setValue('u_old_work_category',billNum.u_pos_category);
g_form.setValue('u_old_job_title',billNum.u_pos_title);
g_form.setValue('u_losing_rater',billNum.u_emp_rater_name);
g_form.setValue('u_losing_reviewer',billNum.u_emp_reviewer_name);
}
}
It appears to be an error here
var billNum = g_form.getReference('u_billet',findBilletInfo);
==> console.log('Emp Name: ' + billNum.u_emp_name);
In this case billNum is undefined since getReference is run asynchronously. See the documentation for the function.
This means that it won't guarantee a return value immediately or at all. This is probably why you get a record sometimes and not others.
You can move these debug logs within your findBilletInfo callback to check the values
if (isLoading || newValue == '') {
return;
}
var billNum = g_form.getReference('u_billet',findBilletInfo);
function findBilletInfo(billNum) {
console.log('Bill Num' + billNum);
console.log('encumbent' + billNum.u_emp_name);
console.log('OFfice: ' + billNum.u_pos_office);
console.log('Career Field: ' + billNum.u_pos_career_field);
...
}
If you debug in Firefox or Chrome, you should be able to just log the object to the console to explore the entire object at once.
function findBilletInfo(billNum) {
console.log(billNum);
...
}
The output will look like something like this in the console and you can see all fields at once.

variable is not getting defined even though the code works somwhere else

so i am building a game in three js and trying to make it multiplayer throught socket.io so i am loading all of my characters into an array called players on my server side
and then i pass it to each client when they connect like so
socket.on('addPlayer', function(username) {
players.push(username)
console.log(username + " joined")
console.log("online Users " + players)
socket.broadcast.emit('syncPlayers', players)
socket.emit('syncPlayers', players)
})
and on my client syncPlayers looks like this
socket.on('syncPlayers', function(players) {
players.forEach(function(value) {
if (value == username) {
console.log("not adding " + value + " thats you ")
loadPlayerdata(username)
} else {
console.log("player Online " + value);
newplayer = value;
loadPlayerdata(newplayer)
addPlayer(newplayer)
}
});
})
then it calls this wich sends the server data
function loadPlayerdata(playerName) {
console.log(playerName)
console.log("phase1")
socket.emit('loadPlayerdata', playerName)
}
then this is called and it retrieved the player name and the data of the players location this is were my problem lies
socket.on('loadPlayerdata', function(data, username) {
toMove = threeObjects[username + "Char"]
if (data == "null" || "") {
console.log(username + " is new")
} else {
console.log(username + " Exists")
console.log(toMove)
toMove.position.set(world.spawnPointx, world.spawnPointy, world.spawnPointz)
}
i keep getting Uncaught TypeError: Cannot read property 'position' of undefined
even though i can use this
function addPlayer(playerName) {
var charObjectName = playerName + "Char"
var threeObject = models.tent1.mesh.clone();
scene.add(threeObject)
//threeObject.position.set(world.spawnPointx, world.spawnPointy, world.spawnPointz)
// set reference
threeObjects[charObjectName] = threeObject;
}
btw i have an object
var threeObjects = {};
can someone please explain why it wont work and how to fix it
You can read this answer to understand the difference between dot and brackets notation.
You are getting error because, tomove seems to be undefined and dot notation will throw error if any new user joins and if the object is empty.
Check if this helps. This will assign the object key as username and position as an value which will be array like this,
{"usernamechar": {"position": [x,y,z]}}
socket.on('loadPlayerdata', function(data, username) {
if (data == "null" || "") {
console.log(username + " is new")
} else {
console.log(username + " Exists")
threeObjects[username + "Char"]["position"] = [world.spawnPointx, world.spawnPointy, world.spawnPointz]
}
}

JavaScript global boolean Variable convert itself to string

first I'm new to Javascript, so please don't hate me if I ask such an trivial question.
I've searched some hours on Google, but I cannot find a solution for my Problem. I have declared a global Variable "status", which is false. But if I check the datatype within a function, it says "string", which is allways true, so that my Script doens't work at all.
var status = false;
function slide(element) {
if (status) {
slideUp(element);
} else {
slideDown(element);
}
}
function slideDown(element) {
status = true;
// Testprint todo remove
alert('Status is ' + status + ' and is type: ' + typeof status);
// todo
}
function slideUp(element) {
status = false;
// Testprint todo remove
alert('Status is ' + status + ' and is type: ' + typeof status);
// todo
}
Am I wrong or have I write myself a function to convert every time a boolean var to a "real" boolean var? I have tested it by myself and this show me boolean datatype...
This works fine...
var test = false;
myTestFunc();
myTestFunc() {
if(!test) {
test = true;
alert('Test is false');
} else {
test = false;
alert('Test is true');
}
alert('Test contains: ' + test + ' and has the type: ' + typeof test);
}
I see, that Javascript seems to randomly declare Variables. Is there a solution to make my Variable to a boolean datatype?
Better is there a chance to force the initialation to boolean like int varname?
The problem is window.status - see https://developer.mozilla.org/en-US/docs/Web/API/Window/status
change the name (or scope) of the var and it's all sweet
For boolen use this way from MDN doc
var test = new Boolean(false);

SDK GridRefresh Call Throwing Exception

I'm going to try to explain this as best I can, please feel free to ask for clarifications as required.
Using IE10, CRM Online with RU12.
I am playing about with subgrids and getting them to refresh. Consider the following script, which I have nicked wholesale from MSDN (and wrapped in a try/catch block)
function start() {
try {
var controls = Xrm.Page.ui.controls.get(isSubGrid);
if (controls.length > 0) {
var subGridNames = "";
for (var i in controls) {
controls[i].refresh();
subGridNames += (" - " + controls[i].getName() + "\n");
}
alert("The following subgrids were refreshed: \n" + subGridNames);
}
else {
alert("There are no subgrid controls on the current form.");
}
}
catch (ex) {
alert(ex);
}
}
function isSubGrid (control)
{
return control.getControlType() == "subgrid";
}
Nothing special going on there - get all controls of type subgrid (this returns 10 elements as expected) and call refresh() on them.
However this is consistently failing on the first call to refresh().
The exception details is fairly straightforward
TypeError: Unable to get property 'Refresh' of undefined or null reference
Which suggests that the control[i] is null when called in the loop at this point here
for (var i in controls) {
controls[i].refresh();//error thrown here - suggests controls[i] is null
subGridNames += (" - " + controls[i].getName() + "\n");
}
However I can see that it isn't null (and has the method refresh as expected).
I can make it work by using setInterval
function waitAndThenRefresh(gridname) {
var grid = Xrm.Page.ui.controls.get(gridname);
var intervalId = setInterval(function () {
if (grid === null || grid._control === null || grid._control._element === null) {
return;
}
if (grid._control._element.readyState === 'complete') {
window.clearInterval(intervalId);
if (grid != null) {
grid.refresh();
}
}
}, 1000);
}
But that is pretty hideous, not to mention does not explain with the SDK call doesn't work as expected.
So I guess the question is: has anyone else seen this issue? Or can you replicate it on another instance? Am I missing something? There is nothing in the SDK that suggests you need to defer calling refresh until the inner control's readyState is complete?
The code block you are using,
for (var i in controls) {
controls[i].refresh();
subGridNames += (" - " + controls[i].getName() + "\n");
}
should be replaced with the following:
for (var i in controls) {
i.refresh();
subGridNames += (" - " + i.getName() + "\n");
}
or:
for (var i = 0; i < controls.length; i++) {
controls[i].refresh();
subGridNames += (" - " + controls[i].getName() + "\n");
}
You are getting the exception because controls[i] is undefined in your case, i being the control (the element of the array controls).
I asked a CRM-buddy of mine. He said that the issue depends on the new refreshment Engine. According to him, it's sort of a bug but not really. If I got it right, the refresh has been reengineered to accommodate the new perpetual saving functionality.

javascript cookie is not returning the value

I have a cookie that is being set by this function (page calendar.html):
function bookit(id){
document.cookie='eventid' + "=" + id;
document.location.href ="/sys/account.php";
}
After that the users is redirected to account.php so he can sign in and the id is used on that page and this is what I have on account.php:
function getCookieValue(key)
{
currentcookie = document.cookie;
if (currentcookie.length > 0)
{
firstidx = currentcookie.indexOf(key + "=");
if (firstidx != -1)
{
firstidx = firstidx + key.length + 1;
lastidx = currentcookie.indexOf(";",firstidx);
if (lastidx == -1)
{
lastidx = currentcookie.length;
}
return unescape(currentcookie.substring(firstidx, lastidx));
}
}
return "";
}
alert(getCookieValue("eventid"));
well the dilemma is : when I refresh calendar.html the alert returns 51 for example which is the value of that event, BUT when i refresh account.php it says: NULL
I have tried a jquery cookie plugin, same results so I went back to basic javascript, its driving me insane at this point I dont understand how to pass that value.
I figured using cookies would be the right method - but its not working.any suggestions please? I have a javascript debugger my code has no errors.

Categories

Resources