document.getElementByID - checking whether an element has been found or not
if isObject(some_object) then
if the variable holds an object then it was found ...
[update]
After some tests and some comments, you need to use the isNothing
method..
since you are setting an object it will always be an object variable type, but if it is not found it is set to nothing ..
if isNothing(some_object) then
I have checked the above with your sample code, and it works as expected..
in vbscript terms it would be
if some_object is nothing then
Kirill Leontev
Updated on June 04, 2022Comments
-
Kirill Leontev almost 2 years
Here's a sample code, that opens an internet explorer window, navigates to google, and gets some element on the page by its unique id:
set ie = CreateObject("InternetExplorer.Application") ie.navigate("www.google.com") ie.visible = true while ie.readystate <> 4 wscript.sleep 100 WEnd set some_object = ie.document.getelementbyid("xjsc") MsgBox some_object.tagname, 0
This sample brings me a
DIV
popup, which satisfies me completely.But at the next step I'd like to check whether some id exists in the page, or not. Unfortunately, I can't just be, like,
set some_object = ie.document.getelementbyid("some_non_existant_id") if some_object.tagname = "" then ...
because it gives me the following error:
ie.vbs(12, 1) Microsoft VBScript runtime error: Object required: 'some_object'
So, what's the best practice to check whether an element has been found or not?
-
Kirill Leontev about 14 yearsyeah, but it retruns
true
for me in BOTH cases, even when nothing was found! -
Gabriele Petrioli about 14 years@be here now, check out
isNull
(or evenisEmpty
). According to specs, it returns null if nothing is found.. -
Kirill Leontev about 14 yearsI did, they all seem to output false regardless the element has been found or not.
-
Kirill Leontev about 14 yearsThings are getting weird....
D:\work\_tmp\ie.vbs(12, 1) Microsoft VBScript runtime error: Type mismatch: 'isnothing'
By the way, isNothing is a VB6 function, isn't it? I havent found it in VBScript refs. -
Gabriele Petrioli about 14 years@be here now, you are right .. i was thinking in terms of vb.. vbscript equivalent is
if some_object is nothing then