PDA

View Full Version : Resolved IE losing Jquery Functional



bluewalrus
01-12-2011, 04:10 AM
I'm using this code


location.reload();

in the result of a jquery ajax post and it works in ff, chrome, and safari. In IE it is losing the page the reload came from though and is calling it from the root directory. Does anyone know how to correct this? I don't have the site live so I can't provide a link.

This is a the full function I have it executing at.


<script type="text/javascript">
$('#pass').keydown(function(e) {
if (e.keyCode == '13') {
$('#status').html();
$.post("login.php", {user : $("#user").val(), pass : $("#pass").val()},
function(data){
if (data == "\r\nReload" || data == "Reload") {
location.reload();
}
if (data == "\r\nLogin Failed" || data == "Login Failed") {
$('#status').html('<span style="color:#f00; font-weight:bold;">Sorry, login failed. Please try again.</span>');
}
});
}
});
</script>

Does anyone also know why the 'data' coming in from the '$.post' request has a new line in it some of the time?

Thanks.

EDIT:

Forget an example

site.com/dir/dir/page.php (so this triggers the reload)

site.com/dir/dir/ (this is what ie loads)

I need the reload so the session data is available to the php. I 'm using ajax so if the wrong user/password is entered there will be no session data, and error will be displayed.

jscheuer1
01-12-2011, 10:55 AM
Unless there's some reason not to, I'd try:


location = 'page.php';

instead of:



location.reload();

If the name of the page might vary, use PHP to supply it.

bluewalrus
01-12-2011, 06:32 PM
Thanks, that worked for the login click and enter key. For the logout it is not working though it still redirects to the directory.



$('#logout').live('click', function() {
$.post("login.php", {logout : true},
function(data){
if (data == "\r\nReload" || data == "Reload") {
location = 'header.php';
}
});
});

Is the "location" defined(usage?) in jquery or JS?

jscheuer1
01-12-2011, 08:03 PM
It (location) is a javascript object. It's both a setter and a getter and it means the location of the page.

Technically it's not a string though, so you might have better luck setting:


location.href = 'header.php';

But I've never seen that be a problem before when used that way as just location. More likely this condition is false:


if (data == "\r\nReload" || data == "Reload")

because, if it were true and the location directive fired, you would either get the page it's asking for or an error page if the page was not found. You wouldn't get a directory listing or an index page, which is what you seem to be saying that you're getting. What exactly are you getting?

One other possibility is that there may be some kind of redirect (javascript, PHP, meta tag, etc.) on header.php which is causing it to load whatever it is that you're getting in place of header.php.

bluewalrus
01-12-2011, 10:48 PM
I tried window.location.reload and location.href="page.php". I also tried location.href="http://www.google.com"; that didn't work in any browser. Here's a full example:



<?php
session_start();
?>
<link href="css.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$('#login').live('click', function() {
$.post("login.php", {user : $("#user").val(), pass : $("#pass").val()},
function(data){
$('#status').html();
if (data == "\r\nReload" || data == "Reload") {
location = 'header.php';
}
if (data == "\r\nLogin Failed") {
$('#status').html('<span style="color:#f00; font-weight:bold;">Sorry, login failed. Please try again.</span>');
}
});
});
$('#logout').live('click', function() {
$.post("login.php", {logout : true},
function(data){
if (data == "\r\nReload" || data == "Reload") {
location.reload();
}
});
});
</script>
<body>
<div class='header'>
<?php if(!isset($_SESSION['userid'])) {?>
<div id="status"></div>
<div class="login">
<form method="post">
<table cellpadding="0" cellspacing="0">
<tr>
<td width="200px;">&nbsp;</td>
<td width="107"><a class="top_link" href="http://www.jove.com/index/Forgot Password.stp">Forgot Password</a></td>
<td width="83"><a class="top_link" href="http://www.jove.com/index/CreateAccountF.stp">New Account</a></td>
<td width="100"><input type="text" class="input_top login_name" name="login" value="Email" id="user" /></td>
<td width="100"><input type="password" class="input_top login_pass" name="pass" id="pass" value="Password"/></td>
<td width="69"><input type="button" class="submit" value="Sign In" id="login" /></td>
</tr>
</table>
</form>
</div>
<?php } else {?>
<div class="logout" id="logout"><a href="">Log Out</a></div>
<?php } ?>
<div class="clr"></div>
</div>
<script type="text/javascript">
$('#pass').keydown(function(e) {
if (e.keyCode == '13') {
$('#status').html();
$.post("login.php", {user : $("#user").val(), pass : $("#pass").val()},
function(data){
if (data == "\r\nReload" || data == "Reload") {
location = 'header.php';
}
if (data == "\r\nLogin Failed" || data == "Login Failed") {
$('#status').html('<span style="color:#f00; font-weight:bold;">Sorry, login failed. Please try again.</span>');
}
});
}
});
</script>

The reload comes through I tested that with breakpoints. found that I needed the \n\r also because that comes through some of the time.

bluewalrus
01-12-2011, 11:03 PM
hah think i found the problem

jscheuer1
01-13-2011, 12:20 AM
And that was?

bluewalrus
01-13-2011, 02:02 AM
I was calling it with an a and had an empty href="". I replaced it with a ? and it works now.