How to stop window.open from refreshing the window in javascript? - javascript

Lets say you have code like this.
function openNewTab(url) {
const newTab = window.open(url, 'windowNameHehe');
};
Now every time you call this function it will open a new tab. However, if the window is already opened it will go to it and refresh/reload it. How do I stop it from reloading/refreshing and just bringing it to view?
Doing something like newTab.focus() or adding the boolean (true/false) in the 4th parameter of window.open isn't working for me either. Any help?

function openNewTab(url) {
var newTab = window.open(url, 'windowNameHehe', , false);
newTab.focus();
}
https://www.w3schools.com/jsref/met_win_open.asp
Open a URL in a new tab (and not a new window) using JavaScript

function openNewTab(url) {
const newTab = window.open('', 'windowNameHehe');
newTab.focus();
if (!newTab.location.hostname) {
newTab.location.href = url;
}
};

Related

Launch a custom protocol handler using javascript in Chrome without a popup?

I am trying to start a custom protocol handler in Chrome via Javascript. I can get the app to start but to do so creates a popup window which then triggers a pop-up blocker. Is there anyway to start the app without a pop-up window? The window closes but it is still considered a pop-up.
This is my current code:
function setBrowser() {
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("chrome") > -1) {
//If Chrome launch without ActiveX involved
var url = 'ABCProcessLaunch:-Domain mydomain -Args http://google.com -requirelogin true -method "chrome"';
setTimeout(window.close, 10);
window.open(url, '_blank');
}
}
I'm inferring from your call to window.close that this is likely why you need to open the link in a new window in the first place.
You may have some luck with opening the custom URL scheme in an <iframe> element instead. That way, your setTimeout call will still be triggered after 10 seconds:
function setBrowser() {
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf("chrome") > -1) {
//If Chrome launch without ActiveX involved
var url = 'ABCProcessLaunch:-Domain mydomain -Args http://google.com -requirelogin true -method "chrome"';
var iframe = document.createElement("iframe");
iframe.src = url;
setTimeout(window.close, 10);
document.querySelector("body").appendChild(iframe);
}
}

Perform action after window closed

I have a JS that opens a new tab. And I want to refresh the original window but only once the opened tab is closed. How would you do it?
Here's the code I have now (which obviously doesn't work - it does the refresh immediately after opening the new window):
window.open( SOME_WINDOW );
window.location.reload();
You can do it with something like this. Store the window handle, and use a polling timer to check the closed property of the window.
var childwindow = window.open(SOME_WINDOW);
var timer = setInterval(function() {
if(childwindow.closed) {
clearInterval(timer);
window.location.reload();
}
}, 1000);
Another solution (even nicer) can be found here

Check if window is already open window.open

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:
var newWindow = null;
function launchApplication()
{
if ((newWindow == null) || (newWindow.closed))
{
newWindow = window.open('abc.html','','height=960px,width=940px');
}
}
when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.
newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');
Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.
Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).
Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.
Edit:
Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like clicky! will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.
To open a window and keep a reference to it between page refresh.
var winref = window.open('', 'MyWindowName', '');
if(winref.location.href === 'about:blank'){
winref.location.href = 'http://example.com';
}
or in function format
function openOnce(url, target){
// open a blank "target" window
// or get the reference to the existing "target" window
var winref = window.open('', target, '');
// if the "target" window was just opened, change its url
if(winref.location.href === 'about:blank'){
winref.location.href = url;
}
return winref;
}
openOnce('http://example.com', 'MyWindowName');
You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:
var newWindow;
var openWindow = function(){
newWindow = newWindow || window.open('newpage.html');
newWindow.focus();
newWindow.onbeforeunload = function(){
newWindow = null;
};
};
Use the "closed" property: if a window has been closed its closed property will be true.
https://developer.mozilla.org/en-US/docs/Web/API/Window/closed
When you move on another page (on the same domain), you can re-set the window.open variable with popup page like this :
https://jsfiddle.net/u5w9v4gf/
Step to try :
Click on Run (on jsfiddle editor).
Click on Try me (on preview).
Click on Run to move on another page, the variable will be re-set.
Code :
window.currentChild = false;
$("#tryme").click(function() {
if (currentChild) currentChild.close();
const child = window.open("about:blank", "lmao", 'width=250,height=300');
currentChild = child;
//Scrope script in child windows
child.frames.eval(`
setInterval(function () {
if (!window.opener.currentChild)
window.opener.currentChild = window;
}, 500);
`);
});
setInterval(function() {
console.log(currentChild)
if (!currentChild || (currentChild && currentChild.closed))
$("p").text("No popup/child. :(")
else
$("p").text("Child detected !")
}, 500);

open page in new instance of browser

please tell me how to open a page on click of hyper link in new browser instance (so that it should not be blocked by popup blocker) in flash AS3/ javascript
Thanks
You can open on a new tab (not on a new browser!) by doing this:
var urlRequest:URLRequest = new URLRequest("http://stackoverflow.com");
navigateToURL(urlRequest, "_blank");
Please, use it wisely...
JavaScript
// spawnWindow( 'www.duh.com', 'windowRef' )
function spawnWindow(URL, Name) {
var features = 'toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=900,height=700';
window.open(URL, Name, features);
}
AS3 code to call JS function
var retVal:int = ExternalInterface.call("spawnWindow",address,newId);
It is impossible! Unless you use ActiveX.

window.open and searchpopop

We are use window.open for open popup. But then we want find it and close. Unfortunately we can`t save this popup handle to variable.
P.S. How get list of all windows?
This should work:
var wh = window.open(..)
wh is the handle to the popup window.
If you have control over the page that loads the script, you could do something like this. Warning: this is a really scary and generally bad thing to do:
<script>
var windowHandles = {};
(function() {
var realOpen = window.open;
window.open = function(url, name, features) {
windowHandles[name] = realOpen(url, name, features);
};
})();
</script>
That will build an object (windowHandles) in which the handles for each opened window will be saved.
Put that script in your page before the script that opens the other window is loaded.
I found not perfect solution, but it work.
win = window.open(null, 'Window1');
This code search search window with this name and return handler, but if window is closed it open empty popup.
I Think this is temporary solution
I don't like this solution. Fixing the script to give you a handle would be a better bet.
<button onclick="go()">Go</button>
<button onclick="stop()">Stop</button>
<script type="text/javascript">
function go() {
// Existing function. It opens a window with a name.
window.open('http://google.com', 'test', 'width=300,height=300');
}
var foo;
function stop() {
// Open a new window with the same name. It replaces the existing window.
// Since it opens a local document, the Same Origin Policy does not apply.
// ... and we can capture its return value to grab a handle on an existing
// window
foo = window.open('black-local-page.html', 'test', 'width=300,height=300');
// Give the local page time to load
setTimeout(continue_stopping, 500);
}
function continue_stopping() {
// Call window.open() on the window
foo.close();
}
</script>

Categories

Resources