简体   繁体   English

无法在浏览器中打开新标签?

[英]Unable to open new tab in browser?

This may sound like duplicate question but i don't know what problem i am facing. 这可能听起来像重复的问题,但我不知道我面临的问题。

This is my code 这是我的代码

<!DOCTYPE html>
<html>
<body>
<script>
function open()
{
var win = window.open('http://stackoverflow.com/', '_blank');
if(win){

win.focus();
}else{
alert('Please allow popups for this site');
}
}
</script>
<div align="center">
<h4>Online Users</h4>
<ul>
<li onClick="open()">John</li>
</ul>
</div>
<link href="http://fonts.googleapis.com/css?family=Open+Sans Condensed:300italic,300,700" rel="stylesheet" type="text/css">

    <div class="nomessages">

        <img src="../img/unnamed.jpg" id="noMessagesImage" />

        </div>
<div class="chatscreen">

        <ul class="chats">
            <!-- The chat messages will go here -->
        </ul>

    </div>

 <form id="chatform">

        <textarea id="message" placeholder="Write something.." rows="10" cols="30"></textarea>
        <input type="submit" id="submit" value="SEND"/>

    </form> 


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="../js/moment.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="../js/chat.js"></script>
</body>
</html>

I expect to open a new tab when i click John but instead it is opening the url on same window.I don't know what am I doing wrong here 我希望在我点击John时打开一个新的标签,但它会在同一个窗口打开网址。我不知道我在这里做错了什么

Change name of function, defining a function without scope is same as window.functionName 更改函数名称,定义不带作用域的函数与window.functionName相同

so when you define open, you are over-riding window.open so change the name of for your function to open2 or something 因此,当您定义打开时,您将覆盖window.open,因此将函数的名称更改为open2或其他内容

function open2()
{
var win = window.open('http://stackoverflow.com/', '_blank');
if(win){

win.focus();
}else{
alert('Please allow popups for this site');
}
}

and in your HTML 并在您的HTML中

<li onclick="open2()">John</li>

Try: 尝试:

 <li onClick="window.open('http://stackoverflow.com/', '')">John</li>

DEMO DEMO

use openx() instead of open() . 使用openx()而不是open()。

<!DOCTYPE html>
<html>
<body>
<script>


function openx()
{
var win = window.open("http://stackoverflow.com/", "_blank");
}
</script>
<div align="center">
<h4>Online Users</h4>
<ul>
<li onclick="openx()">John</li>
</ul>
</div>
</body>
</html>

Demo 演示

Try using different name for the function.. 尝试为函数使用不同的名称..

<script>
function open1()
{
    var win = window.open('http://stackoverflow.com/', '');
    if(win){

    win.focus();
}
else{
   alert('Please allow popups for this site');
}
}
</script>

HTML Markup : HTML标记

<li onclick="open1()">John</li>

Well, it depends on your browser settings: opening in a new window, or opening as a new tab. 嗯,这取决于您的浏览器设置:在新窗口中打开,或作为新选项卡打开。 So probably, you can't force browser opening new tab instead of window. 所以可能,您不能强制浏览器打开新选项卡而不是窗口。

To open win you should: 要打开胜利你应该:

<li onClick="window.open('https://stackoverflow.com/', '_blank')">John</li>

If in browser not set special behaviour on prefer new tabs or windows it will open in new tab. 如果在浏览器中没有设置偏好新选项卡或窗口的特殊行为,它将在新选项卡中打开。 But if you`ll add additional params for window it will open in new win. 但如果您为窗口添加额外的参数,它将在新的胜利中打开。

<li onClick="window.open('https://stackoverflow.com/', '_blank','height=200,width=200')">John</li>

Besides, this topic have a lot of discussion: 2011 year StackOverflow 此外,这个话题还有很多讨论: 2011年StackOverflow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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