This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed 7 years ago.
I tried several things, but everytime Chrome (and others?) detect a pop up... is possible to bypass it ?
window.open(
'/test.php',
'_blank'
);
}, 2000);
You would have to open the window using javascript, then set a setTimeout for two seconds that would wait for a variable to be set.
The new window would have to set a variable in the parent window to, lets say true.
Then with the setTimeout runs, it checks whether the variable is true, and if not, then opens the link. If it is, then do nothing
var didItOpen = false;
window.open('page.html');
setTimeout(function () { if (!didItOpen) location.href = 'page.html'; }, 2000);
Can you please try the code below?
//1000 = 1 second
setTimeout(function () {
window.open('url here', '_blank);
}, 1000);
Related
This question already has an answer here:
How do I make my webtime tracking extension more efficient?
(1 answer)
Closed 10 months ago.
I am working on a chrome extension, and I need the time spent on each tab. I'm using chrome.tabs api.
You need a timer, storage and tabs permissions and background Script.
The bg script needs an event and then check if its the desired URL:
window.addEventListener('load', function () {
chrome.tabs.getSelected(null,function(tab){
myURL=tab.url;
});
If the desired URL is met, retrieve current timestamp and save it to a variable.
If the user closes the tab or chrome window catch it:
chrome.tabs.onRemoved.addListener(function(tabid, removed) {
alert("tab closed")
})
chrome.windows.onRemoved.addListener(function(windowid) {
alert("window closed")
})
Wihtin the catch you get the current timestamp again, calculate end - start and you could save it to e.g. sessionStorage.
Maybe like a "chronometer", init a new Date() when your user open a tab and subtract a new Date() with the init time when your user close a tab?
This question already has answers here:
Calling functions with setTimeout()
(6 answers)
Closed 3 years ago.
I am trying to simulate a script that takes a long time to run on page load.
I tried this:
window.onload = function() {
setTimeout(alert("Page Rendered"), 200000);
};
But alert message happens instantly.
What am I doing wrong?
Check the function(). docs
window.onload = function() {
setTimeout(function(){alert("Page Rendered")}, 200000);
};
This question already has answers here:
How to close a window.open
(2 answers)
Closed 7 years ago.
I'm trying to crash a website a website I'm visiting by blasting it with requests and hopefully overwhelming the server the site is running on. The simple script I made
while ( true ) { window.open(window.location.href); }
keeps requesting the page I'm on until my browser crashes. So, what I'd like to do is the equivalent of
while ( true )
{
window.open(window.location.href);
CloseTheWindowIJustOpened();
}
How do I actually implement that?
Sure thing... the open() object returns you a window object pointing to that new window, and you can manipulate it using it...
example:
var win = window.open("", "myNewWindow", "width=200, height=100");
then, just invoke that variable and use the close() action
win.close();
remember to make your window variable global, so you can invoke it whenever you need:
var myWindow = null;
$(function() {
...
myWindow = window.open(...);
});
function closeMyAmazingWindow() {
myWindow.close();
}
function writeTextOnWindow(txt) {
myWindow.document.write(txt);
}
I am opening a popup window and attaching an onbeforeunload event to it like this:
win = window.open("http://www.google.com", "", "width=300px,height=300px");
win.onbeforeunload = function() {
//do your stuff here
alert("Closed");
};
If I leave the URL empty, the new popup opens with "about:blank" as the address but when I close it, I see the alert.
If I open in as you see it (with an external URL), once it's closed, I cannot see the alert anymore. Any idea why this is happening?
As mentioned, same origin policy prevents Javascript from detecting such events. But there's a quite simple solution which allows you to detect closure of such windows.
Here's the JS code:
var openDialog = function(uri, name, options, closeCallback) {
var win = window.open(uri, name, options);
var interval = window.setInterval(function() {
try {
if (win == null || win.closed) {
window.clearInterval(interval);
closeCallback(win);
}
}
catch (e) {
}
}, 1000);
return win;
};
What it does: it creates new window with provided parameters and then sets the checker function with 1s interval. The function then checks if the window object is present and has its closed property set to false. If either ot these is not true, this means, that the window is (probably) closed and we should fire the 'closeCallback function' callback.
This function should work with all modern browsers. Some time ago Opera caused errors when checking properties from windows on other domains - thus the try..catch block. But I've tested it now and it seems it works quite ok.
We used this technique to create 'facebook-style' login popups for sites which doesn't support them via SDK (ehem... Twitter... ehem). This required a little bit of extra work - we couldn't get any message from Twitter itself, but the Oauth redireced us back to our domain, and then we were able to put some data in popup window object which were accessible from the opener. Then in the close callback function we parsed those data and presented the actual results.
One drawback of this method is that the callback is invoked AFTER the window has been closed. Well, this is the best I was able to achieve with cross domain policies in place.
You could listen to the 'focus' event of the opener window which fires when the user closes the popup.
Unfortunately, you're trying to communicate across domains which is prohibited by JavaScript's same origin policy. You'd have to use a server-side proxy or some other ugly hack to get around it.
You could try creating a page on your site that loads the external website in an iframe. You could then pop open that page and listen for it to unload.
I combined #ThomasZ's answer with this one to set an interval limit (didn't want to use setTimeout).
Example (in Typescript, declared anonymously so as not lose reference to "this"):
private _callMethodWithInterval = (url: string, callback: function, delay: number, repetitions: number) => {
const newWindow = window.open(url, "WIndowName", null, true);
let x = 0;
let intervalID = window.setInterval(() => {
//stops interval if newWindow closed or doesn't exist
try {
if (newWindow == null || newWindow.closed) {
console.info("window closed - interval cleared")
callback();
window.clearInterval(intervalID);
}
}
catch (e) {
console.error(`newWindow never closed or null - ${e}`)
}
//stops interval after number of intervals
if (++x === repetitions) {
console.info("max intervals reached - interval cleared")
window.clearInterval(intervalID);
}
}, delay)
}//end _callMethodWithInterval
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