添加或替换传统的ASP查询字符串参数 [英] Add or replace a query string parameter in classic ASP

查看:265
本文介绍了添加或替换传统的ASP查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套它包括每个页面上的链接header.asp;这些链接有自己的href属性设置为任何当前页面加上一个参数的linkID。有时页面的链接引用,将会产生查询字符串参数,可以但有时不会。如果当前页面的URL中包含的linkID参数已经我需要更换,否则我需要将其添加到当前的URL。

I have a set of links in header.asp which is included on every page; these links have their href attribute set to whatever the current page is plus a linkID param. Sometimes the page the link references will have query string params, but other times it won't. If the current page URL contains the linkID param already I need to replace it, otherwise I need to add it to the current URL.

我串联Request.ServerVariables( SCRIPT_NAME )和Request.ServerVariables( QUERY_STRING )成一个变量的页面名称的获取当前网页的网址。不幸的是,我使用的是传统的ASP / VBScript中。

I am concatenating Request.ServerVariables("SCRIPT_NAME") and Request.ServerVariables("QUERY_STRING") into a variable pagename to get the current page URL. Unfortunately, I'm using classic ASP/VBScript.

我想我需要使用恩章,但我不是在它的专家,尤其不要在传统的ASP。当我需要更换元素作为参数的linkID的价值可以相差如此简单的替换法将无法正常工作的问题就来了。

I think I need to use reg ex but am not expert in it, especially not in classic ASP. The problem comes when I need to replace the element as the linkID param's value can vary so the simple Replace method won't work.

推荐答案

您可以检查,如果该参数已经在查询字符串的Request.QueryString(的linkID)=并简单地追加如果不是,并使用一个函数来更新值,如果它是存在的。这样的事会被罚款,只要你没有的参数数量庞大,你就不必大量优化,但是,那么你很可能不使用ASP。

You can check if the parameter is already in the querystring Request.QueryString("linkID") = "" and simply append if it isn't and use a function to update the value if it is there. Something like this will be fine as long as you don't have a massive number of parameters and you're not having to optimise massively but then you would probably not be using ASP.

Function updateQueryStringParam(key, value)

  Dim qs, x

  For Each x In Request.QueryString
    qs = qs & x & "="

    If x = key Then
      qs = qs & value & "&"
    Else
      qs = qs & Request.QueryString(x) & "&"
    End If
  Next

  If Len(qs) > 0 Then
    If Right(qs, 1) = "&" Then
      qs = Left(qs, Len(qs)-1)
    End If
  End If

  updateQueryStringParam = qs

End Function

您可以创建另一个函数调用的检查,然后更新使用上述功能;

You could create another function call for the check and then update using the function above;

Function setQueryStringValue(key, value)

  Dim qs

  If Request.QueryString(key) = "" Then
    If Len(qs) > 0 Then
      qs = qs & "&"
    End If
    qs = key & "=" & value
  Else
    qs = updateQueryStringParam(key, value)
  End If

  setQueryStringValue = qs

End Function

这篇关于添加或替换传统的ASP查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆