简体   繁体   中英

pass query string from aspx to html back to aspx, using javascript, link problem

I am passing a querystring from my previous page to this page and then i want to pass it to the next page, but it isn't working.

<script type="text/javascript">
           function qs(search_for) {
        var query = window.location.search.substring(1);
        var parms = query.split('&');
        for (var i=0; i<parms.length; i++) {
            var pos = parms[i].indexOf('=');
            if (pos > 0  && search_for == parms[i].substring(0,pos)) {
                return parms[i].substring(pos+1);;
            }
        }
        return "";
    }
    </script>

here is the link

<a href="http://www.TEST.com/TEST/TEST/TEST.aspx?comp=" & <script type="text/javascript"> document.write(qs("comp")); </script> & "name=test" >CLICK HERE </a></font></b></p>

只需将其粘贴到隐藏字段中,然后使用简单的document.getElementById()进行检索,可能会更容易,更灵活且更易于维护。

  • Page 1 Creates URL querystring

  • Page 2 add this to your second page
    <asp:hiddenfield runat="server" id="hdncomp" value=""/> <asp:hiddenfield runat="server" id="hdnname" value=""/>

    Now, in your Page 2 Load event run the following code.

     Me.hdncomp = Request.QueryString("comp") Me.hdnname = Request.QueryString("name") 
  • Finally, when you go to Page 3 Use the values from hdncomp and hdnname as your parameter values. For example:

Response.Redirect("page3.aspx?comp=" & hdncomp & "&name=" & hdnname)

Untested code

well, your syntax is very wrong. you cannot write a new element before closing the first elements tag : <element <element2 /> /> . here's a function that you can use :


function createLink(address,param,text){
   address += ((address.indexOf('?') == -1) ? "?" : "&") 
      + param 
      + "="
      + qs(param);
    document.write(''+text+'');
}
and then, when you want to insert the desired link by inserting a script tag calling the "createLink" function with the desired parameters :
 <script type="text/javascript"> createLink("http://www.TEST.com/TEST/TEST/TEST.aspx?name=test","comp","CLICK HERE"); <script/> 

Hope this helps!

ps : you should probably use only one semicolon and not two at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM