Forum Moderators: open

Message Too Old, No Replies

Ajax Javascript Safari onKeyDown request status undefined bug - help!

Ajax Javascript Safari onKeyDown request status undefined bug - help!

         

rcain

3:32 pm on Oct 5, 2006 (gmt 0)

10+ Year Member


Hi
I'm new here, but I've read a lot of posts from peple who seem to know what theyre talking about, so I really hope someone can help me out with this bug that's been doing my head in:

I am performing a remote login via post form and before i send the form I use Ajax to first retrieve the target host url from an .htusers.php file on the server, then fill in the action attribute of the form, then submit it to the remote host. The code is initiated either when the user clicks the enter button or when they hit the enter key on the form. (Also, focus is set on the input fields when the page loads).

It works fine on all browsers - IE, FF, Safari (V2.0.4) for mac & PC, with one exception: on Safari Mac the onKeyDown method consistently causes 'Ajax request send' to return status undefined and an empty responseText, whereas the click button call works perfectly!

Can anyone help explain whats happenning in Safari to make it fail and importantly, how I can fix it? My greatest thanks for any help offerd, I've run out of options/clues.

Here is the code I'm using (complete with all the possible workarounds/clues I have managed to find to far :[smilestopper]) ):

The html form:
##################################################################
<html>
<head> ...etc here...
</head>

<body>
<script language="javascript" type="text/javascript" src="sniffer2.js"></script>

<script language="javascript" type="text/javascript" src="scl_ajax_remote.js"></script>

<form class="clientloginform" name="login" id="login" action="loginfailed.php" enctype="multipart/form-data" method="POST">

<h1>CLIENT LOGIN</h1>
<p>Please enter your username and password below</p>
<p><label accesskey=U>USERNAME<input type=text id="p_user" name="p_user" onKeyDown="javascript: if((13 == event.keyCode) ¦¦ (13 == event.which)) {dologin();}"></input></label></p>
<p><label accesskey=P>PASSWORD<input type=password id="p_pass" name="p_pass" onKeyDown="javascript:if((13 == event.keyCode) ¦¦ (13 == event.which)) {dologin()};"></input></label></p>

<div id="enterbutton"><a id="enter" class="button" href="javascript: login();">ENTER</a></div>

<SCRIPT language="JavaScript">document.getElementById('p_user').focus();</SCRIPT>
</form>

</body>
</html>

##################################################################

.. and the javascript in scl_ajax_remote.js:
##################################################################

var urlroot="http://systemcore.net/myaccount/";
var url = "";

var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP"[smilestopper]);
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP"[smilestopper]);
} catch (failed) {
request = false;
}
}
}

if (!request) {alert("Error initializing XMLHttpRequest!"[smilestopper])};

function updatePage() {

//mod jrc 051006 - possible work around for safari heisen bug - not!
var agt=navigator.userAgent.toLowerCase();
var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;

if (request.readyState == 4) {
//mod jrc 041009 - 203 and undefined added as possible workaround for safari heisenbug - not!
if (request.status == 200 ¦¦ request.status == 203 ¦¦ (is_safari && (typeof(request.status) == 'undefined'))) {

var response = request.responseText.split("¦"[smilestopper]);
//parse my Ajax php return string....
if (response[1] && response[1]!= "" && response[1]!= "SCL_NO_SERVER"[smilestopper]) {

var server_url = "http://"+response[1]+"/index.php?action=login&order=name&srt=yes";
alert ("serevrurl="+server_url);

document.getElementById('login').setAttribute("action", server_url);

document.getElementById('login').submit();

} else {

window.location = "index.php?page=loginfailed&style=3";
};
} else
alert("status is " + request.status);
};
};

function login() {

if (document.getElementById("p_user"[smilestopper]) && document.getElementById("p_pass"[smilestopper])) {
getRemote_Host_Post(document.getElementById('p_user'));
};
}

//note - required to attach onkeydown event to func
function dologin() {alert('hi aa'); login();};

function getRemote_Host_Post() {
url = "scl_get_remote.php?action=remote&user="+escape(document.getElementById('p_user').value);

request.open("GET", url, true); //make this a synchronous request

request.onreadystatechange = updatePage;

//mod jrc 051006 - possble fix for safari heisenbug - not!
request.setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');

request.send(null);
};

##################################################################

the php side of the ajax call invokes a function returning a simple non-empty string, ie:

echo "<SERVER>" . "¦" . $remote_url . "¦" . "</SERVER>";

which works fine (problem seems to be instigated on the initial call to request.send under the keydown event in safari - although i may be mistaken).

penders

3:45 pm on Oct 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi rcain, not sure if this your problem... but there have been a few posts regarding the problem of key events firing twice in Safari...
[webmasterworld.com...]

rcain

6:10 pm on Oct 5, 2006 (gmt 0)

10+ Year Member



Hi Penders,
thanks for the reply...but guese what, I've just fixed it:

turns out that safari onKeyDownEvent misses off the last character entered in the buffer. I changed the html call to onKeyUp='javascript:...' - and it now works fine.

of couse this still doesnt explain the strange ajax status undefined return (since even with a bad string sent to it my ajax server code still returns valid data). so, some strange effect between onKeyDown and Ajax send request is still going on in Safari (cache?) somewhere, though i really cant understand what.

hope this solution helps someone else out anyway.