This seems to be a common kind of complaint, but I don't see any exact duplicates and more importantly I don't see any working answers, so I'm joining the crowd.
I have some code that does
window.onbeforeunload = function() { PubNub.publish(...); };
to publish a message to a PubNub channel when the user leaves the current page. It's a chat forum, and we want to publish the fact that User X is no longer in the room. (Please suppress the urge to suggest other designs, such as having every user send a heartbeat; this is just a toy project I'm playing with.)
Watching the channel in another browser tab, I observe that the PubNub message gets published successfully 100% of the time when I
close the tab
and occasionally (both yes and no) when I
refresh by clicking the circley icon in the address bar
but apparently never when I
navigate away by clicking a link
navigate away by entering a new URL in the address bar
refresh by entering the same URL in the address bar
refresh by hitting ⌘R
Notice that even putting return "please don't exit"; in my onbeforeunload handler results in flaky publishing: sometimes I enter the same URL in the address bar and the publish happens, sometimes I enter the same URL and the publish doesn't happen.
Is this some quirk of Safari, and if so is there a known workaround?
Alternatively, is PubNub.publish() somehow "too slow" or "too multithreaded" to succeed during an onbeforeunload event? I don't really understand how onbeforeunload works.
(For the time being, the whole code can be accessed here, but if you're reading this question a month from now, be aware that that link may be dead and/or fixed.)
This is really what our Presence feature is for. Check it out here:
http://www.pubnub.com/docs/javascript/overview/presence.html
This way, when you "leave" the channel, the Presence system can broadcast this to those interested. Is this what you are looking for?
geremy
Related
I used speechSynthesis API in this way:
speechSynthesis.speak(new SpeechSynthesisUtterance("hello world"));
But right now I get error after update Google Chrome:
[Deprecation] speechSynthesis.speak() without user activation is no
longer allowed since M71, around December 2018. See
https://www.chromestatus.com/feature/5687444770914304 for more details
speechSynthesisMessage #
application-2c16c437c2795ae01c0a8852e5f8da58dad99d6e17814a31f1eea19922c5ebd2.js:147
How I can fix this issue and ask permission?
This is part of Chrome's new policies regarding making sound from web-pages.
You simply need your user to provide an user-gesture (for which you can find a list here) during the lifetime of the parent document (i.e the event may long be dead, as long as the user ever interacted with the page).
Note that these events can even traverse frames, so for instance, in StackOverflow, the simple fact that you do have to click on the "Run" button will make the inner frame allowed to execute this code:
const ut = new SpeechSynthesisUtterance('No warning should arise');
speechSynthesis.speak(ut);
And in your code, you simply have to provide some kind of an UI that will ensure your users have interacted with the page before you call this method (e.g a button / toggle will do perfectly).
If you set your site address as "trusted" in chrome://settings/content/sound it seems to enable sound and speech synthesis even without user interactions.
I use Chrome in a TV just as a system monitor, using kiosk mode and without any user interactions. It doesn't even have keyboard and mouse.
Still, I was able to enable in some versions of Chrome/Chromium, but not in others.
This error means entire document (Website) has no user interaction and Google Chrome update its policy regarding making sound from the website without user interaction.
User interaction means: click, dblclick, mouseup, pointerup, reset, submit etc.
Solution:
So, if you want to run speechSynthesis.speak(); without real user interaction, then you just create temporary user interaction using a method like .click(), etc.
Although I haven't found any way to ask permission, the user can enable permission in Google Chrome:
Click on the icon on the left of the URL bar, and open Site settings
Change the Sound setting from "Automatic (default)" to "Allow"
After doing that, the site will be able to make sounds without any user interaction, including speech.
Unfortunately, I haven't found a way in code to know whether this is working or not. (Perhaps we could try one of the other audio APIs and see if it responds with an error message.)
I resorted to swal("Click OK to speak").then(() => speakButton.click()); (with https://sweetalert.js.org) -- https://patarapolw.github.io/tts-api/?q=你好&lang=zh-CN&rate=0.8
Note that if (confirm("Click OK to speak")) speakButton.click() doesn't work.
I was having same issue, and could sort it by using window.onload like this:
window.onload = function(){
var u = new SpeechSynthesisUtterance('All is Ok');
u.text = 'Hello World';
u.lang = 'en-US';
u.rate = 1;
u.pitch = .4;
speechSynthesis.speak(u);
}
Of course this triggers only once after page is loaded, but it was a good solution for me.
A simple hack, without needing a real user activity, is to execute a click event on a hidden button like so.
document.querySelector('button').click();
var msg = new SpeechSynthesisUtterance('Test');
I'm building a quick VOIP demo using Skype and when I press a call button, the Skype application takes the focus away from the browser. You can try here http://developer.skype.com/skype-uris/skype-uri-tutorial-webpages where you'll find several "Try it here" links. When I click those links, I would like the browser to maintain focus. Is there a way to do this?
Thanks.
What you would need to do is apparently called "focus stealing" from my web searches.
At least as far as Windows is concerned, there does not seem to be a reliable way to do this from the browser alone.
I just googled "focus stealing" (which is what the JavaScript only solution would need to do to get this done) and found many answers showing that, though theoretically possible, depending on the configuration of Windows stealing the focus away from Skype by the browser would probably not work in the majority of cases.
The complaints in the Google links are numerous and some answers conflict, but it looks like reliably "stealing the focus" back to the browser is not going to be supported.
This is a good thing though, if you think about it - I do not personally want just any old JavaScript program running in my browser to change my focus from what I am working on back to the browser willy nilly - this would be a very annoying behavior for a web page to be able to do at best, making my system useless at worst.
If you could do it in this case using some methodology allowed in a browser, so could anyone else - even malevolent websites.
The best answer is to never let the focus leave the browser, but I have no idea how to do that in your specific case. Perhaps whatever means you are using to launch Skype may have an option or something to launch it in the background or whatever, never changing the focus.
I did not hit on specific links pertaining to Apple OSes, Linux or mobile OSes, but I have a feeling the same concerns and limitations apply for those as well.
Here are some of the links on the Google search (and sorry about the bad news for your needs):
Microsoft Answers Forum Post
Focus stealing is evil
http://pcsupport.about.com/od/windowsxp/ht/stealingfocus02.htm
you can open it on new window, then close the new window and refocus on yours
somthing like:
a=window.open('skype:ohadcn?chat',10,10);
//i couldn't find a relevant event, onload() do not work for me here
//so i used setTimeOut, hoping that two seconds is enough to open skype but not enough to loose the user
setTimeout(function(){ a.close();window.focus();},2000)
I went to the skype tutorial page in Chrome, brought up the console and tried Ohad's answer, but it would not return the focus to the tutorial web page.
I even tried a script to perpetually put the focus in the Search textbox:
function ASDF() {
document.getElementsByName("q")[0].focus();
setTimeout(ASDF, 1000);
}
setTimeout(ASDF, 1000);
Still no luck.
I tried changing Ohad's script so that it would reopen the tutorial page in a new window after the skype app opened. It would work if the tutoral/console page was the only tab in the window:
a=window.open('skype:ohadcn?chat',10,10);
setTimeout(function(){
a.close();
a=window.open('http://developer.skype.com/skype-uris/skype-uri-tutorial-webpages', 10, 10);
window.close();},2000);
However, if the tutorial page/console script was in window with other tabs, it did not return focus to the reopened page. Not to mention, IE might warn the user that the original page is trying to close.
I do not think there is a way to consistently achieve your goal, but I reserve the right to be wrong.
For accessibility reasons, for my Chrome extension, I need to be able to replace all standard window.onbeforeunload popups with an in-page HTML substitute. I know how to create a substitute skeleton, that's trivial.
And I know the basic idea here is window.onbeforeunload = {my substitute code}.
What I don't know how to do is grab the text and code from each confirmation window that a page issues, and channel it into my substitute, so that the same message is shown as would be in the original popup and clicking my substitute confirm and leave page (or whatever) buttons yield the same result as the original popup.
How can this be done?
EXAMPLE:
You have my extension installed (don't ask what its purpose is, irrelevant).
You start writing a question on StackOverflow, then realize you already know the answer and go to close the tab.
Normally, a confirmation window would come up asking if you're sure you want to leave the page.
But because of my extension, instead, the whole page's HTML gets shoved down a bit to make room for a temporary HTML confirmation box is shown with the same text and the same two buttons that yield the same two results when clicked, all in-page. No pop-up.
The feature you're talking about is the window.onbeforeunload event, and unfortunately the only way it can be customized is to provide a custom string message for the user, because the potential for abuse is so high.
The api reference over at msdn for Internet Explorer states:
The default statement that appears in the dialog box, "Are you sure
you want to navigate away from this page? ... Press OK to continue, or
Cancel to stay on the current page.", cannot be removed or altered.
I take this to mean the dialog itself cannot be altered, all you can do is provide an additional context-specific message. I cant locate the same reference for Chrome, but it seems to be the same story there.
window.onbeforeunload = function() {
//all you can do is provide a message..
return "you have unsaved changes, if you leave they will be lost";
}
Our application forbids going back for several reasons.
Basically because that's just how our application works (JSF with facelets as GUI)
You always have to enter on the welcome site, once you chose an application-flow you can only leave / abort when you tell the application (e.g. press a button). If you just browse away e.g. enter "example.com" in the address bar the state of your flow gets saved and once you relogin, you can resume the work. Going back is only possible when it was specifically designed like this with a 'back' submit - button.
Of course users keep pressing the 'back' button (i would do so as well) and they keep getting 'error: session out of synch'. This is a learning process and a couple years ago we just disabled the back-button to make things clear. Sadly this is no longer supported.
So instead of teaching the user the hard way and forcing him to relogin, are there some good alternatives I'm missing?
i found this link which should offer 3 methods to disable the back button - but in reality it just further confirms the fact that it is impossible to do it in a semi-nice way.
when the user tries to go to a previous page you can redirect him to the page he should be at in other words catch the "out of sync" and redirect him
You might find a workable solution here How do I insert an entry into browsing history via JavaScript
by inserting an extra step into the browser's history (perhaps a link to the current page with query string parameters that result in a nice big red box message to the user), or you could try attaching an event handler to the OnBeforeUnload event so the user gets a confirmation dialog when trying to leave the page (you'd want to remove the handler when the submit button was clicked).
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.