5

How to open pop-up new window when load page using Javascript?

I want, when a website load, it will automatic open popup new window.

I use the following:

<script type="text/javascript">
function open_on_entrance(url,name)
{ 
   window.open('http://www.google.com','google', ' menubar,resizable,dependent,status,width=300,height=200,left=10,top=10')
}
</script>
<body onload="open_on_entrance()"></body>

Not work??

mrTom
  • 45
  • 1
  • 1
  • 4
  • You can't do this. It's a browser security feature. Some functions, like opening a pop-up window, have to be tied to a user action, like clicking a button. You can pop up a `
    `, which might be a way to do what you want.
    –  Oct 10 '13 at 10:15
  • Thanks @MikeW. But I want to display the new window. There is no other way to display it so? – mrTom Oct 10 '13 at 19:35
  • If you _must_ have a new window then you'll have to get the user to click something - an ugly 'Click here to enter' with a `window.open()` in the click handler should work, but it would look horrible. Alternatively, popping up a `
    ` with an `
    –  Oct 10 '13 at 20:17
  • I tested your exact code, seems to work fine on chrome Version 36.0.1985.125 m. Make sure you do not have a popup blocker enabled. I have used the similar feature on some of my codes too and it works fine.here is the link http://stackoverflow.com/q/24830249/2666522 the only issue I had was getting focus on the popup window if it was minimized before reopening it again. – dishwasherWithProgrammingSkill Aug 13 '14 at 20:40

4 Answers4

0

You can't do this. It's a browser security feature. Try using jQuery UI: Dialog

http://jqueryui.com/dialog/

Example:

    <html>
      <head>
          <meta charset="utf-8">
          <title>jQuery UI Dialog - Default functionality</title>
          <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
          <script src="//code.jquery.com/jquery-1.10.2.js"></script>
          <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
          <script>
          $(function() {
              $( "#dialog" ).dialog();
          });
          </script>
      </head>
      <body>
        <div id="dialog" title="Basic dialog">
            <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
        </div>
      </body>
    </html>
Community
  • 1
  • 1
Eduardo Cuomo
  • 17,828
  • 6
  • 117
  • 94
0

try this-

<script type="text/javascript">
window.onload = function() {
   window.open('http://upcominggames2015.blogspot.in/','new games release',' menubar=0, resizable=0,dependent=0,status=0,width=300,height=200,left=10,top=10')
}
</script>

Paste this code in head section of your website.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
nitishk
  • 11
-1
<html>
<head>
<script type="text/javascript">
window.onload = function() {
   window.open('http://www.google.com','google',' menubar=0, resizable=0,dependent=0,status=0,width=300,height=200,left=10,top=10')
}
</script>
<body>
</body>
</head>
</html>
babonamu
  • 397
  • 1
  • 4
  • Thanks for your answer, but it did not work. Do you have any other ideas? – mrTom Oct 10 '13 at 19:37
  • This won't work - it's to do with browser security. –  Oct 10 '13 at 20:12
  • @MikeW - This code has been tested in Google Chrome and IE 10, and successfully operated. – babonamu Oct 10 '13 at 23:49
  • It works only if the pop-up blocker is turned off. Pop-up blockers tend to be turned on by default, and require specific user action to disable them. mrTom has already indicated that this didn't work for him. –  Oct 11 '13 at 00:22
  • @MikeW - Would not it unavoidable because it is the basic constraints of the pop-up? – babonamu Oct 11 '13 at 00:48
-1
var newwindow;
function poptastic(url)
{
    newwindow=window.open(url,'name','height=400,width=200');
    if (window.focus) {newwindow.focus()}
}
MS Wani
  • 31
  • 6
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Jan 25 '18 at 10:45