Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.getElementbyId: Object required

Tags:

vbscript

I have some code on a classic ASP page that is attempting to get the innerHTML of a <div> element.

    Dim oElm 
    Set oElm = document.getElementById("screenDiv")

    if oElm Is Nothing then
        '''
    else
        document.getElementById("screenDiv").innerHTML = ""
    end if

I've tried the above but I'm getting the Object Required error.

What do I need to do to get around this error?

like image 677
webdad3 Avatar asked Feb 22 '23 06:02

webdad3


1 Answers

NOTE: client side VBScript is outdated, deprecated, and doesn't work in any modern browser including newer versions of IE.

This means the code is located before the element, so when it executes, such element does not exist.

To solve this, have the code execute in the onload event:

<script type="text/vbscript">
Set window.onload = GetRef("WindowLoad")
Function WindowLoad
    Dim oElm 
    Set oElm = document.getElementById("screenDiv")
    if oElm Is Nothing then
        MsgBox("element does not exist")
    else
        oElm.innerHTML = ""
    end if
End Function
</script>

Credit for the correct way goes to Korikulum, and the official documentation can be found here.

Live test case. (IE only)

like image 199
Shadow Wizard Hates Omicron Avatar answered Mar 05 '23 12:03

Shadow Wizard Hates Omicron