onTab Change event in Javascript - javascript

Is there any onTab change event in javascript . Like whenwver the user is moved to new tab then an event is fired .
I have tried onfocus , onblur ,add listener but they are behaving differently in different browser.
So is there any event that can be created when the tab is changed in java script ?
I tried the following links
Event for when user switches browser tabs
Also tried the
https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
but again the events are not proper

The focus/blur events should work,
but I suggest you listen on window and not document (if you did):
if (document.addEventListener) {
window.addEventListener('focus', onFocus, 1);
window.addEventListener('blur', onBlur, 1);
} else {
// For IEs older than IE10
document.attachEvent('onfocusin', onFocus);
document.attachEvent('onfocusout', onBlur);
}
If this really doesn't work, the Page Visibility API should be covered enough and work properly. If you want to support some older browsers or just want to make sure to cover all possible cases, I recommend you use visibly.js by Addy Osmani.

Related

Chrome for iOS (iPad/iPhone) does not fire focus event for inputs

I have a simple detection if a software keyboard is opened on Android and iOS:
$(document).on({
focus: function() { keyboardOpened = true; },
blur: function() { keyboardOpened = false; }
}, 'input'); //catch all focus and blur events fired on any input element
It's used to prevent certain actions while user is writing a message (e.g. ignoring resize events).
It works on all browsers except for Chrome on iOS. I've included Firebug Lite on the site but there is no error, just the events are never fired and the web does not work correctly when opening SW keyboard.
Here is a fiddle: http://jsfiddle.net/WYULR (click the input and see an alert)
Anyone knows about a reason or workaround for this?
I get this too :( My solution is to detect if ipad + chrome using :
var isCriOS = /CriOS/.test(navigator.userAgent);
I tried https://github.com/gabceb/jquery-browser-plugin + webkit type distinguishing code found here How to detect chrome and safari browser (webkit) but I couldn't get that to work.
If ipad+chrome, apply a 'click' event for the field to do the same thing ... this is only good if the only focus you will have is from user taps, as calling .focus() will not trigger it ... but in this case you could just call the function ascribed through click event after the .focus() . This seemed a bit hackish so I decided to test vanilla js .onfocus event and proved that it wasn't firing either.

Determining the correct Android WebView screen size after soft keyboard hides with JavaScript?

I'm working on a "Mobile Web" app, and relying completely on javascript to solve this problem. On certain Android devices, most notably the Samsung Galaxy family, a window.resize event fires when the soft keyboard appears, shortening the height of my screen.height. When a user taps the "Go" button of the soft keyboard, my app reloads with a height that is less the height of the soft keyboard.
Now, I am listening to the window.resize event, so if the user rotates the device, everything will right itself. However, I'm hoping to figure out a way to fire off the window.resize event programmatically, so that every action performed when I rotate the device is triggered.
Can this be done?
if you're using jQuery mobile use jQuery trigger()
if not use this code from a stackoverflow question:
You can use fireEvent on IE, and w3c's dispatchEvent on most other
browsers. To create the event you want to fire, you can use either
createEvent or createEventObject depending on the browser.
Here is a self-explanatory piece of code (from prototype) that fires
an event dataavailable on an element:
var event;
if (document.createEvent) {
event = document.createEvent("HTMLEvents");
event.initEvent("dataavailable", true, true);
} else {
event = document.createEventObject();
event.eventType = "ondataavailable";
}
event.eventName = eventName;
event.memo = memo || { };
if (document.createEvent) {
element.dispatchEvent(event);
} else {
element.fireEvent(event.eventType, event);
}
Update:
There is a fireEvent() for IE and dispatchEvent() method for non IE browser. You can manually fire your events. The trick is to create an event just like window.resize and fire that. I never tried this. But I hope that can help.
I had this same problem and abandoned trying to fire the events and went with modifying the meta viewport after keyboard has gone. See answer here: https://stackoverflow.com/a/11321889/1500269

javascript onchange vs onkeyup events for <Select> menu (Up and Down keys don't fire onchange event in Firefox)

I have a <select id="myselect" name="myselect" onChange="(myfunction();)">...</select> which works perfect in IE and Opera. The word "perfect" means the event fired when you change the values from the drop-list by mouse or by any of "Up", "Down", "PageUp"(not for Opera), "PageDown"(not for Opera), "Home" and "End" keys when select menu is active (blue). The problem appears when you test it using Firefox, 3.6.Xv. Nothing happens when you use "Up" and "Down", but for mouse it still works.
Do you recommend to use onkeyup event? I've tried it, it "catches" up and down, but, IE appears to have both onChange and onkeyup event. But I need just one event.
How do people solve this issue?
Thank you.
I recommend that you keep using the change event. The Firefox implementation makes lots of sense for keyboard users. If you tab to the select element and choose an entry using Up and Down keys (and you have to press them a lot for a lengthy list) you don't want to trigger tons of actions on the web page. It is ok to have the action executed once you've selected the correct entry and moved on to something else.
This is a pretty dirty hack, but you can force the the change event to fire by doing this:
element.addEventListener('keyup', function(evt){
evt.target.blur();
evt.target.focus();
}, false);
So you'd register an event listener for change as well, and that function would get called when the user presses a key on the <select> via the code above.
You may want to scope this only to Firefox, but AFAIK you'd have to use UA sniffing for that so it's up to you if that's acceptable.
Source
You could be clever and make your own handler for the keyup event which tests the keycode to see if it was an up arrow or down arrow, and fires the change event accordingly.
My own js isn't good enough to write you an example but I could show some example jQuery to do that:
$('yourSelect').keyup(function(e)
{
if (e.keyCode===38)
{
//this is an up arrow press
//trigger the change event
$('yourSelect').change();
}
else if (e.keyCode===40)
{
//down arrow has pressed
//trigger the change event
$('yourSelect').change();
}
});

Mobile Safari Event Issue

I have designed a website with a menu that is initially invisible. When the user clicks on a button, the menu becomes visible. There are two ways for the user to hide the now visible menu:
Click the button that caused the menu to become visible
Click anywhere on the web page that isn't the menu
The way I have coded the second option is to tie an onclick event to the window element, and have it compare where the user clicked to the menu's position to determine if the menu should be hidden. This works great in Firefox and Safari, but it fails in Mobile Safari.
I noticed that the window onclick event only fires when I click on another element with an onclick event already assigned. If I click on an element with no event(s) assigned, the window's onclick event never fires. If I click on the button which displays the menu, it fires along with the event tied to the button.
Is it possible to assign events to the window element in Mobile Safari?
I'v been encountering this same problem. Here is what worked for me. (Note: I am working within a Modernizr and jQuery context)
First, I add a custom Modernizr class using Modernizr's addTest Plugin API to test for iOS, which will add the class appleios or no-appleios accordingly.
Because in my research the body seems to fire events on it's own agenda, I am taking a little precaution by wrapping all the document's content with an element in an iOS context. Then I add an event handler to this element.
$(".appleios body").wrapInner('<div id="appleios-helper" />');
$("#appleios-helper").bind("mouseup", function(){return;});
What was suggested earlier in this thread is using void(0). I did some quick testing, and found that void(0) as the event just wasn't causing touches on the body to be recognized. When I plugged in my own "empty" function in the form of function(){return;} things started working.
This all hinges on the fact that no events are fired in Mobile Safari unless the element explicitly has events to fire (Safari Web Content Guide.) By inserting this empty event on the wrapper, things will bubble up to the body.
If you're doing strait JavaScript with none of these libraries, the same effect could be achieved in the HTML markup
<html>
...
<body>
<div id="appleios-helper" onmouseup="function(){return;}">
...
</div>
</body>
</html>
This worked for me to hide tooltips when touching anywhere on the document's body. Your mileage may vary.
Simply adding the dummy onclick handler to the html body works for me:
<body onclick="void(0)">
Note that I am using usual live event handlers as shown below:
function liveHandler( event ) {
var target = event.target; ...}
window.addEventListener(evtype, liveHandler, true);
// evtype such as 'mousedown' or 'click'
// we use the capturing mode here (third parameter true)
This is an old question, but I struggled with the same thing today.
I found that using touchstart event works.
I solved it like this:
var isTouchDevice = 'ontouchstart' in document.documentElement;
if (isTouchDevice) {
// Do touch related stuff
$(document).on('touchstart', function (event) {
// Do stuff
});
} else {
// Do non-touch related stuff
$(document).on('click', function () {
// Do stuff
});
}
You could just add onclick="void(0);" to some <div> that covers the whole page so that no matter what, you are always clicking on an element that has an onclick event. Not a great solution, though.
I'd prefer not having the onclick event be tied to the window. Why don't you create a container <div> that has that event on it. Then handle it just like you currently are.
You can also:
$('body').css('cursor', 'pointer');
No idea what those "engineers" at Apple are doing. LOL.
This has problems though. You wouldn't want to do this on every touch device. Only touch devices that don't also have a pointing device (Laptops with Touch Screens, for example).
Source: http://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html
The conclusion of the article is this:
So I don’t understand why all this is the case, but it most certainly is the case. If you’re having bubbling problems, just add an empty-function event handler anywhere between the body and the element, and you’re set to go. But it shouldn’t be necessary.

Event for when user switches browser tabs

I'm looking for an event which will fire whenever the user switches away from the page to another tab, and another event which fires when the user switches back to the tab again.
window.onblur and window.onfocus don't seem to work correctly across all browsers
Is there a proxy I could look at in order to synthesize this event?
You can also try and use VisibilityAPI.
document.addEventListener("visibilitychange", function() {
if (document.hidden){
console.log("Browser tab is hidden")
} else {
console.log("Browser tab is visible")
}
});
See also here on Stackoverflow (possible duplicate)
You might try using a framework, such as MooTools or jQuery which provide cross-browser support. They should be able to detect with more reliability the blur and focus events for the browser window.
I personally have used jQuery with much success:
$(window).blur(function(e) {
// Do Blur Actions Here
});
$(window).focus(function(e) {
// Do Focus Actions Here
});

Categories

Resources