Results 1 to 13 of 13

Thread: [RESOLVED FINALLY!] Grab param value

  1. #1

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Resolved [RESOLVED FINALLY!] Grab param value

    I'm using javascript along with asp.net and vb.net and have some questions.

    I have a page that loads with parameters in the URL. Looks something like this:

    http://www.page.com/page.aspx?tokenID=133

    There's a dropdown list in my page, and when the value is changed I have javascript that automatically reloads the page and creates another parameter in the url using the value of the selected dropdownlist. Problem is it removes my tokenID paramter with this new one (ProspectID), but i need them both in there. Here's my javascript:

    Code:
    var pval = document.forms["_ctl0"].ProspectHeader_ProspectsMainListBox1;
    
    var javaid = pval.options[pval.selectedIndex].value;
    window.location.href ="http://www.page.com/ProspectMenu.aspx?ProspectID=" + javaid;
    I know I need to grab the value of my tokenid parameter and add it to my window.location.href but I'm not sure how to do that.

    Thanks!
    Last edited by drpcken; Mar 23rd, 2005 at 04:41 PM.

  2. #2

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    Ahhh figured it out myself thanks to a previous post! Thanks. Here's my solution incase someone needs it or is interested..


    Code:
    var pval = document.forms["_ctl0"].ProspectHeader_ProspectsMainListBox1;
    
    var javaid = pval.options[pval.selectedIndex].value;
    window.location.href =window.location + "&ProspectID=" + javaid;

  3. #3

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    Er well I thought I had it working. Turns out if i select a value from the drop down list once its fine, but when I select another, it takes the existing parameter and leaves it in there, and puts another paramter with the same name at the end of it...

    i guess my question is how can I remove or change a paramter using javascript from the url?

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Grab param value

    I would try something like this (I'm writing this on the fly without testing so can't swear it will work "as is")

    Code:
    var pval = document.forms["_ctl0"].ProspectHeader_ProspectsMainListBox1;
    var javaid = pval.options[pval.selectedIndex].value;
    var winloc = window.location;
    var x = winloc.indexOf("&ProspectID=")
    if (x > 0) {
    winloc = winlock.substring(0, x)
    }
    window.location.href = winloc + "&ProspectID=" + javaid;

  5. #5

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    hmm didn't quite work but thanks. Let me explain the prob a bit more.

    its works great if i only change the drop down list once, it grabs the url and adds a paramter to it based on the value of the drop down list. But if I change the drop down list again, it grabs the url WITH the parameter already in it, and add another parameter with the same name but a different value. I'd like to see if parameter is already there, or even better, remove the parameter (if it exists) if its already in the url, then add the updated one. Here's my code so far:

    Code:
    var pval = document.forms["_ctl0"].ProspectHeader_ProspectsMainListBox1;
    var javaid = pval.options[pval.selectedindex].value;
    
    window.location.href = window.location + "&ProspectID=" + javaid;
    Like i said this works great if they only changed the dropdownlist once,but unfortunately that isn't the case.

    Thanks!

  6. #6
    Fanatic Member
    Join Date
    Jan 2005
    Location
    In front of this pc.
    Posts
    580

    Re: Grab param value

    Ya, that's why I was trying to do spot a prexisting prospectId...maybe you can do a replace on it instead of what I was trying to do...or then again, maybe the method I tried is the correct one but I implemented it incorrectly - like I said, I just shot that out and didn't test it any.

    You might try some searching at http://javascript.internet.com and see if they have any samples doing something similar to what you're trying to do or look at the javascript documentation at http://developer.netscape.com

  7. #7

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    heres what I'm thinking...

    grab the url and remove the param if exists
    then add url back in address bar with new param at end of it.

    that way everytime it'll look for the existing param.

    but i don't know the syntax for removing a param...

  8. #8

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    Hey guys, I'm still trying to solve this small issue. I'll see If i can make it a bit clearer.

    A user opens a page and this page has a drop down list. When the user changes the value of the drop down list, the page is refreshed and a parameter is added to the URL (ProspectID=).

    This works great until the user decides to pick another value from the drop down list after already selecting one. When the user does this, it takes the full url WITH the parameter, and adds another parameter with the same name, but the newly picked value at the end. For example, the url looks like this after they pick a value ONCE from the dropdown:
    www.url.com/page.html?ProspectID=133

    This is perfect until they pick another value, then the url looks like this:
    /page.html?ProspectID=133&ProspectID=134

    this totally confuses my page. So if the parameter already exists, how do I tell it to just change it? Also there will be times when there are many different parameters in this url, and the values need to be retained. Here's my code I'm working with:

    Code:
    var pval = document.forms["_ctl0"].ProspectHeader_ProspectsMainListBox1;
    var javaid = pval.options[pval.selectedindex].value;
    
    window.location.href = window.location + "&ProspectID=" + javaid;
    I've looked all over for a way to change the param

    Thanks again guys!

  9. #9
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602

    Re: Grab param value

    Try setting the variable javaid = "" before assigning it a new value.
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  10. #10

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    Thats a great suggestion, but the window.href.location still contains the &ProspectID parameter already when the next value is selected from the dropdownlist.

    So the first time they select a value from the DDL, the window.location.href = url/page.html?param1=14 and it adds the value of javaid (&ProspectID=133) to the end of it.
    When the next value is selected it and the window.location.href is called again, it already has the &ProspectID in it and adds another...

    I guess the easiest way would be to remove the existing param before adding another. I will try to read up on how to do this, but any suggestions would be great!

    Thanks again!

  11. #11
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602

    Re: Grab param value

    Try this...

    HTML Code:
    <script>
    var whole_link = window.location.href;
    var question_mark = whole_link.indexOf('?');
    var page_link = whole_link.substring(0, question_mark);
    
    window.location.href = page_link + "?ProspectID=" + javaid;
    </script>
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  12. #12

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    hey thats almost working! except i have a paramter thats not being retained thats in the url to begin with, yes, a different parameter.

    Its right after the ? and is the first parameter. so here's how it goes:

    url BEFORE i pick a new value from DDL:
    url.com/page.html?tokenid=14

    url AFTER I pick a new value from DDL
    url.com/page.html&prospectid=133

    Its removing the tokenid param and not throwing my new param on the end.

  13. #13

    Thread Starter
    Fanatic Member drpcken's Avatar
    Join Date
    Apr 2004
    Location
    devenv
    Posts
    591

    Re: Grab param value

    Ok I pretty much took care of this. I found this function online that pulls whichever parameter you specify out of the url and returns its value (very useful, you guys on here might can use it.) Then once it returns that value it builds the url and adds any existing parameters to it, then adds the value from the dropdown list to the parameter I wanted. Works beautifully now. Here's the function that pulls parameters out of the url:

    Code:
    function getURLParam(strParamName){
    
      var strReturn = "";
      var strHref = window.location.href;
      if ( strHref.indexOf("?") > -1 ){
        var strQueryString = strHref.substr(strHref.indexOf("?"));//I commented this out, caused problems with case for my parameter, just uncomment if you wanna use it  // //.toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
          if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
            var aParam = aQueryString[iParam].split("=");
            strReturn = aParam[1];
            break;
          }
        }
      }
      return strReturn;
    }
    So if you had a parameter in your URL named "id" that you wanted to get the value from, do so with getURLParam("id")

    Also here's my code that fires when the dropdownlist is changed. Thank all you guys for your help!

    Code:
         var whole_link = window.location.href;
         var question_mark = whole_link.indexOf('?');
         var page_link = whole_link.substring(0, question_mark);
    
         var token = "?TokenID=" + getURLParam("TokenID");
    
         var javaid;
         var pval = document.forms["_ctl0"].ProspectHeader_ProspectsMainListBox1;
    
         javaid = "";
         javaid = "&ProspectID=" + pval.options[pval.selectedIndex].value;
    
         window.location.href = page_link + token + javaid;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width