Closing a tab without closing the window - javascript

Hi I have set up a task in my task scheduler to login to my UTM by opening a firefox window at regular intervals. When the relevant URL is loaded a auto-login GM script works on it and logs me in to UTM.
I would like to close that tab after the login is done. If I use
window.close();
It works fine but if no other tab is opened at the time, it simply closes the window.
Without going into details of UTM I would like the following -
A GM hack which closes the tab only if other tabs are already open. If it is the last tab of the window, then it should just replace it with a blank tab (so that window is not closed).
Probably GM cannot obtain any info about the other tabs, but is there any hack ?

As far as I'm aware this isn't possible, if nothing else then for security reasons: you don't spam javascript from your video streaming tab collecting all your data and submitting forms on your behalf.
Here's a thread with a solution that worked for IE7 that you may be able to manipulate but as I said it's not likely.
If you decide to have ago, don't think about identifying the number of tabs, it won't happen, instead focus on determining whether or not there are multiple tabs.
The linked example uses if(clientY == 0) to determine whether or not the browser is closing. This would be a good starting point.
Again, for security reasons, you won't be able to find out information about open tabs.

Related

show custom popup on page refress and browser/tab close in javascript/jquery

I want to open popup when closing the tab or browser first time and get the user review. I used onbeforeunload(), but I need to handle page refresh and tab close deferent different event.
Short answer: You can't.
Longer answer: For rather obvious "security" reasons it is very limited what you can do when a user tries to navigate away from a page / close a tab or in any other way leave the site.
The reason is that browsers want to make absolutely sure that you cannot prevent the user from closing a tab or window that he / she wants to close.
Therefore the onbeforeunload javascript event - and by extension the beforeunload jQuery event - are extremely limited in what they can do. One of the things they definitely cannot do is prevent the page from closing - except using one very standardized and rather boring method the browser (usually) allows.

How would you stop a page from refreshing automatically?

I have a problem when I am trying to check the source of an interesting page which keeps refreshing automatically every 3-5 seconds (presumably due to some js script) which resets my Inspect Element Inspector window every time the page is refreshed.
Is there any other way other to stop that page from refreshing or perhaps the Inspector window from resetting itself other than turning on NoScript to stop the page from refreshing automatically?
Usually I just open DevTools, switch to the appropriate panel if necessary, and hit pause.
Opening DevTools: Via menus, or by press F12, Ctrl+Shift+I, or Cmd+Shift+I depending on browser and OS.
Switching panels: Pick the panel from the tabs at the top of DevTools. It'll be called "Debugger" (Firefox, IE) or "Sources" (Chrome) or similar.
Pausing: In the Debugger/Sources panel, click the pause button (usually looks like the pause button on a television remote control, ||) or press the keyboard equivalent. Keyboard equivalents are
Firefox & Chrome: F8
IE: Ctrl+Shift+B
(Updated 2020-03-30)
In Firefox 74 this option is in Options -> Privacy & Security -> Permissions
(Original reply)
Firefox has the option to prevent refresh natively, the option is in Advanced->General->Warn me when websites try to redirect or reload the page
The most popular solution for this problem is to trap the beforeunload event. The browser will ask the user for confirmation to leave the page. The code, in its simplest form, looks like this:
window.onbeforeunload = function() { return true }
You can enter this code in console. Alternately, you can simply paste the following URL in the browser address bar (console not required). You can even bookmark it.
javascript:window.onbeforeunload = function() { return true }
Be advised that modern browsers might chop off the javascript: part when you paste it inside the address bar; make sure you type it back.
To determine the cause of redirect in Firefox, try the following:
Open Web Developer Tools (CTRL + SHIFT + I), open "Toolbox Options" and check the "Enable persistent logs" option. This makes the logs persist across page loads (logs are cleared otherwise).
Now switch to "Network Monitor" tab.
Open the URL and let it refresh.
Inside the Network Monitor > Cause column you will find out why the page reloads.
The cause column is pretty ambiguous (Chrome does a much better job). However, if JavaScript was used to trigger page (re)load then it at least shows you the filename and line number of that script.
When the page is still loading, you can press the Esc key. While the page is still white, press it. When you stop the page from loading at this point, this usually stops all the auto loaded javascript. Any scripts that run on actions are usually not effected. Each page is different, try different timings.
When I use a site called NovelUpdates there is javascript that can make certain elements hidden, and when I press Esc on page load all the elements that would be hidden after page load are visible. Then when I click a button that would execute javascript that operates with no problems. NoScript isn't going to solve your issue I believe.
Another example of this are those websites with annoying boxes that pop out after 10 seconds that says you aren't a member and can't view any more of this site without logging in, like some news article websites.
What you could do is use the command exit(), which is the equivalent to die in php and simply stops the script.
If you don't know what's causing it and you don't want to look for the "bad boy", then you might as well stop the entire script at the very bottom of the page.

Popups being blocked on our app but not others

I am noticing that my share popups are being blocked on our application but not others.
Here is the code execution:
1.) User enters web page.
2.) User clicks on facebook or twitter or googleplus share icon
3.) Onclick event passes the request to an internal controller that saves some information and then redirects back to the originating webpage. This time, however, there is a request parameter that invokes the usage of opening a new window.
The code I have that invokes opening a new window is (for this example we will use facebook):
var url = 'https://www.facebook.com/sharer/sharer.php?u='+copyLink;
window.open(url,'newwindow','width=600,height=600');
Now, if i enable popups it works fine. The problem is the user has to enable popups every time.
Is it a server issue? What is the reason why on other apps they don't have blocked popups but for OUR APP we cannot use popups without enabling popups
Any help would be greatly appreciated.
From what I understand, your application attempts to open a popup window following a page load. The nytimes.com popup appeared in response to a mouse click.
There is an important difference between these two ways to open a popup: the latter follows a user-initiated action but the former does not. As a result, popup blockers will generally block the former but not the latter. A lot of users wouldn't be too surprised if a popup opened when they clicked a button, but they would be more irritated if a popup appeared when a page loaded. The fact that your popup was ultimately triggered by an action on another page doesn't matter - what happens if you manually add to the URL the extra parameter that opens the popup?
Here's an old page on MSDN on popup blockers. It may describe the popup blocker in IE6 (of all browsers), but I think it still provides a reasonable explanation of when popup blockers typically permit or block popups.
Would it be possible to open a popup for your share dialog before calling back to the server? I would expect that opening a popup in the onclick handler would work without needing to explicitly allow popups.

How do I prevent browsers from blocking the pop-up window that I've created?

I created a simple JavaScript function to display my pop-up window once it loads. But it keeps on being blocked by Firefox and Google Chrome and I have to somehow enable it on the Firefox and Chrome to display the pop-up.
Are there any alternatives for this?
I have a player on the pop-up window so I have to use a pop-up to let the player play automatically. The problem is that if I put it on the page itself, once the user clicks another page the entire page reloads and the player automatically stops for a few seconds until the whole page reloads and I have to prevent this from happening.
The general rule is that popup blockers will engage if window.open or similar is invoked from javascript that is not invoked by direct user action. That is, you can call window.open in response to a button click without getting hit by the popup blocker, but if you put the same code in a timer event it will be blocked. Depth of call chain is also a factor - some older browsers only look at the immediate caller, newer browsers can backtrack a little to see if the caller's caller was a mouse click etc. Keep it as shallow as you can to avoid the popup blockers.
Please take a look at dthorpe's answer here. It covers your question.
You could try putting the player on the original page, and using something like History.js to control page changes (you could have the main page body in one wrapper div that changes, and leave the player outside of it).
Otherwise, you could try (assuming you meant a HTML5 <video> or <audio> player) downloading the data to localStorage/cookie/[other persistent storage mechanism] and have it seek everytime you change a page.
It will be hard to stop browsers from blocking your pop up window, because any way to do so is inherently exploitable; however, if you call the function to open another window from an onclick event, you may be able to circumvent some popup blockers. Also, some popup blockers allow popups when using the https protocol, although not many have this feature, and https can be hard to implement for the average website, if you don't have physical access to the server.
One other option is to open the other page in another tab (like this w3c example; you can 'click' the link with javascript).
You might also want to look at this post, as it is somewhat similar.
I only just discovered you asked this question.
Here's the answer in full.
Basically, you can simply create the popup immediately as the user event is fired, then fill it with content (your player, for instance) as you have it available.

Open Popup when user leaves the site (but not when user clicks on an internal link )

Here is what I need to do:
I want to launch a popup window when the user exits the website.
I found code that detects when the user closes the window, but that same code ALSO fires when the user clicks on an internal link,
(which I don't want).
Any ideas how to do this?
I've looked everywhere and I can't find a clear solution.
This solution needs to work on all three browsers : FireFox / IE / Safari!
You can't, there is no such event that will be triggered when someone exits the site. That's why in the early 2000s someone too clever invented "pop-unders": popups that will open immediately, but will be put on the background, behind the browser's window.
Which are one of the most annoying things on the web, and the first ones that any popup blocker or antivirus will kill :) On the other side, there are legitimate uses for that, like most surveys you see (I got one from microsoft some days ago).
Never rely on popups, unless you are writing an intranet site, or one where you are sure all of your visitors will not have a popup blocker.

Categories