Query String from src

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dstefani

    Query String from src

    Hello,

    I was wondering if you can get the info from the query string in a
    server-side javascript tag?

    Here's what I'm trying to do


    In the head of page A
    ....
    <script language="JavaS cript" src="script.js? id=test</script>
    ....


    And in my script.js file
    ....
    getArgs();

    function getArgs() {
    var args = new Object();
    var query = location.search .substring(1);
    var pairs = query.split('&' );
    for(var i = 0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('='); "name=value ".
    if (pos == -1) continue;.
    var argname = pairs[i].substring(0,po s);
    var value = pairs[i].substring(pos+ 1);
    args[argname] = unescape(value) ;
    }
    alert (args.id);
    }
    ....

    I get the alert, but is displays "undefined" .

    Can this be done?

    Thanks,

    dstefani
  • Andrew Thompson

    #2
    Re: Query String from src

    On Sat, 15 May 2004 12:06:50 GMT, dstefani wrote:
    [color=blue]
    > I was wondering if you can get the info from the query string in a
    > server-side javascript tag?[/color]

    Yes. (*server-side*?? No.
    But I don'y think you mean that)
    [color=blue]
    > Here's what I'm trying to do[/color]

    Here is a (bad) example with some good
    links to threads with tips right here..
    <http://www.physci.org/test/003url/index.html?url= http://mybiz.com/&when=now>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology

    Comment

    • dstefani

      #3
      Re: Query String from src

      I think I need to clarify...

      I would like to know if this is possible...
      Sending a query string from this tag:

      <script language="JavaS cript" src="script.js? id=test"</script>

      Can the src script recognize the query string?
      I can do it perfectly with an <a href="page.html ?id=test">, but I can't
      get it to work as noted in the src="script.js? id=test"


      Here is whats in my js script, it works fine for a link.

      Thanks,

      dstefani



      getArgs();

      function getArgs() {
      var args = new Object();
      var query = location.search .substring(1);
      var pairs = query.split('&' );
      for(var i = 0; i < pairs.length; i++) {
      var pos = pairs[i].indexOf('=');
      if (pos == -1) continue;
      var argname = pairs[i].substring(0,po s);
      var value = pairs[i].substring(pos+ 1);
      args[argname] = unescape(value) ;property.
      }
      alert (args.id);
      }

      Comment

      • Andrew Thompson

        #4
        Re: Query String from src

        On Sat, 15 May 2004 14:52:58 GMT, dstefani wrote:
        [color=blue]
        > I think I need to clarify...[/color]

        Spot on there. ;-)
        [color=blue]
        > I would like to know if this is possible...
        > Sending a query string from this tag:
        >
        > <script language="JavaS cript" src="script.js? id=test"</script>[/color]

        Firstly, it is missing a '>' character..
        <script language="JavaS cript" src="script.js? id=test"></script>
        [color=blue]
        > Can the src script recognize the query string?[/color]

        OK, now...

        What is it that you expect the script.js
        file to *do* with the id=test?

        If it requires the information, it can get
        it from the URL (that your normal link
        points to) as shown in the example I pointed to.

        ** But no, I do not believe there is a way
        to hand information to a .js file as you
        describe. **

        Which leads us to an important question.
        What are you actually trying to achieve?

        --
        Andrew Thompson
        http://www.PhySci.org/ Open-source software suite
        http://www.PhySci.org/codes/ Web & IT Help
        http://www.1point1C.org/ Science & Technology

        Comment

        • dstefani

          #5
          Re: Query String from src

          Andrew Thompson wrote:[color=blue]
          > On Sat, 15 May 2004 14:52:58 GMT, dstefani wrote:[/color]
          [color=blue]
          > Firstly, it is missing a '>' character..
          > <script language="JavaS cript" src="script.js? id=test"></script>[/color]
          [color=blue]
          > Which leads us to an important question.
          > What are you actually trying to achieve?[/color]

          Thanks for your reply and time.

          I am trying to track visitor movement through a site.

          1/ use script.js to set a cookie
          2/ grab the querystring in the afore mentioned tag to know which site it
          is. I guess I could have a lookup table that checked HTTP headers for
          the location and mapped it to an account, but it would be easy if I
          could get it from that tag.

          -dstefani

          Comment

          • Lee

            #6
            Re: Query String from src

            dstefani said:[color=blue]
            >
            >Hello,
            >
            >I was wondering if you can get the info from the query string in a
            >server-side javascript tag?
            >
            >Here's what I'm trying to do
            >
            >
            >In the head of page A
            >...
            ><script language="JavaS cript" src="script.js? id=test</script>
            >...
            >
            >
            >And in my script.js file
            >...
            >getArgs();
            >
            >function getArgs() {
            > var args = new Object();
            > var query = location.search .substring(1);[/color]


            You seem to misunderstand how that script block is loaded
            and executed. There is nothing "server-side" about it.
            The code is loaded into the current HTML page and executes
            in that context, which means that location.search will
            always refer to the URL of the current HTML page.

            Comment

            • dstefani

              #7
              Re: Query String from src

              Lee wrote:
              [color=blue][color=green]
              >>And in my script.js file
              >>...
              >>getArgs();
              >>
              >>function getArgs() {
              >> var args = new Object();
              >> var query = location.search .substring(1);[/color]
              >
              >
              >
              > You seem to misunderstand how that script block is loaded
              > and executed. There is nothing "server-side" about it.
              > The code is loaded into the current HTML page and executes
              > in that context, which means that location.search will
              > always refer to the URL of the current HTML page.
              >[/color]

              I knew my stupidity would come into play soon.
              Can I change my function to recognize the query string in the scr=""
              parameter?

              - dstefani

              Comment

              • Ivo

                #8
                Re: Query String from src

                "dstefani" wrote[color=blue]
                > Andrew Thompson wrote:[color=green]
                > > On Sat, 15 May 2004 14:52:58 GMT, dstefani wrote:[/color]
                >[color=green]
                > > Firstly, it is missing a '>' character..
                > > <script language="JavaS cript" src="script.js? id=test"></script>[/color][/color]

                Secondly, the language attribute should be dropped and a type of
                text/javascript be specified, so the resulting tag would be:

                <script type="text/javascript" src="script.js? id=test"></script>

                The location object is about the location of the page containing the script.
                I don't think I 'm saying anything new. The source of the script itself is
                document.script s[n].src in IE4+ and
                document.getEle mentsByTagName( 'script')[n].src in some other browsers where
                n is the index of the script. This is a string which can be parsed for
                questions marks and equal signs.

                The extension to the source file name may be .php or .asp or anything as
                long as the file itself contains javascript and the server sends the
                appropiate headers.

                Sadly, a running script does not know from which file it came if there more
                than one .js file in a page.
                HTH
                Ivo


                Comment

                • Lee

                  #9
                  Re: Query String from src

                  dstefani said:[color=blue]
                  >
                  >Lee wrote:
                  >[color=green][color=darkred]
                  >>>And in my script.js file
                  >>>...
                  >>>getArgs();
                  >>>
                  >>>function getArgs() {
                  >>> var args = new Object();
                  >>> var query = location.search .substring(1);[/color]
                  >>
                  >>
                  >>
                  >> You seem to misunderstand how that script block is loaded
                  >> and executed. There is nothing "server-side" about it.
                  >> The code is loaded into the current HTML page and executes
                  >> in that context, which means that location.search will
                  >> always refer to the URL of the current HTML page.
                  >>[/color]
                  >
                  >I knew my stupidity would come into play soon.
                  >Can I change my function to recognize the query string in the scr=""
                  >parameter?[/color]

                  That's just ignorance, not stupidity.
                  The fact that the code is executed in the context of the current
                  HTML page probably means that there's an easier way to do what
                  you're trying to accomplish. For example:

                  <script type="text/javascript">id= test</script>
                  <script type="text/javascript" src="script.js</script>


                  The value of id will be available to the code within script.js

                  Comment

                  Working...