Is this a valid jquery split? - javascript

I am using $.cookie('mycookie').split('|')[1] it is working in FF but in IE8 i it is throwing "Object doesn't support this property or method".. Any suggestion?
Here is what i am trying to do,
if($.cookie('mycookie') != null && $.cookie('mycookie').split('|')[1] != '')

A quick look at the plugin indicates that is the expected value if the cookie doesn't exist. The default state is null and then a string if a value is found. Why not just look at document.cookie first yourself:
alert(decodeURIComponent(document.cookie.replace(";","\n\n")));
If the cookie value you are looking for does in fact exist, then I would verify the plugin actually loaded properly:
alert($.cookie.toString());
The get portion of that plugin is about 15 lines so would be trivial to debug if it is the problem. The returned value right now is null, which has no split() method and therefore you are seeing the error in IE as expected.

Related

JavaScript runtime error: '[MethodName]' is undefined

I'm trying to use jQuery and AJAX to validate that users entered a number in a particular field and that they didn't leave it blank and I'm a little confused as to why I can seem to do one, but not the other.
I'm doing this in a jQuery change() function so any time they change the value in that field, it updates it in the database without refreshing the whole page and it works fine until I try to use isNull() to validate.
I'm saving their input to a variable called UserInput and first checking to make sure it's a number with this:
if (!isNaN(UserInput))
which works perfectly. I'm also trying to check and make sure it isn't empty by using this:
if (isNull(UserInput))
Intellisense completes isNull() for me just like it did for isNaN() and all appears well in Visual Studio, it compiles without error. I've also tried isNullOrUndefined() here with a similar result, intellisense completes it for me and all seems well. Right up until I change the value in the field, at which point it promptly gives me this error:
JavaScript runtime error: 'isNull' is undefined.
I'm not sure why it's undefined (especially since intellisense is completing it for me) or how to go about defining it.
I also tried this because it seemed like it covered all the bases, not just isNull():
https://stackoverflow.com/a/5515349/8767826
and I put an alert() inside the if and I didn't get an error, but my alert didn't fire either.
The end goal is to get it to change to a zero on the client side if they leave do leave it blank.
Anyway I'm kind of stumped and I appreciate any help anyone can offer.
Thanks
There's no need for an isNull function; you can check
if (UserInput === null)
isNaN exists because NaN, unlike every other value in JavaScript, is not equal to itself.
But null doesn't mean the field is blank! If the field is blank, its value will be the empty string. Check for that instead:
if (UserInput === '')

check for undefined not working

I just started programming with javascript no long ago. But I am still confused with the way it handles arrays.
the problem is that I have an array in which there some content as undefined. I want to check for those undefined values and escape them. How do I do it. the following code is not working and I have not found a way to solve this problem.
if(myarray[0]!=='undefined')
if(myarray[0])!='undefined') // I have also tried
if(myarray[0]).search('undefined')==(-1)) // and also this
however none of these options is working. I could see when debugging that the value in the array is 'undefined' but the condition is never met. once again my aim is to do something when it is not undefined. please how could I do it. I found a similar question Test if something is not undefined in JavaScript but it does not work
You are comparing undefined to 'undefined', which is a string so its different. You need to compare like this:
myarray=[undefined];
if(myarray[0]!==undefined){
console.log(4)
}
juvian provided a good answer. You need to use undefined instead of 'undefined' because you are comparing your array to a string.
On my current project, I use underscore to check for undefined. It's pretty simple and straight-forward.
var array = [];
if (_.isUndefined(array));
http://underscorejs.org/#isUndefined
You're comparing the myarray[0] value to a string value of undefined.
What you need is this:
if (typeof(myarray[0]) !== 'undefined')
Hope this helps!
You should check it as follows: if(typeof(myarray[0]) != 'undefined')

Javascript json, check all keys for undefined ('null') and set default

Firstoff I'd like to add I've been learning javascript for like only 2 days now. I'm pretty much way ahead of myself with what I'm trying to get but here goes.
I have a json array from which I get data to replace/insert in my page. The first problem I have is that if it comes across an empty ('null') key it will just stop. Will not even try to continu.
document.getElementById("id1")src=json.img1.link;
document.getElementById("id2")src=json.img2.link;
document.getElementById("id3")src=json.img3.link;
json.img2.link is empty ('null' response from json.). javascript will then not replace "id2" but it also won't replace "id3".
I'm now trying to find a solution where it will if nothing else at least set a default.
The script is not continuing executing because it comes to an error --trying to access property link of an undefined object
Try
document.getElementById('id2').src = json.img2 ? json.img2.link : 'defaultLink';
This way your are checking for undefined (ie null) object in img2 and assigning the default value. This assumes that what is not defined (null) is the img2 object.
Actually I don't think your code should work at all, you are missing the. before the src So, try
document.getElementById("id1").src=json.img1.link;
document.getElementById("id2").src=json.img2.link;
document.getElementById("id3").src=json.img3.link;
and let us know if that doesn't solve the problem.
Btw, +points for learning JavaScript and not just straight into jQuery!

JavaScript - passing string as an object property

Just FYI this code is part of ExtJS 4 script. I have a global setting which I use to show the path to the dir where my icons are. I'm making some modifications so before was:
g_settings.iconUrl
but now I need to pass a third argument property like this:
g_settings.iconUrl.docIcon
The problem I met is where I have an if statement where I check for different values:
if (extensions == 'doc'||extensions == 'docx'||extensions == 'xlsx'||extensions == 'xls)
and then I do:
extensions += 'Icon';
and I need to use:
g_settings.iconUrl.extension
when I use console.log(extensions) it's value is as expected (for example docIcon) but when I try to use it as a property it doesn't work. If I use console.log on:
console.log(g_settings.iconUrl+ '.' +extensions)
the output is [object Object].docIcon so I think I have to convert somehow extensions to object too.
I'm not sure that this will solve the problem but that's what I have for now.
Any suggestions?
Thanks
Leron
Why not just add it to the string..
console.log("g_settings.iconUrl." + examples)
iconURL was (and probably still is) a string, but then you added some properties... so on the assignment of .iconURL="..." you should use
iconURL.file="...";//(or other name you choose)
and latter use
.iconURL.file+"."+extensions;

null exception in javascript

I met with the following error from the following javascript functions, any ideas what is wrong?
BTW: since the whole page is long, I can not post them all here. I am trying to find a small but complete sample to reproduce this issue. Any ideas to debug further to find the root cause?
'Null' is null or not an object
Script:
<script>
$(document).ready(function() {
$("#tag0").tooltip({ effect: 'slide'});
});
</script>
thanks in advance,
George
My first guess was that
$("#tag0")
is returning null, and attempting to call a method on null is probably giving you the error. I have been informed that jQuery won't actually return null if your selector doesn't match anything -- you just get an empty set of results with a length property of 0. If you call a nonexistent method on an object of this result type, perhaps you get the error message you're seeing.
Is it possible there isn't actually an element on the page with ID "tag0"? Should it be a class instead (".tag0" instead of "#tag0")?

Categories

Resources