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);
Related
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.
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]
}
}
I tried to insert a function into javascript object but I get
undefined
This is supposed to return a message into div.
I would like to do it the first way, but here is the both way I tried :
var errorMessage = {
empty: function(message){"<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"
}
};
console.log(errorMessage.empty("Hello"));
I also try this way
function errorMessage(message){
"<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>"
}
console.log(errorMessage("hello"))
You need a return of the value literally.
The return statement ends function execution and specifies a value to be returned to the function caller.
var errorMessage = {
empty: function (message) {
return "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>";
}
};
console.log(errorMessage.empty("Hello"));
Your function needs to return the value
var errorMessage = {
empty: function(message){
return "<div class='field'><div class='csv'><span class='icon'></span><label class='manual' id='error-message'>" + message + "</label></div></div>";
}
};
console.log(errorMessage.empty("Hello"));
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. :)
I'm trying to modify the code from this script. Basically I'm trying to get the script to send the browser to another page rather than display the results in a div.
This is the code in question:
<script type="text/javascript">
function openOneSlot() {
SpinningWheel.addSlot({1: 'First', 2: 'Second'});
SpinningWheel.setCancelAction(cancel);
SpinningWheel.setDoneAction(done);
SpinningWheel.open();
}
function done() {
var results = SpinningWheel.getSelectedValues();
document.getElementById('result').innerHTML = 'values: ' + results.values.join(' ') + '<br />keys: ' + results.keys.join(', ');
}
function cancel() {
document.getElementById('result').innerHTML = 'cancelled!';
}
window.addEventListener('load', function(){ setTimeout(function(){ window.scrollTo(0,0); }, 100); }, true);
</script>
I've changed the 'done' function to as follows:
function done() {
var results = SpinningWheel.getSelectedValues();
if (SpinningWheel.getSelectedValues() == "1,First")
{
window.location="first.html";
}
else if (SpinningWheel.getSelectedValues() == "2,Second")
{
window.location="second.html";
}
else
{
alert("Damn, Still not working..");
}
But now I'm lost as I'm very new to javascript.. Can anyone help the noob to get this working?
:)
Try this:
function done() {
var results = SpinningWheel.getSelectedValues();
if (results.values[0] == "First")
{
window.location.href="first.html";
}
else if (results.values[0] == "Second")
{
window.location.href="second.html";
}
else
{
alert("Damn, Still not working..");
}
The returned values appear to be an array of all the slots. Since yours has only one slot, I'm only looking at the first array position of the "results.values" array.
Try location.href instead of window.location
Note that this particular thing (changing page) is done differently across different browsers so you should google "javascript redirect " if you run into trouble on a particular browser
Look at what is returned by SpinningWheel.getSelectedValues(). It is an object with two properties keys and values. Therefore, it will not equal "1,First". Check into what is in those properties, and base your conditionals on those.
To pass your variables through, use the following syntax:
window.location = "first.html?var1Name=" + var1 + "&var2Name=" + var2;
To get the value of those variables on your first.html and second.html pages, you can use the window's query string to get hold of their values:
http://www.eggheadcafe.com/articles/20020107.asp
You would want to look at the window.location.search property.