2015-06-10 36 views
0

在VBScript中,我每次运行此代码时都会收到错误消息。我只想重新加载页面,如果在querystring中找不到“p”变量。如何使用新的查询字符串重新加载ASP页面?

我究竟做错了什么?

Dim sURL   'URL of the document 
sURL = document.URL 

'Set page number equal to 1 
If(InStr(sUrl, "?") = 0) Then 
    sURL = sURL & "?p=1" 
    window.location = sURL 
    window.location.reload() 
End If 

回答

2

你在做什么错?它看起来几乎是一切。你的代码看起来像VBS和JavaScript混乱。

你需要的是

<%@LANGUAGE=VBSCRIPT%> 
<% 
    If Request.QueryString("p") = "" Then 
     Response.Redirect Request.ServerVariables("URL") & "?p=1" 
    Else 
     Response.Write "YES! WE HAVE IT!" 
    End If 
%> 
1

您可以使用

If(InStr(sUrl, "?") = 0) Then 
    sURL = sURL & "?p=1" 
    window.location.href = sURL & "?p=1" 
End If 
+0

这个问题被标记为vbscript,但你已经给了一个JavaScript的答案。正如在另一个解决方案中指出的那样,最初的问题是最大化混合两者。 – Dijkgraaf

+1

这很清楚_不是JavaScript的答案_('如果是VB,而不是JS)。虽然这个答案可能是正确和有用的,但是如果你包含一些解释并解释它是如何帮助解决问题的,那么这是首选。如果存在导致其停止工作并且用户需要了解其曾经工作的变化(可能不相关),这在未来变得特别有用。 –

+1

它看起来像VBS,但'window.location'表明它是客户端代码。客户端VBS是独立于ASP的东西(只有Internet Explorer支持) – John

0

何必呢?你只是假设一个默认的,所以当你处理你的网页,并期待它只是设置为默认的p查询字符串值,如果有不匹配的值...

Dim p 
p = Request.QueryString("p") 
If "" & p = "" Then p = 1 

无需任何页面重新加载。

借此阶段进一步我倾向于使用这样的功能...

'GetPostData 
' Obtains the specified data item from the previous form get or post. 
'Usage: 
' thisData = GetPostData("itemName", "Alternaitve Value") 
'Parameters: 
' dataItem (string) - The data item name that is required. 
' nullVal (variant) - The alternative value if the field is empty. 
'Description: 
' This function will obtain the form data irrespective of type (i.e. whether it's a post or a get). 
'Revision info: 
' v0.2 -  Function has been renamed to avoid confusion. 
' v0.1.2 - Inherent bug caused empty values not to be recognised. 
' v0.1.1 - Converted the dataItem to a string just in case. 
function GetPostData(ByVal dataItem, ByVal nullVal) 
    dim rV 
    'Check the form object to see if it contains any data... 
    if request.Form("" & dataItem) = "" then 
     if request.QueryString("" & dataItem)="" then 
      rV = CStr(nullVal) 
     else 
      rV = request.QueryString("" & dataItem) 
     end if 
    else 
     rV = request.Form("" & dataItem) 
    end if 
    'Return the value... 
    GetPostData = rV 
end function 

...让我的代码整洁。如果发布的数据丢失,该函数仅返回默认值。请注意,此函数在返回默认值之前将实际检查QueryString和Form数据。

相关问题