1

I have a link that opens a new window using window.open. The pop up works fine, however the normal web page stops loading objects (images, scripts, ajax scripts) and sometimes the page doesn't load at all.

Here is my code:

MyWindow=window.open('player.php','Player','width=500','height=300'); return false;

Is there anything I am doing wrong?

Thanks, Peter

Peter Stuart
  • 2,362
  • 7
  • 42
  • 73
  • 6
    There is obviously more happening than the single line of code you have provided that is blocking the main page from running. Create a jsfiddle.net example. –  Oct 12 '14 at 20:13
  • Well that is the code that opens the popup, and the pop up itself is just a static HTML page – Peter Stuart Oct 12 '14 at 20:15
  • can you wait for the page to load before spawning the popup? – dandavis Oct 12 '14 at 21:52
  • The popup is a function thats executed onclick. Once the popup is loaded I still want users to be able to go to other pages, but they can't. To help you understand, the popup is a radio station with a streaming link, and the website is a forum. So the pop up is meant to be played in the background. – Peter Stuart Oct 12 '14 at 21:58
  • May i ask why return false? – Romeo Oct 15 '14 at 11:10
  • 3
    There really isn't enough information given in your question. Unfortunately StackOverflow doesn't allow users to vote to close questions which have active bounties, but if it did I'd have voted to close this for the off-topic reason: *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.* I'm otherwise unable to replicate your problem. – James Donnelly Oct 15 '14 at 11:18
  • Is this browser specific? Did you check the console? – inf3rno Oct 15 '14 at 11:38
  • 1
    I couldn't tell you why it's happening, but a possible quick and dirty fix might be to automatically reload the page after opening the popup... that would maybe free it up if it's locked into some kinda loop or something – Kat Cox Oct 19 '14 at 21:05
  • In other way, you can start your `open/streaming` after the whole page has been loaded, you can do this via `jquery's bind` event, `$(window).bind("load", function() { // code here });` – Ravi Oct 20 '14 at 05:52
  • it should be window.open('player.php','Player','width=500,height=300'). window.open accepts 3 parameters – Bruno Calza Oct 21 '14 at 12:33
  • Can you please specify how this line is called? Is it called something like this: "javascript:"? – Tomzan Oct 21 '14 at 19:37
  • 3
    Why is this question getting upvotes? It should be closed as "unclear what you are asking". Looking at the answers it seems like we are playing a guessing game. – Salman A Oct 22 '14 at 06:50
  • I did downvoted question by the reason that it's certainly not contain enough information and during 9 days OP didn't provide full information. – andrey.shedko Oct 22 '14 at 07:07
  • Is there any error messages? Could you provide please full markup and JS code? Is it compulsory to use raw JS or may be there possible to use some jQuery plugin? Did you check in your browser network profile? What happend before and after window.open? – andrey.shedko Oct 22 '14 at 07:10
  • 2
    this questions is really badly written, even after serval advice to give more context. I wish there is a way to do remove these questions is somewhat a waste of time! – charleetm Oct 22 '14 at 10:50

6 Answers6

2

First of all, please be more specific: tell us more about your browser and which version, and possible your OS. It could be more related to the browser than to the web content.

Then on to the possible problem; you start with saying "I have a link that ...".

To me that sound like you use <a href="javascript:DoSomething()">. Or perhaps <a href="#" onclick="DoSomething()">.

I tried both in some modern browsers: Chrome v37, IE v11. Both browsers did not produce what you describe:
- Chrome v37 will happily keep on loading, even if I immediately click a "window.open()"-link on top of a (huge) webpage;
- IE v11 will someshow show "false", which is strange, but still not what you got.
In some cases I also got to deal with the popup blocker.

A general tip might be to NOT USE <a href> for things like this. Behaviour seems inconsistent across browsers, also these days there are better alternatives, such as <span onclick="">...</span> and <button onclick="">...<button> or by using JQuery or other frameworks (which I do not know much about).

Although this many not be a conclusive answer, maybe this can help you experiment on your own, and think about possible causes or alternative ways of doing things.

Peter B
  • 22,460
  • 5
  • 32
  • 69
1

The behaviour you describe should definitely NOT normally happen. This is confirmed by robbmj's JSFiddle, that fails to reproduce the problem. That's evidence that something is going on in the main page that is not plain vanilla page loading, or your "link opening" has something unusual to it. Apart from the syntax error (you use four parameters, not three).

Since you do not supply information on either of these points (how do you load the main page? How do you trigger the popup-opening code?), we do not even know if the problem might be browser-related; I'd start and try to test things in IE, Chrome and Mozilla to see whether anything changes; this might provide some useful insights.

One possibility

A very strong possibility is that your inadvertent fourth parameter goes into the window.open() "replace" parameter, which is a boolean, and triggers undefined behaviour or simply an error that stops everything. You should have things somewhat working in IE and not working at all in Firefox.

You should also be able to see whether this is the case by using Firefox and the Firebug extension, or the Web Developer Console in Chrome.

Another possibility

A more esoteric possibility is that the way you define the link might make the browser believe you've actually moved on to another page, so that there's no point in continuing loading the current page. Depending on the browser, this might have to do with how the link is defined and could be remedied by defining it some other way.

For example it could conceivably happen if you had

<a href="javascript:openPopup();">...</a>

which I suspect is what led user Tomzan to ask, "is the link something like javascript:...?"

So if this is the case, try with this instead (this works for me in IE9/Chrome/FF):

<a href="#" onclick="return openPopup();">link</a>


function openPopup() {
    MyWindow = window.open('player.php', 'Player', 'width=500, height=300');

    // Also try the following. You won't probably like the results (it should send the
    // popup window behind), but if it works, it proves we're dealing with a browser
    // issue there.

    // Blur and refocus
    // MyWindow.blur();
    // window.focus();

    // Just focus
    // window.focus();

    return false;
}

Workaround

A possibly acceptable workaround could be to disable the link altogether (or hide it via CSS), and only reactivate/show it upon main document being ready. This sidesteps the problem, even if user experience could be somewhat worse due to a longer wait.

But if it's so likely that a user clicks on the link before waiting for the whole page to load, I'd also consider not automatically loading the rest of the page at all, and reorganize information to provide a more streamlined navigation. Or maybe distribute it on two sequential pages. Again, unfortunately you did not supply enough information to do more than guess.

LSerni
  • 55,617
  • 10
  • 65
  • 107
  • The issue was becuase the popup content had a ajax requesting every 10ms, when it should have been every 10 seconds. it was hogging the network. Anyway, your answer was the most detailed, so I marked it as correct :) – Peter Stuart Oct 31 '14 at 15:13
-1

As you probably know, JavaScript is single threaded. Every event is queued until there is idle time for it to be executed. In the case of window.open, both windows must share a single context to keep it thread-safe because the opened window can access to it's parent using window.opener. I don't know how browsers implements it, but we can guess two possibilities:

  • Idle time is shared between the two windows. It means if the popup does many blocking statements, it can freeze the main window's events.
  • Only one of the two windows can be active, which depends on which one has the focus. In that case, all events may be paused in the main window when you're using the popup.

    If you want a more precise answer, I need more details about your code.

Community
  • 1
  • 1
Sebastien C.
  • 4,649
  • 1
  • 21
  • 32
-1
document.addEventListener("DOMContentLoaded", function () {
      //whatever the code
      MyWindow=window.open('player.php','Player','width=500','height=300'); return false;
}, false);
  • 1
    It does not address the part where OP says _I have a link that opens a new window using window.open_. – Salman A Oct 23 '14 at 06:51
-2

Try to wrap the code in SetTimeout

setTimeout(function () {
    window.open( .. )
}, 0);
Teemu Ikonen
  • 11,861
  • 4
  • 22
  • 35
  • 2
    Shouldn't change anything: `Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script block finishes executing.` - https://developer.mozilla.org/en-US/docs/Web/API/Window.open#Description – Sebastien C. Oct 15 '14 at 13:38
  • 1
    You can't depend on a timeout! – GuyT Oct 20 '14 at 09:51
-4
Your document should be loaded first, then popup should be open, So write your javascript code in the scope of $(document).ready().

    enter code here

$(document).ready(function(){
$("#clickme").click(function(e){
  MyWindow=window.open('player.php','Player','width=500','height=300'); return false;
});
});
Sanjay Soni
  • 201
  • 1
  • 13