$( window ).on( 'orientationchange') firing on desktop? - javascript

im seeing $( window ).on( 'orientationchange') firing on desktop on Chrome , is that expected behaviour or does that mean some other part of the code im working with is actually dispatching this event?

As far as I can tell the javascript is only detecting when the height grows greater than the width and vice versa. So when you change your browser size to a height > width it will launch the event. This seems to be the expected behavior of the function, I am not sure how to circumvent that for your use.
EDIT: I found that when you check the window.orientation property when the event is fired you can see that it is still set to landscape. Maybe try filtering your landscape only code by putting it inside an if statement? See http://jsfiddle.net/sJ9Hx/2/
$(window).on('orientationchange', function (e) {
if(window.orientation == 0){
alert("Portrait");
}
else{
alert("Landscape");
}
});

The orientation changes when the height and width values are altered!
Lets say when 840x600 becomes 600x840 you can say that the device's orientation has changed. That way the OS would detect and would execute the co-responding function. But wait, there are many other methods of detecting that, using sensors too!
If you're getting that this event is getting executed, then of-course there is some sort of JS file there! Which is causing this event. For this, you need to check the JavaScript code and see how it detects the orientation change and then do what so ever action regarding that!

Related

Is there any way to set a resize function when screen width changes and it triggers a javascript function

Actually i have a hover function which triggers on mouse hover and i want to work this function only on screen size > 768 px as on touch screen there no point to have this function. I have achieved this by adding an if statement in in a javascript function as follow:and placing my jquery inside this function.
function hoverFunction() {
if ($(window).width() >760 ){
}
}
now this hoverFunction() is called twice once onchange and one onwindow load. Now lets talk about the problem. When i load the page and using developer tools move from large screen seize to small the hover function doen not disable even below 768 px until i refresh the page or i trigger the onchange function. it happens same when i move from small to large screen size, i have to either refresh the page i use onchange. is there any way to over come this issue.
Yes you can do it with window.onresize, check the example here:
https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event
you need to attach it on resize event, it will trigger on every screen size change
Please visit below link , this is how you can trigger your custom function using jquery
https://stackoverflow.com/a/15114048/7300804
You can just use window.addEventListener('resize', reportWindowSize); For this event reportWindowSize will be triggered lot of times thus you should use debounced[https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf] reportWindowSize. for debouncing in javascript you can refer this[https://lodash.com/docs/4.17.15#debounce]

javascript: detect when the appearance of the document has changed

Is there a way to detect when the appearance of the document has changed? For example:
$(document).on('change??', function () {
console.log('My aspect has changed');
});
// changes the appearance of a particular div
$('#my-div').css({width: 320, height: 240});
Also, is there a kind of 'render' event? This event would be fired every time the browser redraws the page. For example:
$(document).on('render', function () {
console.log('The page has been redrawn');
});
Don't know of any event like that, but if you are in control of all the changes you can easily cause your own events.
Here is a simple JQuery based example to illustrate the idea.
$( "#test" ).on( "mysitechanged", function( event, param1, param2 ) {
alert( 'changed' );
});
$( "#test").trigger( "mysitechanged", [ "Custom", "Event" ] );
http://api.jquery.com/trigger/
Here is info on how to do it in pure JavaScript
How to trigger event in JavaScript?
Since i think you're trying to detect whether the aspect has changed or not, this is what you're looking for.
window.onresize = function(){
// your code goes here;
}
Here is a good example of how the window.resize event is handled: http://jsfiddle.net/CoryDanielson/LAF4G/
I am on the lookout for that too. Apparently not. However it should be possible to implement one.
As far as I know, for the document to change appearance, one of the following is true:
The DOM has changed
The viewport changed (resize, orientation change, media changed, etc.)
You can detect changes to the DOM using MutationObserver, and detect viewport changes using native JavaScript events (onresize, etc.).
I am not sure whether this is necessary or useful (events should cover all realistic cases), but window.matchMedia(media).addListener() will also notify you about media changes to the viewport.
A DOM or viewport change does not necessarily imply a change of appearance, so then you should do some measurements on the page to verify whether anything changed.
More realistically and pragmatically, assuming that you build the page or app, you should be able to predict what would cause a change of appearance (ex: new content loaded through ajax, the user typed something in or clicked a button, new message received, page resized, etc.) and watch for these actions instead.

Javascript Custom Resize Event [duplicate]

This question already has answers here:
How to detect DIV's dimension changed?
(28 answers)
Closed 7 years ago.
I have a very simple question, or at least it seems that way.
I have a DIV element which will be resized at one moment. I want to be able to capture the resizing moment.
Something like this:
function myFunction(){
alert('The DIV was resized');
}
divElement.addEventListener("resize", myFunction, false);
Does anyone know the answer?
Thanks
As of December 2011, there's no built-in event to detect when a div resizes, just when a window resizes.
Check out this related question: Detecting when a div's height changes using jQuery, and this plugin from the solution to that question: http://benalman.com/projects/jquery-resize-plugin/
With jQuery resize event, you can now bind resize event handlers to
elements other than window, for super-awesome-resizing-greatness!
Why is a plugin needed for the resize event?
Long ago, the powers-that-be decided that the resize event would only
fire on the browser’s window object. Unfortunately, that means that if
you want to know when another element has resized, you need to
manually test its width and height, periodically, for changes. While
this plugin doesn’t do anything fancy internally to obviate that
approach, the interface it provides for binding the event is exactly
the same as what’s already there for window.
For all elements, an internal polling loop is started which
periodically checks for element size changes and triggers the event
when appropriate. The polling loop runs only once the event is
actually bound somewhere, and is stopped when all resize events are
unbound.
Sample Code
// You know this one already, right?
$(window).resize(function(e){
// do something when the window resizes
});
// Well, try this on for size!
$("#unicorns").resize(function(e){
// do something when #unicorns element resizes
});
// And of course, you can still use .bind with namespaces!
$("span.rainbows").bind( "resize.rainbows", function(e){
// do something when any span.rainbows element resizes
});
You can try this plugin - http://benalman.com/code/projects/jquery-resize/examples/resize/
There are various examples. Try resizing your window and see how elements inside container elements adjusted.
Example with js fiddle
In that resize() event is bound to an elements having class "test" and also to the window object and in resize callback of window object $('.test').resize() is called.
e.g.
$('#test_div').bind('resize', function(){
console.log('resized');
});
$(window).resize(function(){
$('#test_div').resize();
});
See this
My first thought is to use a custom event system. You can find a pure javascript one here ( http://www.nczonline.net/blog/2010/03/09/custom-events-in-javascript/ )
After including his code, you can do something like this:
function myFunction(){
alert('The DIV was resized');
}
div_elm = document.getElmentById('div-to-resize');
EventTarget.call(div_elm);
div_elm.addListener("resize", myFunction);
Then later, just add one line to wherever you are resizing the div.
div_elm.width += 100 //or however you are resizing your div
div_elm.fire("resize");
I think that should work for you.
EDIT:
If you are not the one coding the resizing, then my first thought is something like this:
var resizeScannerInterval_id = (function(div) {
var width = div.offsetWidth;
var height = div.offsetHeight;
var interval_id = setInterval(function() {
if( div.offsetWidth != width || div.offsetHeight != height )
width = div.offsetWidth;
height = div.offsetHeight;
div.fire();
}
},250);
})(document.getElementById('div-id'))
There is a very efficient method to determine if a element's size has been changed.
http://marcj.github.io/css-element-queries/
This library has a class ResizeSensor which can be used for resize detection. It uses a event-based approach, so it's damn fast and doesn't waste CPU time.
Please do not use the jQuery onresize plugin as it uses setTimeout() loop to check for changes. THIS IS INCREDIBLY SLOW AND NOT ACCURATE.

$(window).scrollTop tied to $(window).scroll() event acting oddly

I'm trying to trigger a function when the window is scrolled more than a certain number of pixels.
Here's my code:
$(window).scroll(function(){
if( $(this).scrollTop() >= 100 ) {
someFunction();
} else {
someOtherFunction();
}
});
It kinda works, but there's either a delay of around 2-4 seconds after scrolling before the function(s) are fired or else the functions aren't triggered at all.
Tried it out in Safari / Chrome. Don't know if that helps or not!
The code looks fine and works for me.
As Wolfram says, it's rarely a good idea to attach handlers directly to the scroll event, as this fires a lot and can bring the user's system to a crawl.
I'd recommend using Ben Alman's jquery throttle/debounce plugin.
It works using jQuery 1.6.1 + mousewheel / scrollbar in Chrome15/Safari5.1/FF7 on OSX. What are you doing in those two functions? For testing, I put a simple alert() in someFunction and nothing in someOtherFunction.
Remember that one of those functions is executed every time the scroll-event fires unless you stop it once it was called... e.g. someFunction is called a lot after you scrolled below the 100px line.
John Resig: It's a very, very, bad idea to attach handlers to the window scroll event.
If you're scolling by holding in the click-button instead of using the scroll wheel, I believe the event won't fire until you release the click-button.
Have you considered running a loop that checks the scrollTop instead?
EDIT:
I just check an old project of mine using window scroll event, and it runs perfect with the same event.
I asume you have this script of yours wrapped inside:
$(function() {
// code
});

Javascript - Maximize and tab switching events are not called in Firefox using focus

I am facing a weird behavior and I need some help..
I am trying to catch every tab switching event and every maximize event (meaning: whenever my window is shown), using the following code:
window.addEventListener("focus", function (e) { this.foo(e); }, false);
window.document.addEventListener("focus", function (e) { this.foo(e); }, false);
However, It doesn't catch maximize event! How can I tell whenever a user maximizes my window after a minimize?
Also, it seems redundant to me to have both window.addEventListener("focus") and window.document.addEventListener("focus") but I found out that when switching between tabs, the selected document it focuses on might not be the content I'm interested in, so that's why I'm using window.focus.... However it doesn't always fire this event for window, so I must listen to both focuses... What do you think?
Can you please help me understand where's my problem? I need to create an event that fires on every tab switching \ maximize - and for every document inside my window so that it'll process my content?
Thank a lot,
Nili
You can use window.onresize event, if window.outerHeight === screen.availHeight && window,outerWidth === screen.availWidth, it's maximized.
But outerHeight/Width DOES NOT sopport IE8-, see http://www.javascriptkit.com/domref/windowproperties.shtml
Updated:
For IE8- hacks:
If you can sure the user can accept to maximize window size to maximum, you can use window.resizeTo(screen.availWidth, screen.availHeight) and record the document.documentElement.clientWidth and document.documentElement.clientHeight to compare if it's maximized for later.

Categories

Resources