javascript 我想打开一个新标签而不是一个弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19098705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:19:49  来源:igfitidea点击:

I want to open a new tab instead of a popup window

javascriptphphtmltabspopup

提问by RedSun

I am trying to open a new tab. But Window.open()is opening up popup window.

我正在尝试打开一个新标签。但是Window.open()正在打开弹出窗口。

I want to open hello.phpfile in a new tab. But it is opening up in a new popup window.

我想hello.php在新选项卡中打开文件。但它在一个新的弹出窗口中打开。

<!DOCTYPE html>
<html>
<head>
<script language="javascript">

document.onmousedown=disableclick;
//status="Right Click Disabled";

function disableclick(event)
{
  if(event.button==2)
   {
     //alert(status);
     return false;    
   }
}
</script>
</head>
<body oncontextmenu="return false">

<form action="" method="POST" oncontextmenu="return false">



<b>Enter Username:</b><input type="text" name="username" value=""/><br>

<b>Enter Password: </b><input type="password" name="password" value=""/><br>

<input type="submit" value="submit" name="submit"/>
<input type="reset" value="reset" name="reset"/>

</form>

<?php


if (isset($_POST['submit'])) 
{

$username=$_POST['username'];
$password=$_POST['password'];

mysql_connect("localhost", "root", "") or die(mysql_error()); 

mysql_select_db("demo") or die(mysql_error()); 

$result=mysql_query("select * from employees where name='$username' and pass='$password'") 
 or die(mysql_error()); 


if(mysql_num_rows($result)==0)
{
print "<br/>";
print "<b>Incorrect Username/Password!!!</b>";
}
else
{


mysql_query("Create table $username(Question_No varchar(10),Selected_Answer varchar(10))") 
 or die(mysql_error());  


print "<br/>";
print "<b>Login successful!!!</b><br/><br/>";


print "<script>window.open('hello.php?username=$username')</script>";


print "<script>window.close('userdetails.php')</script>";
}

}
?>

</body>
</html>

回答by fray88

In case it could be a javascript issue about override functions, do that:

如果可能是关于覆盖函数的 javascript 问题,请执行以下操作:

<script>
(function(window, undefined){
    var win = window.open('your_url', '_blank');
    win.focus();
})(window);
</script>

That should make you can't use functions from other javascript code out of your function(window, undefined) wrapper-

这应该使您无法使用函数(窗口,未定义)包装器之外的其他 javascript 代码中的函数-

回答by souvickcse

You can do it by window.open(url, '_blank');

你可以通过 window.open(url, '_blank');

回答by Rajesh

assign the url to open to a tag's href attribute, with target="_blank", then trigger link click when you want. Example:

将要打开的 url 分配给标签的 href 属性,使用 target="_blank",然后在需要时触发链接点击。例子:

<a id="myLink" href="hello.php?username=<?php echo $username; ?>" target="_blank">

Then, call a js function to trigger link click

然后,调用一个js函数触发链接点击

document.getElementById('myLink').click();