Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Get text out of location

    I would like to get the string "palli" out the webpages location:
    "webpage.com/?i=palli&w=stebbi&re=dagsetning&rada=012"
    and then write it into the webpage. This would then be repeated with other strings. Any ideas?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,015
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    var temp = "webpage.com/?i=palli&w=stebbi&re=dagsetning&rada=012";
    temp = temp.replace(/palli/i,"");
    alert (temp);

  • #3
    New Coder
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the thing is. palli was just an example. I'm runnig script and I need to get just the i= field in the location into the template file? I hope I'm explaining myself well enough.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I think marringi wants to get the string "palli" from the url, not out of the url.
    This could be easily done using a server-side language.

  • #5
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tossed together this example for one way it can be done. It's not optimized by any means but hopefully can give you some ideas.

    Code:
    <html>
      <head>
        <title>Parse Query String</title>
        <script type="text/javascript">
          function doIt() {
            var s = "";
            var adiv = document.getElementById("adiv");
            var q = document.getElementById("q").value;
            var v = document.getElementById("v").value;
            var re = /\?(.*)$/;
            var qstr = null;
            var result = re.exec(q);
    
            if (result && result[1]) {
              qstr = result[1];
            }
            
            if (qstr) {
              var pairArray = qstr.split("&");
              
              for (var i = 0; i < pairArray.length; i++) {
                var keyValArray = pairArray[i].split("=");
                if (v == keyValArray[0]) {
                  s = keyValArray[1];
                  break;
                }
              }
            }
    
            adiv.innerHTML = s;
          }
        </script>
      </head>
      <body>
        <div>
          <label>URL <input type="text" id="q" size="100" value="webpage.com/?i=palli&w=stebbi&re=dagsetning&rada=012"/></label>
          <br/>
          <label>Value of <input type="text" id="v" size="100" value="i"/></label>
          <br/>
          <button onclick="doIt();">Do it</button>
          <br />
          <div id="adiv"> </div>
        </div>
      </body>
    </html>
    david_kw

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I think the url is the current url of the page. And as I said, if there is a server-side language used, this could be done much easier than in javascript.

    If server-side language is PHP:
    PHP Code:
    <?php echo $_GET["i"]; ?>
    If VBScript (ASP):
    Code:
    <%=Request.QueryString("i")%>

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If it is the current url then I believe you can replace the qstr stuff with

    var qstr = location.search.substring(1);

    david_kw

  • #8
    New Coder
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is a CGI/Perl script. I'm displaying result from a database on template pages. Do you have the code for that?

  • #9
    New Coder
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you want to grab the name of the DB from the URL string, then it is possible in theory with js, but I think it would take some thinking to nail down the substring.

    Something along the lines a creating a dummy <span> tag with an id, then within the span tag use document.write to write the location url into the span tag, then using the getElementById it might be possible to use substr(value1,value2) to identify the mydatabase call in the url. Might get messy, but possible.

    Is this something that would take a lot of time and energy?

  • #10
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    as someone above stated, it would be alot easier to do it with a server side script, but if you really want a javascript version, I have posted a response to a similar question here: How do I do this ?
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • #11
    New Coder
    Join Date
    Apr 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the thing is I don't know how I would use server-side language fix this problem. He talk's about PHP and ASP and I'm using Perl.

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I don't know CGI/Perl but here's what I found.

    Code:
    my $param_i = $cgi->param('i');
    More info here.

  • #13
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To use that you'd have to

    use CGI;

    to get the module loaded.

    david_kw


  •  

    Posting Permissions

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