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]
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
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
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]
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?
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
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.
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:
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]
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