Obtaining query_string from JavaScript

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

    Obtaining query_string from JavaScript

    Hi All,

    Let's say we have site index.html with JS embedded there in this
    way:

    <script type="text/javascript" src="my_script. js?username=mr_ bean"></
    script>

    Question:
    How to get access to "username" query string constant directly from
    my_script.js file ? "location.searc h" is no option here because it
    will access index.html's QUERY STRING not my_script.js's. Any idea ?

    Best regards,
    Marcin Zduniak
  • SAM

    #2
    Re: Obtaining query_string from JavaScript

    Le 9/24/08 5:15 PM, Marcin a écrit :
    Hi All,
    >
    Let's say we have site index.html with JS embedded there in this
    way:
    >
    <script type="text/javascript" src="my_script. js?username=mr_ bean"></
    script>
    funny idea, no ?
    Question:
    How to get access to "username" query string constant directly from
    my_script.js file ? "location.searc h" is no option here because it
    will access index.html's QUERY STRING not my_script.js's. Any idea ?
    var S = document.getEle mentsByTagName( 'script');
    for(var i=0, n=S.length; i<n; i++)
    if(S[i].src.toString() .indexOf('my_sc ript') >=0) {
    alert('var = '+S[i].src.toString() .split('=')[1]);
    break;
    }


    --
    sm

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Obtaining query_string from JavaScript

      Marcin wrote:
      Let's say we have site index.html with JS embedded there in this
      way:
      >
      <script type="text/javascript" src="my_script. js?username=mr_ bean"></
      script>
      >
      Question:p
      How to get access to "username" query string constant directly from
      my_script.js file ? "location.searc h" is no option here because it
      will access index.html's QUERY STRING not my_script.js's. Any idea ?
      The script cannot "know" how it is included. However, you can use a
      server-side script to generate a client-side one, and the former can
      read the query part of the request URI.

      For example, in my_script.js.ph p (I would use Content Negotiation to get
      rid of the security-relevant `.php' in the URI):

      var username = "<?php echo addslashes($_RE QUEST['username']); ?>";


      PointedEars
      --
      Prototype.js was written by people who don't know javascript for people
      who don't know javascript. People who don't know javascript are not
      the best source of advice on designing systems that use javascript.
      -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

      Comment

      • Conrad Lender

        #4
        Re: Obtaining query_string from JavaScript

        On 2008-09-26 20:24, Thomas 'PointedEars' Lahn wrote:
        For example, in my_script.js.ph p (I would use Content Negotiation to get
        rid of the security-relevant `.php' in the URI):
        I wouldn't. I suppose content negotiation could also be (mis)used to
        hide an extension, but it was really intended to serve alternate
        representations of resources depending on the UA's capabilities and
        preferences (different languages or media types). See RFC 2616.
        URL rewriting would be more appropriate to hide or change file names.

        - Conrad

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Obtaining query_string from JavaScript

          Conrad Lender wrote:
          On 2008-09-26 20:24, Thomas 'PointedEars' Lahn wrote:
          >For example, in my_script.js.ph p (I would use Content Negotiation to get
          >rid of the security-relevant `.php' in the URI):
          >
          I wouldn't. I suppose content negotiation could also be (mis)used to
          hide an extension, but it was really intended to serve alternate
          representations of resources depending on the UA's capabilities and
          preferences (different languages or media types). See RFC 2616.
          URL rewriting would be more appropriate to hide or change file names.
          URL rewriting cannot be reasonably applied to this problem: you would have
          to write expressions for each script file (and have PHP parse all, which is
          inefficient), or write an expression for all directories where you have to
          keep all your generated script files (which is inflexible).

          I might add that He who invented the Web and those who largely implemented
          it disagree with you about the use of content negotiation as I do:

          <http://www.w3.org/Provider/Style/URI>
          <http://httpd.apache.or g/docs/2.0/content-negotiation.htm l#naming>


          PointedEars
          --
          Prototype.js was written by people who don't know javascript for people
          who don't know javascript. People who don't know javascript are not
          the best source of advice on designing systems that use javascript.
          -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

          Comment

          Working...