2

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

Fresher
  • 894
  • 1
  • 7
  • 27

6 Answers6

5

Change the name of function, defining a function without scope is same as defining window.functionName

so when you define open, you are over-riding existing window.open so change the name of your function to open2 or something else.

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

<li onclick="open2()">John</li>
LNT
  • 876
  • 8
  • 18
3

Try:

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

DEMO

laaposto
  • 11,835
  • 15
  • 54
  • 71
3

use openx() instead of 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

Amit
  • 1,841
  • 1
  • 19
  • 36
3

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:

<li onclick="open1()">John</li>
Jameem
  • 1,840
  • 3
  • 15
  • 26
2

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.

dimko1
  • 872
  • 7
  • 15
2

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

Community
  • 1
  • 1
deadulya
  • 686
  • 6
  • 15