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 10 of 10
  1. #1
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing data to HTML

    I have this code that seems to work when I want to pass a URL or other type of data to HTML. I also found some examples on Huntingground (see http://www.huntingground.freeserve.c...n/mainfram.htm) that explain how to do this. How do I go about parsing a SECOND (or more) value? I would like to pass more than just the one string.

    For example, how do I parse this string:
    http://www.mywebsite.com/testing?id=...n=confirm.html

    var urlstring = location.href;
    var qrystring = urlstring.substring(urlstring.indexOf("=") + 1, urlstring.length);
    document.write(qrystring);


    <script type="text/javascript">
    <!--
    var dataPassed = '';
    if (location.search.length > 0)
    dataPassed = unescape(location.search.substring(1));
    document.write(dataPassed);
    //-->
    </script>


    Thanks,
    -tdavis

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Tony Davis
    I have this code that seems to work when I want to pass a URL or other type of data to HTML. I also found some examples on Huntingground (see http://www.huntingground.freeserve.c...n/mainfram.htm) that explain how to do this. How do I go about parsing a SECOND (or more) value? I would like to pass more than just the one string.

    For example, how do I parse this string:
    http://www.mywebsite.com/testing?id=...n=confirm.html

    var urlstring = location.href;
    var qrystring = urlstring.substring(urlstring.indexOf("=") + 1, urlstring.length);
    document.write(qrystring);


    <script type="text/javascript">
    <!--
    var dataPassed = '';
    if (location.search.length > 0)
    dataPassed = unescape(location.search.substring(1));
    document.write(dataPassed);
    //-->
    </script>


    Thanks,
    -tdavis
    i'm not really sure what your asking for here?

    do you need a function to simply parse a url string to pull out the variables?

    i.e

    parsing

    myDomain.com/?variable1=test&variable2=1234

    you want to return

    variable1 = test
    variable2 = 1234

    in an array i assume?? maybe just as variables??

    so like

    var rs1 = test;
    var rs2 = 1234;
    *not that clean - you will obviously have to store it into the variable.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes. that is what i am trying to do, parse the string:
    here is the code I am using now. It works, but only parses the first parm:

    as in: E:\slideshow\index.htm?id=gila&count=22

    alert(GetParam("id")); //this shows gila&count=22
    alert(GetParam("count")); //this is blank


    <SCRIPT LANGUAGE="Javascript">
    function GetParam(name)
    {
    var start=location.search.indexOf("?"+name+"=");
    if (start<0) start=location.search.indexOf("&"+name+"=");
    if (start<0) return '';
    start += name.length+2;
    var end=location.search.indexOf("&",start)-1;
    if (end<0) end=location.search.length;
    var result=location.search.substring(start,end);
    var result='';
    for(var i=start;i<=end;i++) {
    var c=location.search.charAt(i);
    result=result+(c=='+'?' ':c);
    }
    return unescape(result);
    }
    </SCRIPT>

  • #4
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <script>
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0]==variable) document.write(variable + ' = ' + pair[1]);
        }
      }
    }
    </script>
    not tested
    Last edited by Nischumacher; 01-11-2006 at 08:15 PM.
    - NS 666
    .net DEVILoper

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,505
    Thanks
    3
    Thanked 501 Times in 488 Posts
    may be missing something

    but
    I would normally

    myDomain.com/?est&234


    var variable1,variable2,variable3;
    var data=self.location.toString().split('?');

    if (data[1]){

    variable1=data[1].split('&')[0];
    variable2=data[1].split('&')[1];
    // or
    variable3=data[1];
    }

  • #6
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there are numerous examples in the Post a Javascript form.

    here is a good one posted by another user in said form

    Code:
    function urlDecode(s)
    {
        return unescape(s.replace("+", " "));
    }
    
    function parseQueryString()
    {
        var obj = new Object();
    
        var nvpairs = location.search.substring(1).split("&");
    
        for (var idx = 0; idx < nvpairs.length; idx++)
        {
            var tokens = nvpairs[idx].split("=");
    
            obj[urlDecode(tokens[0])] = tokens.length == 2 ?
                urlDecode(tokens[1]) : undefined;
        }
        
        return obj;
    }
    however if a server side lang is avail to do this i suggest using it.

    for example: here is it in php

    http://codingforums.com/showthread.php?t=72151
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #7
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by vwphillips
    myDomain.com/?est&234
    var variable1,variable2,variable3;
    var data=self.location.toString().split('?');
    if (data[1]){
    variable1=data[1].split('&')[0];
    variable2=data[1].split('&')[1];
    // or
    variable3=data[1];
    }
    this is a better method...

    TONY : you got to use "E:\slideshow\index.htm?gila&22"
    Code:
    var id, count;
    var data=self.location.toString().split('?');
    if (data[1]){
    id=data[1].split('&')[0];  // will assign gila
    count=data[1].split('&')[1];// will assign 22
    - NS 666
    .net DEVILoper

  • #8
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I get the same results.

    And dont i have to have the = signs?
    The first split takes the string to the end, doesnt it?

  • #9
    New Coder
    Join Date
    Aug 2003
    Location
    Atlanta, Georgia
    Posts
    75
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found the problem. But I cant believe it! I created an HTML link as follows, and now it works. I originally included the path to the script (as in the first example) then when I removed that (as in the second) it works. Why?

    <a href="e:\slideshow\index.htm?id=gila&count=22">Test</a>

    <a href="index.htm?id=gila&count=22">Test</a>

  • #10
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    doesn't like the FS??

    i have had problems trying to run some scripts on local machine.

    maybe it only likes relative paths?
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<


  •  

    Posting Permissions

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