How to trigger current browser tab to close via Remix.js? - javascript

I want to close current browser tab via button click.
I tried window.close, but for clear reason it didn't work, because, the page is SSR.

Related

Capturing the browser tab reload and tab close functionality

I want to call an ajax to other pages on the browser or tab close. When we reload tab or close tab it calls onbeforeUnload and onUnload events.
If I reload page either by pressing ctrl+r or by pressing enter in address bar it should reload page without any prompt and if I click close of browser or tab or I press ctrl+w keys it should prompt that "Changes you made might be lost" and if user click leave it should close tab and call an ajax, else it should stay on page.
Can anyone help me with this?
Thanks in advance
Most of the browsers intentionally block popups triggered in onbeforeunload, for known reasons.
But if you want to preserve users' data from erasing, you can engage window.localstorage property.

How to detect if browser tab has changed and refresh it if so

I need a cross-browser javaScript code (if possible) that detects when entering a browser's tab (crossing from current opened tab to another tab) and refresh its page when the change was made.
Window events can be in handy. So you can react to the blur and focus events, so leaving your browser tab will cause blur event and returning back - focus event.
$(window).focus(function(){
//refresh your page here
});

Chrome Extension : Retain modal window when page refreshes

In my chrome extension, I am using content script on a particular site. When a particular button is pressed, the page refreshes with new updates. Is there any way to show a Modal window or Popup, from the time the button is clicked till the page refreshes.
Modal window or popup: no
But you can use a chrome notification. It will show a customizable window which you can remember its id and take it down when the page refreshes. I do this for a popular extension i develop.

Close a Chrome extension popup by clicking the browser action icon again

I'm developing a Chrome extension and I wanted to know if it is possible to close a popup by simply clicking again the icon that lets you open the popup: I tried anything but it looks like you must click elsewhere to close it. The docs states the onClicked event is:
Fired when a browser action icon is clicked. This event will not fire if the browser action has a popup (http://developer.chrome.com/extensions/browserAction.html#popups).
Thanks in advance.
[UPDATE] I tried the following and it half (!) works:
1. in popup.html I link popup.js;
2. popup.js reads the value of a variable contained in background.js;
var currentStatus = chrome.extension.getBackgroundPage().open;
if(currentStatus==0){
chrome.extension.getBackgroundPage().open=1;
}else{
chrome.extension.getBackgroundPage().open=0;
window.close();
}
What happens: the first click opens the app, the second closes it, BUT it remains a micro popup with no content upon the icon. If I remove that, I reached my goal.
The onClicked event is called if your extension's browser action does not define default_popup in the manifest. That note from the documentation isn't about whether the popup is currently open.
If the manifest defines default_popup then clicking the button again closes and reopens the plugin. The mousedown closes and the mouseup opens. (So clicking on the button and dragging away and releasing the mouse does close the popup, not that anyone should do this.)
I recommend setting default_popup and making a button in the html for the popup that closes the popup with window.close;, or find a point in your popup's use case where closing makes sense.
Well It has been a long time, and the issue/bug still persists on Chrome browsers. I've found a workaround, it's not great, but it does what I need - closes the window on a second icon click. Here's what I did in the popup javascript file:
if(localStorage.getItem('firstClick')==='true'){
localStorage.setItem('firstClick', 'false');
window.close();
}
else {
localStorage.removeItem('firstClick');
localStorage.set('firstClick', 'true')
}

How can I get number of child windows for a browser

I have one icon (say open) in home page. When I click on this icon one child window will be open and the icon in that home page is also changed to 'close icon'. When close this popup window the icon should be same as previous icon (i.e. open icon) in home page. It's working fine when I stay on the same page.
But when redirecting from home page to next page the entire page gets reloaded. And the default image (open icon) is displaying even if the popup window is opened.
Now my requirement is:
At the time of page redirection the image should be loaded based upon the popup window. i.e. if popup window is open it should display the close icon otherwise it display open icon.
If page is refreshed or redirecting to another page the reference of the popup window is removed. then how can I get the reference of that popup window in a redirecting page.
How to count the number of child windows for a browser
EDIT:
I have tried the following solutions:
I set cookie at the time of opening a popup window and reset that cookie whenever I have closed that popup window. But the problem is, at the time of page redirection if I close the popup window the cookie is not reset to it's previous value, because the page is still in processing.
same problem with the session variable also
Please help me.
Set a cookie or a session variable when you open and close. This way you can remember the state of your popup window during new requests
When you go from one page to the next, you lose the reference to the pop-up window. But the pop-up window doesn't lose its reference to the main window. window.opener will always point to the window that opened it, even when there's a page change. Use this fact to reestablish communication between the windows after a navigation event. You might need to use an interval function to probe the main window, as I don't think you can listen for an event.

Categories