Javascript警报,然后一个位置替换不工作


javascript alert and then a location replace not working

当有人输入错误的登录详细信息时,我试图使用警告消息,然后我想将他们重定向到相同的登录页面。我在一个php文件中这样做,当有人提交登录表单时调用。我正在尝试这样做:

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

但是它转到PHP文件本身,它什么也没显示

如果缺少;,则更新

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属性已弃用

location.replace("target.php")必须有有效的url: location.replace("http://mywebsite.com/target.php")或者您可以使用history.back()

如前所述,不要错过分号