简体   繁体   中英

javascript alert and then a location replace not working

I trying to use an alert message when someone enters incorrect login details, and I then want to redirect them back to the same login page. I'm doing this inside a php file that is called when someone submits a login form. I'm trying something like this:

echo '<script language="javascript">';
echo 'alert("Invalid Username or Password")';
echo 'location.replace("target.php")';
echo '</script>';

but it goes to the php file itself, which shows nothing.

update it you have missing ; for alert

echo '<script language="javascript">';
echo 'alert("Invalid Username or Password");';
echo 'window.location("target.php")';
echo '</script>';
echo '<script>';
echo 'alert("Invalid Username or Password"); ';
echo 'location.replace("http://mywebsite.com/target.php"); ';
echo '</script>';

language attribute is deprecated

location.replace("target.php") must have valid url: location.replace("http://mywebsite.com/target.php") or you can use history.back()

and as noted, don't miss semicolon

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM