Chris
Chris

Reputation: 101

javascript window redirect doesn't work in chrome ((canceled) - status )

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

Upvotes: 10

Views: 11307

Answers (8)

Alexey Ruzin
Alexey Ruzin

Reputation: 1027

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';
};

Upvotes: 0

Nata
Nata

Reputation: 1164

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.

Upvotes: 0

Barnett  John
Barnett John

Reputation: 1

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

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

Upvotes: 0

Sky.Zhao
Sky.Zhao

Reputation: 31

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

Upvotes: 0

Jose Benitez
Jose Benitez

Reputation: 163

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.

Upvotes: 4

SquirFly
SquirFly

Reputation: 63

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 }/`

Upvotes: 2

Mark Torres
Mark Torres

Reputation: 71

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

Upvotes: 3

DnT
DnT

Reputation: 61

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

Upvotes: 3

Related Questions