Detect If Browser Tab is focused on mobile browsers - javascript

I am willing to detect if the tab is in focus on as many mobile browsers as possible.
I saw that the common use in determining visibility is "document.hidden" or "document.visibilityState", but on https://developer.mozilla.org/en-US/docs/Web/API/Document/hidden#Browser_compatibility is listed that it's not compatible on IE mobile.
Found also that many use document.addEventListener("visibilitychange", function() {}) - which also seems to might not be compatible on IE Mobile.
Another less common use is document.hasFocus() which is not supported in Opera Mini and might not be supported in Safari and Mobile Chrome..
plus, the "window.onblur" doesn't seem to work on chrome emulator.
Anyone knows maybe another way to check if the browser is in focus on all mobile browsers? or maybe one of the above is actually compatible to all?

Have you tried using the two events:
window.onfocus and window.onblur?
You can associate code which triggers when it focuses and it blurs, so actually you can detect when it's not focused

Related

window.focus() does not work for existing tabs in ALL IOS browsers

I'm trying to create a "re-launch" button that should bring the user to an existing opened tab. So far, this is how it's implemented:
Using window.open() initially to create the tab and focusing to that tab using window.focus().
The relaunch button should basically call window.focus() and redirect to that tab.
This has been asked in $window.focus() doesn't work on existing tabs in iOS Safari but I'd like to get a more general overview on this question. I've looked everywhere but I couldn't find any lead to this question.
Apparently, it works in android browsers but I can't get it to work in ios browsers (safari, chrome). Does anyone have any clue about this issue?
This seems to be a bug in iOS browsers. As a workaround you may replace
focus() by a sequence of close() and open(...).

How best to fix Chrome datalist rendering

You may be aware that Chrome has problems with datalist/option autocompletes.
Essentially whilst it works in IE, Chrome doesn't allow you to scroll through the list and the list runs out of the browser into the main window.
My question is whether someone can suggest a good method of overriding the Chrome implementation, and what to override it with. Solution has to work in main modern browsers and mobiles
I had the exact same problem. I searched for 2 days and ended up using https://jqueryui.com/autocomplete/
It is far better than datalist.

JS library to simulate Internet Explorer?

There are some JS library to simulate the IE in Webkit? For instance: IE8 doesn't support border-radius currently (maybe on IE10). So I run a JS library that check if I'm using the border-radius then remove it to make similar to IE vision.
It's a crazy idea, I know, but work on IE is too slow, and if I can simulate it on Webkit, will be great! I need do it to an own project too.
Note: I know that exists a Chrome extension to make a IE tab, but the proposal is different: I need run on Webkit, but eliminating features not supported on IE.
Edit1: I'm working on a Webkit based IDE to developer HTML templates. Basically I need a button that emulate the IE view version without need a IE installed too (Mac, Linux for instance). Will be interesting have a Mozilla emulator too, for instance. Basically I will generate a CSS file to each browser too. For instance: -webkit properties not will be included to MSIE CSS. filter not will be included in Webkit CSS (but can be emulated).
So, I'll make a copy of current HTML page before apply a JS method that will edit or delete the unsupported content, make it similar to IE. If IE8 not support border-radius, it'll be removed and I'll see basically an IE version of page. If Mozilla not support -webkit-box-sizing it will be renamed to -moz-box-sizing if disponible.
I know of no script, and frankly I don't expect to see one any time soon as the task of re-creating the topography of IE's support for CSS in various versions of the browser would be a massive undertaking. It would further complicate things if the undocumented hacks like _height were supported too.
Frankly, the best way to test your site in IE8 is to use IE8+. In versions IE8 and up Microsoft introduced Browser Emulation, permitting you to fallback and emulate any number of IE versions all from a single browser - I use this daily, and it's a fantastic feature.
Within IE, open up the Developer Tools by pressing F12 and from there you can change the Browser Mode to IE8. No refresh will be necessary as the browser will handle that on its own. Using this method you can quickly test versions 7 through 10 (assuming you're testing from IE10) with the click of a button.
Disabling CSS3
If all you would like to do is disable CSS3 features, you can use the Strip Tease bookmarklet. It's not a fully-developed solution, so keep in mind that it won't handle things like advanced selectors, etc.

Does IE9 have browser mode down to IE7 and does this mean different version of Javascript

I'm having some trouble setting up virtual machines for testing IE versions (I use a mac) and have noticed on IE8 it has a browser switching mode.
Does this mode do a full switch for CSS and Javascript, I need testing to be 100% reliable.
Also does IE9 have a similar feature going down to IE7?
It's not going to be 100% reliable, I can tell you that right now. The only way to really test on IE7 is to test on IE7. For most layout checks, using the newer browser in emulation mode will be fine, but there are definitely bugs and oddities in the actual older browsers that the newer browsers don't mimic faithfully.
When you change The browser mode from the IE developer tools, IE renders the site according with your selection.
IE 9 also has this option, and if say you pick IE7, you'll get pretty much the same expercience you would get in that browser (from a layout and functionality point of view), but it's not completely reliable, some Javascript quirks are not the same (I can't remember any specific ones)
If you really need to test in IE7, get IE7 :D
More info here!
Press F12, and in the developer tools toolbar change browser mode to IE7 and Document Mode: IE7 (if you change the browser mode, then doc mode automatically changes also).
As for whether you get a different Javascript API? I don't know.
I agree with #Pointy, though. Realistically you actually need to run IE7. A VM is often a good way to do this if you don't want to muck about getting the different versions running side-by-side.

onbeforeunload in Opera

I'm using the code that netadictos posted to the question here. All I want to do is to display a warning when a user is navigating away from or closing a window/tab.
The code that netadictos posted seems to work fine in IE7, FF 3.0.5, Safari 3.2.1, and Chrome but it doesn't work in Opera v9.63. Does anyone know of way of doing the same thing in Opera?
Thx, Trev
Opera does not support window.onbeforeunload at the moment. It will be supported in some future version, but has not been a sufficiently high priority to get implemented as of Opera 11.
onbeforeunload is now supported in Opera 15 based on the WebKit engine but not in any prior versions based on Presto.
Have you tried this?
history.navigationMode = 'compatible';
Reference, found via this page
I haven't actually tried it myself, but it looks promising.
Mobile Safari (iPhone/iPad) also doesn't support onbeforeunload, and I strongly suspect it is not likely to.
For detecting back/forward navigation there may be workarounds e.g. see Is there an alternative method to use onbeforeunload in mobile safari?.

Categories