0

我必须修改使用 VBScript 的页面。

我在函数中将局部变量设置为 Application 对象时遇到问题。下面的代码,在getObjectSchema函数中,会产生一个错误:

Set LocalSchema = Application("ObjectSchema")

虽然下面的代码在主体中(在任何函数之外)工作得非常好:

Set Schema = Application("ObjectSchema")

有谁知道出了什么问题?从函数访问应用程序对象是否有任何限制?

这是完整的代码

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
    .style1 {
        width: 87px;
    }
</style>
</head>
<body>
    <script type="text/vbscript">
        Option Explicit

        Dim ObjectSchema
        Dim strXML
        Dim strXML1
        Dim sUserId

        On Error Resume Next

        Function GetUserID()
            GetUserID = "{3450E0D8-EE30-48EE-B63F-486506AD1D97}"
        End Function

        Function getObjectSchema()
            Dim LocalSchema

            Set LocalSchema = Application("ObjectSchema")

            If LocalSchema Is Nothing Then
                Set LocalSchema = CreateObject("Scripting.Dictionary")  
            End if

            If LocalSchema.Exists(sUserId) Then
                strXML = LocalSchema.Item(sUserId)
            Else
                strXML = "<head><title>Title</title></head><head1><title1>Title1</title1></head1>"
                LocalSchema.Add sUserId, strXML
                Set Application("ObjectSchema") = LocalSchema
            End if

            getObjectSchema = strXML
        End Function

        sUserId = GetUserID()
        strXML = ""
        strXML1 = ""
        strXML = getObjectSchema()
        strXML1 = getObjectSchema()
        strXML = getObjectSchema()
        strXML1 = getObjectSchema()

        Dim Schema

        Set Schema = Application("ObjectSchema")

        If Schema Is Nothing Then
            Set Schema = CreateObject("Scripting.Dictionary")  
        End if

        If Schema.Exists(sUserId) Then
            strXML = LocalSchema.Item(sUserId)
        Else
            strXML = "<head><title>Title</title></head><head1><title1>Title1</title1></head1>"
            Schema.Add sUserId, strXML
            Set Application("ObjectSchema") = Schema
        End if   

        strXML1 = strXML
    </script>
</body>
</html>
4

1 回答 1

1

在设置之前尝试空检查LocalSchema

If Not Application("ObjectSchema") Is Nothing Then
    Set LocalSchema = Application("ObjectSchema")
End If

更新

你有这个在一个<script type="text/vbscript">元素中运行,我认为这意味着它是由浏览器解释的(由于vbscript业务原因,特别是 Internet Explorer)。

浏览器可能没有Application对象的概念。

尝试将您的代码包含在代码块中(以在服务器上运行它):

<body>
    <%
    Option Explicit

    Dim ObjectSchema
    Dim strXML
    Dim strXML1
    Dim sUserId

    ...

    strXML1 = strXML
    %>
</body>
于 2013-01-17T17:42:59.753 回答