如何将vbscript复制到函数中,并在body onload触发时运行它



我继承了一个主要是vbscript.asp的网站。一个页面有<%在DOCTYPE HTML PUBLIC之前的vbscript%>。。。行,并在页面打开前运行。即使用户按下另一个页面上的后退按钮,我也需要让它运行。大概是通过将其放置为一个onload函数-这是正确的吗?

这是<%%>的缩短部分代码:

<%
if not (IsEmpty(Session("MM_Username"))) then
    Set checkSet = Server.CreateObject("ADODB.Recordset")
        checkSet.ActiveConnection = MM_CA_STRING
        checkSet.Source = "SELECT * FROM cpgdb.dbo_tbl_printing_tempstore WHERE username = '" & Session("MM_username") & "' AND addedtocart = 'NO'"
        checkSet.Open()
end if
%>
<!DOCTYPE HTML PUBLIC...
...
<body>

我想我需要让它像这样运行:

<!DOCTYPE HTML PUBLIC...
...
<body onload="runcode()">
Function runcode()
    if not (IsEmpty(Session("MM_Username"))) then
        Set checkSet = Server.CreateObject("ADODB.Recordset")
        checkSet.ActiveConnection = MM_CA_STRING
        checkSet.Source = "SELECT * FROM cpgdb.dbo_tbl_printing_tempstore WHERE username = '" & Session("MM_username") & "' AND addedtocart = 'NO'"
        checkSet.Open()
    end if
End Function

我试图通过简单地复制<%代码%>,并将其粘贴在Function-End Function之间。这不起作用-语法看起来不正确。有人能告诉我为什么以及我需要做什么调整才能让它发挥作用吗?以及加载事件是否会达到我需要的效果?

感谢您的帮助。

不幸的是,您所要求的在VBScript/ASP中是不可能的。VBScript/ASP是一种服务器端编程语言,您正试图使用它来诱导客户端行为。如果你想做这样的事情,你必须用Javascript或设置"元刷新"值:

<meta http-equiv="cache-control" content="no-cache"> <!-- tells browser not to cache -->
<meta http-equiv="expires" content="0"> <!-- says that the cache expires 'now' -->
<meta http-equiv="pragma" content="no-cache"> <!-- says not to use cached stuff, if there is any -->

最新更新