How do I reload an ASP page with a new querystring?

928 views Asked by At

In VBScript I'm getting an error every time I run this code. I just want to reload the page if the "p" variable can't be found in the querystring.

What am I doing wrong exactly?

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
3

There are 3 answers

0
Zam On

What are you doing wrong? It looks like almost everything. Your code looks like mess of VBS and JavaScript.

What you need is

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

You can use

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

Why bother? You're simply assuming a default, therefore when you process your page and look up the p QueryString value just set it to the default if there's not a matching value...

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

No need for any page reloads.

To take this a stage further I tend to use a function like this...

'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

...to keep my code tidy. The function simply returns a default if the posted data is missing. Note that this function will actually check for both QueryString and Form data before returning the default.