Search code examples
javascripthtmlasp.netbrowserwindow.open

How can I open a new window on top of any other with JavaScript?


If you open a window like this and open a notepad at the same time, new window will open below the notepad.

I'd like to know how to open a new window on top of any other windows. Adding a window.focus() is not working..

    setTimeout(function() {
        window.open('http://google.com', 'google new window', 'width:10');
    }, 5000);
    

openning window


Solution

  • Try this -

    window.open(url, "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=900, top=10, left=10");
    

    The open() method creates a new secondary browser window, similar to choosing New Window from the File menu. The strUrl parameter specifies the URL to be fetched and loaded in the new window. If strUrl is an empty string, then a new blank, empty window (URL about:blank) is created with the default toolbars of the main window.

    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. The window creation and the loading of the referenced resource are done asynchronously.

    var windowObjectReference;
    var strWindowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
    
    function openRequestedPopup() {
          windowObjectReference = window.open("http://www.cnn.com/", "CNN_WindowName", strWindowFeatures);
        }
    
    
    var windowObjectReference;
    
        function openRequestedPopup() {
          windowObjectReference = window.open(
            "http://www.domainname.ext/path/ImageFile.png",
            "DescriptiveWindowName",
            "resizable,scrollbars,status"
          );
        }
    

    If a window with the name already exists, then strUrl is loaded into the existing window. In this case the return value of the method is the existing window and strWindowFeatures is ignored. Providing an empty string for strUrl is a way to get a reference to an open window by its name without changing the window's location. On Firefox and Chrome (at least), this only works from the same parent, ie. if the current window is the opener of the window you try to get an handle on. Otherwise the call to window.open() will just create a new window.

    To open a new window on every call of window.open(), use the special value _blank for strWindowName.