Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

HTTP header insertion using javascript

Question posted by: spsmn (Newbie) on July 19th, 2008 10:25 PM
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 ?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
rnd me's Avatar
rnd me
Expert
242 Posts
July 20th, 2008
07:04 AM
#2

Re: HTTP header insertion using javascript
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.

Reply
spsmn's Avatar
spsmn
Newbie
3 Posts
July 20th, 2008
10:49 AM
#3

Re: HTTP header insertion using javascript
Quote:
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 ?

Reply
dmjpro's Avatar
dmjpro
Lives Here
2,079 Posts
July 20th, 2008
01:38 PM
#4

Re: HTTP header insertion using javascript
Quote:
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.

Reply
spsmn's Avatar
spsmn
Newbie
3 Posts
July 20th, 2008
04:35 PM
#5

Re: HTTP header insertion using javascript
Quote:
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

Reply
acoder's Avatar
acoder
Site Moderator
11,000 Posts
July 20th, 2008
05:12 PM
#6

Re: HTTP header insertion using javascript
Use the setRequestHeader method of the XMLHttpRequest object.

Reply
rnd me's Avatar
rnd me
Expert
242 Posts
July 20th, 2008
07:54 PM
#7

Re: HTTP header insertion using javascript
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: ( text )
  1. function el(tid) {return document.getElementById(tid);}
  2.  
  3. function IO(U, V) {//customized for post...
  4. //LA MOD String Version. A tiny ajax library.  by DanDavis
  5.     var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
  6.     X.open(V ? 'PUT' : 'GET', U, !1);
  7.     X.setRequestHeader('Content-Type', 'text/html')
  8.  
  9.     X.setRequestHeader('X-Try', "blabla");
  10.  
  11.     X.send(V ? V : '');
  12. return X.responseText;}
  13.  
  14. var doc = el("iframe1").contentWindow.document
  15. doc.write( IO("page1.htm"));
  16. doc.close()

Reply
Reply
Not the answer you were looking for? Post your question . . .
184,215 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Javascript / DHTML / Ajax Forum Contributors