HTTP header insertion using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spsmn
    New Member
    • Jul 2008
    • 3

    HTTP header insertion using javascript

    I would like to use javascript (or something similar) in my page in order to make the browser send a specific HTTP header (let's say the header 'X-Try:' with some value). I would like to do it on every subsequent request to the same domain (The domain that originally sent the javascript to the client).
    Can I do such a thing? and if yes - how exactly ?
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    are you talking about a request header, or a response header ?

    you can set request headers in ajax.

    you can mimic response headers with meta tags, splicing them into the response and then doc.writing the output, but that's about as good as you can do with just javascript.

    Comment

    • spsmn
      New Member
      • Jul 2008
      • 3

      #3
      Originally posted by rnd me
      are you talking about a request header, or a response header ?

      you can set request headers in ajax.

      you can mimic response headers with meta tags, splicing them into the response and then doc.writing the output, but that's about as good as you can do with just javascript.
      I'm talking about injecting request HTTP headers in all subsequent requests that are destined to the same domain as the domain that initially sent that javascript.
      I want to embed in my javascript (the one I sent as a response for request#1) a piece of code that will add a specific HTTP header in all the next requests originated from that javascript page.
      I prefer using javascript to do that...
      but, if there's a way to do that in ajax - can someone elaborate on that ?

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by spsmn
        I'm talking about injecting request HTTP headers in all subsequent requests that are destined to the same domain as the domain that initially sent that javascript.
        I want to embed in my javascript (the one I sent as a response for request#1) a piece of code that will add a specific HTTP header in all the next requests originated from that javascript page.
        I prefer using javascript to do that...
        but, if there's a way to do that in ajax - can someone elaborate on that ?
        What type of HTTP header you want to add?
        And how to add HTTP header before Ajax call, go for Google.

        Comment

        • spsmn
          New Member
          • Jul 2008
          • 3

          #5
          Originally posted by dmjpro
          What type of HTTP header you want to add?
          And how to add HTTP header before Ajax call, go for Google.
          I would like to add the HTTP header "X-Try: blabla" inside each subsequent request .
          I would like it to be placed somewhere inside the HTTP header of a regular request coming out of the client's browser

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use the setRequestHeade r method of the XMLHttpRequest object.

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              you can use ajax, and a fullscreen iframe to do it.


              lets say you had an iframe (id="iframe1") styled to take up 100% of the screen: no body margin or padding:








              Code:
              function el(tid) {return document.getElementById(tid);}
              
              function IO(U, V) {//customized for post...
              //LA MOD String Version. A tiny ajax library.  by DanDavis
                  var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
                  X.open(V ? 'PUT' : 'GET', U, !1);
                  X.setRequestHeader('Content-Type', 'text/html')
              
                  X.setRequestHeader('X-Try', "blabla");
              
                  X.send(V ? V : '');
              return X.responseText;}
              
              var doc = el("iframe1").contentWindow.document
              doc.write( IO("page1.htm"));
              doc.close()

              Comment

              Working...