safari/chrome onsubmit="location.reload(true)" not working
Asked Answered
C

4

7

A form on my website is not functioning correctly in Safari/Chrome. When a user submits the form, it opens up a new tab, but I want the original page (page with the form on it) to reload. It works in IE, Opera, and Firefox.

The Code:

<form action="/search.php" method="post" onsubmit="location.reload(true)" target="_blank" name="myform">

I tried other javascript functions like:

  • window.location.reload();
  • document.location.reload();
  • window.location.replace('http://www.websiteurl.com');
  • window.location.href='http://www.websiteurl.com';
  • And other variations of these.

I thought maybe it was the onsubmit="" not working, but when I tried onsubmit="alert('test')" that worked fine in both Safari/Chrome.

Also, on the search.php page that the form posts to, if a user goes directly to the page using the url, and not submitting the form, I have it set that the body tag will load as:

<body onload="window.location.replace("http://www.websiteurl.com")>

which works on all browsers includeing Safari/Chrome.

What is going on here?!?!

Thanks!

Chavarria answered 5/3, 2012 at 20:16 Comment(4)
I don't know why it doesn't work regularly, but a setTimeout hack works: jsfiddle.net/xzanQ.Plucky
Do you want the new tab to open as well?Adenoidectomy
I've had a similar problem with reloading not working in all bowsers (though not in the context of form reloading) and ended up with: window.location=window.location;Campinas
I do want to open a new tab aswellChavarria
P
4

Since it was the solution for you:

Using setTimeout sometimes works as a hacky solution by postponing execution for a very short time: http://jsfiddle.net/xzanQ/.

Plucky answered 5/3, 2012 at 20:46 Comment(0)
E
0

You could try:

window.location.href=window.location.href
Eolian answered 5/3, 2012 at 20:29 Comment(0)
D
0

onsubmit gets executed before a form is posted. If a page is already being unloaded, the form may not be submitted anymore.

Try something like:

var myForm=document.getElementById('myForm');
myForm.addEventListener('submit',function(event){
  event.preventDefault();
  this.submit();//Submit the form BEFORE reloading
  location.reload(true);      
},false);
Denbrook answered 28/5, 2012 at 13:29 Comment(0)
H
0

I just faced a similar problemnd after debugging for hours I found out that a ; was missing in my onsubmit statement..

Changing:

onsubmit="location.reload(true)"

To:

onsubmit="location.reload(true);"

Fixed my safari on mac problem..

Hydromancy answered 9/10, 2013 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.