Removing a QueryString

Posted on 2006-05-27
Last Modified: 2008-03-10
Is there a method I can use to remove part of a QueryString. For example I the url


If I want to remove url but keep new and id, how would I do that?

Question by:eadam-uk
    LVL 95

    Expert Comment

    by:Lee W, MVP
    Not sure what you're looking for without a more clear code example...

    You could just do this:

    Original URL

    new URL
    url="bob.asp?id=" & Request.QueryString("id") & "&new=" & Request.QueryString("new")
    LVL 4

    Expert Comment

    If your looking to extract the "new" and "Id" values then
    New = Request.QueryString("New")
    Id = Request.QueryString("Id")

    If you are looking to keep the string intact and just get rid of the bob.asp
    then text functions.

    URL = "bob.asp?id=12345&url=next.asp&new=jimmy"
    NewUrl = Mid(URL,instr(1,URL,"?",1),Len(URL)-instr(1,URL,"?",1)+1)

    This will variable NewUrl the value"?id=12345&url=next.asp&new=jimmy"
    LVL 1

    Expert Comment

    You may want to redirect to 'bob.asp?id=12345&new=jimmy' by the help of this code:

    LVL 1

    Accepted Solution

    Here i worte a function to do what you want:

    Function RemoveQueryString(ByVal OriginalQS, ByVal RemoveItem)
          If OriginalQS = "" Then Exit Function
          If RemoveItem = "" Then Exit Function
          Dim newQS: newQS = ""
          tmpQS = Split(OriginalQS ,"&")
          For i=0 To UBound(tmpQS)
                tmpPair = Split(tmpQS(i),"=")
                If Trim(tmpPair(0)) <> Trim(RemoveItem) Then
                      If Trim(newQS) <> "" Then newQS = newQS &"&"
                      newQS = newQS & tmpPair(0) &"="& tmpPair(1)
                End If
          RemoveQueryString = newQS
    End Function

    Just pass to the function the full query string and the value you want to remove and it will return you the querystring without the part you want to remove, example:

    OriginalQueryString = "id=12345&url=next.asp&new=jimmy"
    newQueryString = RemoveQueryString(OriginalQueryString,"url")
    response.write newQueryString

    it will return id=12345&new=jimmy

    Tip: To get the querystring in a page use: Request.ServerVariables ("QUERY_STRING")
    and it will return the querystring without the filename and ? (filename.asp?)



    Author Comment

    Thanks Valdimir, just what I wanted. Apologies for the tardy response back but looks like the admin got it right!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now