开发者

How do I setup an "if object exists" condition?

开发者 https://www.devze.com 2023-01-29 22:50 出处:网络
Is there some way to check if an object exists?I keep g开发者_JAVA技巧etting an \"object required\" error.I know the object does not exist and I would like to bypass a part of my code should that be t

Is there some way to check if an object exists? I keep g开发者_JAVA技巧etting an "object required" error. I know the object does not exist and I would like to bypass a part of my code should that be the case. I don't know what I have not tried...

    var codeName = document.getElementById('testCode');
    //I have tried
    if(codeName != null)
    if(codeName.length != 0)
    if(typeOf codeName != 'undefined')
    if(!codeName)
    if(codeName.value != null)

Is there any way to see if an object exists?


After the getElementById call, codeName is either a DOM Element or null. You can use an alert to see which:

alert(codeName);

So if (codename != null) should work.

Does the error happen before it gets that far? I would try adding alerts to see the values as the code runs. Or step through this code in a debugger.


Try:

var codeName = document.getElementById(code[i]) || null;
if (codeName) {/* action when codeName != null */}

if you want to be sure codeName is an Object:

if (codeName && codeName instanceof Object) {
  /* action when codeName != null and Object */
}


var codeList = document.getElementById('codeList');
if(!codeList && !codeList.value && !codeList.value.length) return;
var code = codeList.value.split(","),
    itemCount = code.length;
if(!itemCount) return;
for (var i=0, i<itemCount; i++) {
    var codeName = document.getElementById(code[i]);
    if(!codename || !codename.length) continue;
    //do something here...
}

I got a working example here: http://jsbin.com/uduxe4/15


I don't know what document is, but you could try something like

if(document.getElementById('code'+[i]) == null)
{
//...do Something
}

so testing if it exists before you using it...


I don't do a whole lot of JS coding, but it seems like your [i] is the problem. As far as I know, [] is used for accessing a field of an array, and you don't have an array. Just use "code" + i


In ruby nil is equivalent to false.

So try to check just:

if codeName


<div id='code1'></div>

var itemCount = 10;
var len = 10;
len = itemCount;
for (var i=0;i<len; i++) {
    var codeName = document.getElementById('code'+ i);
    if(codeName == null)
       alert("Nope " + i);
    else
        alert("Yep " + i);
}


Would a try/catch work for you? Example

function toDoStuff(elem) {
    codeName = document.getElementById(elem);
    if (!codeName) throw "Object isn't here yet!"
}


for (var i = 0; i < 5; i++) {
    try {
        toDoStuff('someElem');
    } catch (err) {
        if (err == "Object isn't here yet!") {
            alert("Object isn't ready yet leaving loop!");
            break;
        }
    }
}


Maybe try checking that your object inherits from the type your function needs? Like this:

if(codeName is String)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号