2011-09-29 86 views
0

我有一个用户输入用户名和密码的登录表单。当他们点击一个图像按钮来提交我做一个Ajax调用来验证用户。该响应返回目标网址。现在,当我尝试做一个“window.location.href = url”的过程不适用于FireFox 7(FF),但它可以在其他浏览器IE,Chrome ...等工作window.location.href和JQuery不在FF 7中工作

这是我的jquery ajax调用。

var loginSubmit = function() { 

    if ($('#loginForm').data('isSubmitting')) return false; 

     showProgress('Logging in..'); 
     $('#loginErrors').empty(); 
     $('#login-form').fadeTo(100, 0.4); 
     $.ajax({ 
      cache: false, 
      url: '/dispatcher/user/login', 
      data: { 
       'user': $('#userName').val(), 
       'pwd': $('#password').val(), 
       'continuePath': $('#continuePath').length > 0 ? $('#continuePath').val() : '/my-account' 
      }, 
      type: 'POST', 
      success: function (data) { 
      $('#loginForm').data('isSubmitting', true); 

      console.log("Data from Controller: " + data.dest); 

      if (data.dest) { 
       var href = data.dest; 
       window.location.href = href; 
       return false; 
      } else { 
       console.log("DATA DEST FALSE"); 
       $('#loginForm').data('isSubmitting', false); 
       $('#login-form').fadeTo(100, 1); 
       showError('Invalid username/password'); 
      } 
      } 
     }); 
     return false; 
    }; 
    $('#loginForm').submit(loginSubmit); 

这是我的表格。

<form id="loginForm" action="#"> 
     <c:if test="${sessionScope['continuePath'] != null}"> 
      <input type="hidden" name="continuePath" value="${sessionScope['continuePath']}"/> 
     </c:if> 
     <div id="progressMessage" style="color: #333; font-weight: bold;"></div> 
     <div id="loginErrors" style="color: red; font-weight: bold"></div> 
     <div id="login-form"> 
      <dl> 
       <dt><label for="userName">Email Address:</label></dt> 
       <dd><input name="user" id="userName" type="text" size="30" maxlength="80"/></dd> 
       <dt><label for="password">Password:</label></dt> 
       <dd><input name="pwd" id="password" type="password" /></dd> 
      </dl> 
      <div class="login_button"> 
       <input type="image" src="/assets/images/user/btn-log-in.gif" alt="Login"/> 
       <br/><br/> 
       <a href="javascript:openPopup('forgotPassword');" class="default">Forgot your password?</a> 
       <!-- 
       <a href="#" rel="#forgotPasswordDialog" class="default">Forgot your password?</a> 
       --> 
      </div> 
     </div> 
    </form> 

为什么这种行为在Firefox 7有什么不同?有人可以提出一个可以在所有浏览器中工作的解决方案吗?你能感觉到我的沮丧吗?

+0

是否只用'location.href =网址工作;'? –

+0

如果在作用域链上存在某个中间变量对象的* window *属性和no * location *属性,那么这只会有所作为。这是偶然的机会,所以我猜想值得一试。 * window.location *和* location *都可以预期通常解析为相同的对象,即[window.location](http://www.w3.org/TR/html5/history.html#the-location - 接口)对象,在大多数情况下。当然不能保证,这就是为什么许多脚本保持对全局对象的独立引用,这可以用来保证访问预期的对象。 – RobG

+0

我试过window.location,位置和window.location.href在FF7上没有成功,所有其他工作正常。 – Byron

回答

1

什么:基于评论

window.location = url; //no .href 

编辑。

由于您的网址与您的网站根目录相关,因此要使用的window.location对象的“最正确”部分将是路径名,因为它描述了相对于主机的路径。

window.location.pathname = "/my/relative/path/page.html"; 

然而,任何变化对window.location对象应当触发window.location.assign()方法和加载新页。如果在修改对象属性时没有触发该事件,则可以尝试直接调用它。

window.location.assign(href); 

最后,我通过控制台检查,无法用FF7重现这种行为。 window.location使用相对路径和完整的URL。

进一步参考:https://developer.mozilla.org/en/window.location

+0

我改变它为“window.location = url”,这也没有工作。 data.dest中返回的路径是相对的,并转到“/ my-account”不知道这是否有所作为。 – Byron

+0

window.location.assign(href)完成了这项工作。谢谢。 – Byron

相关问题