Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 4.33 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-01-2007, 08:18 AM   PM User | #1
cuchandoo
New to the CF scene

 
Join Date: Aug 2007
Location: India,Tanuku,A.P
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
cuchandoo is an unknown quantity at this point
what is &sid in this programe?[url=url+"&sid="+Math.random()]

It is a ajax program
I am unable to understand why he uses this "&sid"
var url="livesearch.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
This is the reference for it,
Thank u for u r reply.
[/SIZE][/SIZE]
cuchandoo is offline   Reply With Quote
Old 08-01-2007, 08:44 AM   PM User | #2
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
Quote:
Originally Posted by cuchandoo View Post
[SIZE="3"]It is a ajax program
I am unable to understand why he uses this "&sid"
var url="livesearch.php"
It is the standard way to append a query sting to the URL

Code:
url=url+"?q="+str
url=url+"&sid="+Math.random()
  - or -
url += "?q="+str+"&sid="+Math.random()

will yeild
http://www.theurlvalue.com/livesearch.php?q=stringvalue&sid=randomnumber

Last edited by rwedge; 08-01-2007 at 09:02 AM..
rwedge is offline   Reply With Quote
Old 08-01-2007, 09:14 AM   PM User | #3
cuchandoo
New to the CF scene

 
Join Date: Aug 2007
Location: India,Tanuku,A.P
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
cuchandoo is an unknown quantity at this point
Thanks

Thank u and all the best for u r career and life
cuchandoo is offline   Reply With Quote
Old 08-02-2007, 03:36 PM   PM User | #4
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
That sId is put there to not cahce the page with IE, there are better ways to do it.

also the correct order for IE6 and below is:

...open
...onreadystatechange
...send

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-02-2007, 06:38 PM   PM User | #5
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
Quote:
Originally Posted by A1ien51 View Post
That sId is put there to not cahce the page with IE, there are better ways to do it.
It could also be a session id - sid. It could force a fresh page to load because it's unique, but it would be cached, and if ran again, the chance the random number would be the same is low, so the browser would get a fresh page and not a page from the cache.


Quote:
Originally Posted by A1ien51 View Post
also the correct order for IE6 and below is:

...open
...onreadystatechange
...send
Not according to MSDN. The onreadystate before open sets the readyState handler to 0, the open following the call to the onreadystatechange then sets readyState to 1
rwedge is offline   Reply With Quote
Old 08-02-2007, 08:14 PM   PM User | #6
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Smile

Quote:
Originally Posted by rwedge View Post
It could also be a session id - sid. It could force a fresh page to load because it's unique, but it would be cached, and if ran again, the chance the random number would be the same is low, so the browser would get a fresh page and not a page from the cache.



Not according to MSDN. The onreadystate before open sets the readyState handler to 0, the open following the call to the onreadystatechange then sets readyState to 1
1) when the code is manually setting the sid to a random number it is to be a cache buster.

2) You can quote the standards documents all you want, but in reality it does not work like this. I have do plenty of research on this stuff for my books and my job as an Ajax developer.

It is easy to test. Find your self a copy of IE6. Reuse the XMLHttpRequest object make sure to set the onreadystatechange first. I will bet you that the code will run the first time and the second time it will not work. IE6 and under does not reset the value to zero, it keeps the value at 4!

There are plenty of people that ask questions on ths forum on why their code only works one time with IE. The order matters and their problems are fixed when they do open, onreadystatechange, and send!

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-02-2007, 08:30 PM   PM User | #7
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
I have used it with IE 5.5 up, and have loaded file after file and have not seen the issue.

I do not have IE6 installed any more, but it took MS quite a while to come out with IE 7, and through my IE 6 usage, onreadystatechange has not failed to continue to get xml docs for me.

Is this 'bug' a readyState issue or is it actually from not initializing XMLHttpRequest on each request?

Last edited by rwedge; 08-03-2007 at 09:28 AM..
rwedge is offline   Reply With Quote
Old 08-03-2007, 03:52 PM   PM User | #8
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
Quote:
Originally Posted by A1ien51 View Post
1)
There are plenty of people that ask questions on ths forum on why their code only works one time with IE. The order matters and their problems are fixed when they do open, onreadystatechange, and send!
Eric

Like this thread
Quote:
Originally Posted by A1ien51 View Post
Are you reusing the Object?
The correct order with IE is
open()
onreadystatechange
send()
If you have onreadystatechange before the open, IE will not work.
Eric
glenngv had it right, you do not need to hack the open order, XMLHttpRequest was only initialized on page load, not with each request.
rwedge is offline   Reply With Quote
Old 08-03-2007, 05:27 PM   PM User | #9
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
If you load the object each time there is no issue at all with the order [well you need to have send last!] The new object reference gives you a clean slate to work on.

If you reuse the object [only load it once on a page] stuck in a global variable, that is when the order matters with the older versions of IE. I actually installed windows 98se with IE5 and IE5.5 to test this and it will fail. You should have seen my friend's faces when I was asking if anyone had windows 98 install discs!

I actually did not know what the problem was when I was trying to reuse the XHR object in IE. One of my readers of my blog showed me what the issue was! To bring up old posts on my blog:

http://radio.javaranch.com/pascarell...735438047.html

and than if you see the comments it was brought to my attention what the issue was and I posted the solution here:

http://radio.javaranch.com/pascarell...817890773.html

It is one of those browser quirks that we have to build around all the time. Just like how IE does not support obj.style.display = "table-row" or how measurements differ depending on the doc-type. That is why coding in JavaScript is so much fun!

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 08-03-2007, 09:18 PM   PM User | #10
rwedge
Regular Coder

 
Join Date: Feb 2005
Posts: 679
Thanks: 0
Thanked 16 Times in 15 Posts
rwedge is on a distinguished road
So now, with IE7 supporting the native version of XMLHTTP
If you put open after the onreadystatechange:

onreadystatechange
open
send

and try to reuse XMLHttpRequest it will not work, only runs once.
If you keep the sequence in the order:

open
onreadystatechange
send

you can reuse the object.


The easiest fix, to include IE5-IE7, is to just initiate a new XMLHttpRequest for each fetch. There is no evidence of memory leaks, and it works every time in all browsers that support XMLHttpRequest.

If you change the order to open, onreadystatechange, send , though most get to 4, readyState gives some mixed up results in all browsers.

if you want to reuse the object when you can, and don't mind a little detection :
Code:
<script type="text/javascript">
/*<![CDATA[*/
var xmlhttp, isOldIE = (document.all && !window.opera)? true:false;
function setReqOldIE() {
 if (window.ActiveXObject) {
  try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch(e) {
       try {   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch(e) {
          xmlhttp = false;
        }
    }
 }
return xmlhttp;
}
function ckState() {
     if (xmlhttp.readyState==4) {
        if(xmlhttp.status == 200){ alert(xmlhttp.responseText); }
        else  { alert('Error Message: '+xmlhttp.statusText); }
     }
}
if (!isOldIE) {  xmlhttp = new XMLHttpRequest(); }
function getPage(){
if (isOldIE) { xmlhttp=setReqOldIE(); }
if(xmlhttp) {
   xmlhttp.onreadystatechange = ckState;
   xmlhttp.open("GET","test.xml",true);
   xmlhttp.send(null);
 } else {
   alert(navigator.appName+' Does Not Seem To Support HttpRequest');
 }
}
/*]]>*/
</script>

<a href="javascript:getPage();">Get File</a>

Last edited by rwedge; 08-04-2007 at 11:51 PM.. Reason: add script
rwedge is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:22 PM.


Advertisement
Log in to turn off these ads.