Wednesday 15 February 2012

jquery - JavaScript redirect not working and works only in Chrome if debugger is turned on -


What is wrong with the following javascript code to redirect to "success"?

  $ ('#recordUser'). Click (function () ($ {AXX} ({type: 'post', url: 'API / record user', data: $ ("# recordUserform"). Serialize (), datatype: 'Jason', success: function User emails) {var newUrl = "/ home / other page" email = "+ e-mail email; $ (location) .attr ('href', newUrl); // I also tried the following methods to redirect Is: //window.location.href = "/ home / other page? Email =" + user email. Email; // window. Location = "/ home / other Page? Email = "+ e-mail e-mail; // window.space location (newUrl);}, error: function (excerpt, position, fault) {console.log (xhr);}});}); / / # RecordUser is a button inside a form, for the method = "post"   

html :

  & Lt; form id = "record user farm" accept-charset = "UTF-8" method = "post" & gt; & gt; div class = "..." & gt; & lt; input id = " RecordUserEmail "class =" span6 "name =" email "type =" text "& gt; & lt; / div & gt; & lt; button class =" ... "id =" recordUser "& gt; Submit & lt; / Button & gt; & Lt; / Form & gt;   

I tested it in chrome and firefox and there is no redirect on both. I have already made sure that my server code is working properly.

If I run into Javascript debug mode, the page is redirected with any redirect command, but if I stop the redirect from debugger, the command stops working.

If I remove the "method" attribute from my html form then the query string appears in the URL, but the page remains in the same place. For example, if I was on the local host: 80 and then submitting the form, then the URL changes to the local host: 80 * email = Expected New URL instead of the email Strange, if I run it in debug then why does it work as expected?

If someone can point out the error and the reason for the code is only working in Chrome debug mode, would that be thankful / happy? I would also like to know how can I get a useful error message in my "Error" callback? By being useful I mean something that points to the source of error (like an exception message, stack trace, etc.) and nothing that I have to manually insert.

UPDATE 1

If I use html for the button

  & lt; Input class = "..." id = "recordUser" value = "submit" type = "change button" />   

And use the click method in the script, it works fine.

I still appreciate to inform about working with a code and why was the script working in Chrome Debug Mode?

Update 2

While posting Update 1, I realized that I had to type the type to the button Was missing in html for Probably, I thought that because it is marked as a button , then specify that this type will be redundant, but it is not.

  & lt; Button type = "button" class = "..." id = "recordUser" & gt; Submit & lt; / Button & gt;   

The above markup now works with the original Javascript, so I think the only question is why did he work in Chrome debug mode?

if window.location = "something" fails because two possibilities One of:

  1. The code is not being executed because your AJAX query is not successful (you may have some problems back end, using console.log at the beginning of the success function Do, to find out if it is executed).
  2. "Some" is exactly the same URL that is currently in your browser navigation bar, so it's actually working.

    Do not use them because they are completely wrong (the other two methods are correct):

    • $ (place) .attr ('href' , NewUrl);
    • window.location.replace (newUrl);

      The correct methods for redirection are (either crossbrowser will work):

      • window.location = "something";
      • window.location.href = "something";

No comments:

Post a Comment