2015-06-10 33 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數據。

相關問題