admin管理员组

文章数量:1201573

I'm trying to do a redirect using the below code:

<script  type="text/javascript">
window.location.href = ""
</script>

FF and IE work as they should. Chrome doesn't.

The request above to , gets a 'canceled' status in Chrome browser > Development tools > "Network".

I've tried several other functions:

location.href = url
location.replace(url)
document.location = url
location.assign(url)
window.open(url, '_self')

Same code pasted within a local html file works fine.

Below is the redirect request that it's canceled by chrome:

Any clues? Thanks

I'm trying to do a redirect using the below code:

<script  type="text/javascript">
window.location.href = "http://google.com"
</script>

FF and IE work as they should. Chrome doesn't.

The request above to http://google.com, gets a 'canceled' status in Chrome browser > Development tools > "Network".

I've tried several other functions:

location.href = url
location.replace(url)
document.location = url
location.assign(url)
window.open(url, '_self')

Same code pasted within a local html file works fine.

Below is the redirect request that it's canceled by chrome: http://pastebin.com/hD36M1RG

Any clues? Thanks

Share Improve this question edited Dec 19, 2011 at 22:18 Chris asked Dec 19, 2011 at 19:13 ChrisChris 1011 gold badge1 silver badge4 bronze badges 13
  • 1 That should definitely work. You must have something else causing the problem. Try and reproduce it on jsfiddle.net, and please give us what OS and browser version you have. – Alex Turpin Commented Dec 19, 2011 at 19:15
  • Yeah, it should, but it doesn't. OS: windows 7 Browser: Google Chrome latest version: 16.0.912.63m – Chris Commented Dec 19, 2011 at 19:57
  • Something else must be wrong with your code. Can you reproduce the problem on jsfiddle.net or alone in a new HTML file? – Alex Turpin Commented Dec 19, 2011 at 19:59
  • Weird. So I copied the redirect code within a local html page, and redirect worked as it should. What I don't understand, why the redirect it's just canceled, when it runs from the remote apache server. – Chris Commented Dec 19, 2011 at 20:03
  • I'm sorry but I don't know either. At least now you have a better idea of where the problem stems from (Apache). – Alex Turpin Commented Dec 19, 2011 at 20:04
 |  Show 8 more comments

8 Answers 8

Reset to default 4

I was getting a similar problem, the issue was a button submitting a form while executing the window.location.href = ... code.

I had to put type="button" in the button attributes to prevent it from submitting.

Try without window., it helped in my case. I had used location.assign() instead of window.location.assign() and worked.

I was doing the same thing. A quick patch was to add a little delay before the redirect using setTimeout.

I had a somewhat similar issue, (but it did not work on Firefox either). The problem was just that i forgot to add http:// in :

window.location.href = `http://${ window.location.host }/pages/${ name }/`

using setTimeout worked for me, and i'm facing this problem with vue-router, tring to redirect to an auth page in 'beforeEach' hook.

I ran into a problem about vue here . Use vue.nextTick (function () ()) solved it.

Vue.nextTick(function () {
   window.location.href="/***/****_view.action
})

It appears that Chrome wants to first complete some (or all) of the outstanding AJAX calls before redirecting or reloading the page, so it is worthwhile examining your traffic.

For me, it happened because multiple AJAX requests were being sent at the same time from within a setInterval function executing at a high rate. I had to clearInterval in order to get the window.location.href request to not get cancelled.

That would explain why setTimeout worked for some people above.

I had similar issue when adding reaction to form submit:

const myOnClick = () => {
   window.location.href = 'http://go.whatever';
};
-----
<form>
  <button onclick=myOnClick>Go go go</button>
</form

It cancels going to go.whatever and refreshes current page. When I call preventDefault(), it starts working well.

const myOnClick = (event) => {
   event.preventDefault();
   window.location.href = 'http://go.whatever';
};

本文标签: javascript window redirect doesn39t work in chrome ((canceled)status )Stack Overflow