当前位置: 动力学知识库 > 问答 > 编程问答 >

How to copy vbscript to a function and run it when the body onload triggers

问题描述:

I've inherited a site which is mostly vbscript/asp. One page has <% vbscript %> before the DOCTYPE HTML PUBLIC... line and runs before the page opens. I need to get this to run even if the user presses the back button on another page to get there. Presumably by placing it an onload function - Is that correct?

This is a shortened piece of the <% %> code:

<%

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>

I think I need to get it to run like this:

<!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

I've tried to move this code into the function by simply copying the <% code %> and pasting it between the Function - End Function. This doesn't work - the syntax looks incorrect. Can someone please tell me why and what adjustments I need to make it work and also if the onload event will do what I need it to?

Any help appreciated.

网友答案:

Unfortunately what you're asking for is not possible in VBScript/ASP. VBScript/ASP is a server side programming language and you're trying to use it to induce client side behavior. If you want to do something like this, you'd have to do it in Javascript or set the "Meta Refresh" values:

<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 -->
分享给朋友:
您可能感兴趣的文章:
随机阅读: