So, the problem I ran into is this: my Firefox mobile browser does not retrieve the correct values for window.innerWidth , document.documentElement.clientWidth or even the width of the style div to cover the entire client window after the page loads.
I'm not crazy, my code works fine in any other browser! For some reason, Firefox initializes these defaults and then gets the correct values later. If at any time I interrupt my JavaScript with alert() , these properties will magically become accurate afterwards.
I browsed the internet for an answer, and all I can find is a workaround: use window.setTimeout to delay the use of these properties until they have time to fill out correctly. This is crazy! Users want speed, not an extra delay, just to browse my site in Firefox.
I do not understand that I can set the div to fill the client window before the values become accurate. I do this in css, setting the width and height of my id div to 100%. document.documentElement is basically the same as document.getElementById("my_div"); after loading all the elements of the document, so how does the browser know how big the div should be when it does not have the right size of the client window in the first place?
I tried to run my code inside window.addEventListener("load",function(event_){ //My Code }); but these values will not be generated. Is there a page load event that appears after window.onload ?
If someone tells me why only the Firefox mobile seems to display this strange behavior, I will give you a mental five.
Here are some sample code to recreate the problem:
<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> window.addEventListener("load",function(event_){ var output=document.getElementById("output"); output.innerHTML=window.innerWidth; alert("After this alert, the value will change."); output.innerHTML=window.innerWidth; }); </script> <title>Title</title> </head> <body> <p id="output">Default Output</p> </body> </html>
My Firefox for the Android version is 35.0.1. My version of Android is 4.4.4. On my device, Firefox displays "980" in the output element p, displays a warning, and displays "980" again. After updating the page, the first two steps remain unchanged, but the output after the warning changes to 360. This also happens with document.documentElement.clientWidth . There are no properties that I am trying to get in order to get the correct values. It seems that Firefox has some delay after loading the page before it gets access to the client window sizes ...
I tried the verge.airve.com plugin without jQuery and its initial feedback remained at 980. It was also initialized as 980 on Chrome, which was strange because Chrome worked as expected without it ...
After much debate, a solution was found! Firefox apparently resizes the window after it loads (I think for a good estimate, who really knows)! Thus, by adding a resize event handler in addition to window.onload, this problem can be prevented! See the accepted answer below for more details.