473,785 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.getEle mentById works in IE as expected, but not in Firefox

5 New Member
Hi All,

Expand|Select|Wrap|Line Numbers
  1. function getWindowSize() {
  2.   var myWidth = 0, myHeight = 0;
  3.   if( typeof( window.innerWidth ) == 'number' ) {
  4.     //Non-IE
  5.     myWidth = window.innerWidth;
  6.     myHeight = window.innerHeight;
  7.   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  8.     //IE 6+ in 'standards compliant mode'
  9.     myWidth = document.documentElement.clientWidth;
  10.     myHeight = document.documentElement.clientHeight;
  11.   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  12.     //IE 4 compatible
  13.     myWidth = document.body.clientWidth;
  14.     myHeight = document.body.clientHeight;
  15.   }
  16.   window.alert( 'Width = ' + myWidth );
  17.   window.alert( 'Height = ' + myHeight );
  18.  
  19.   //call resize
  20.     resizeContent(myHeight, myWidth)
  21. }
  22.  
  23. function resizeContent(windowHeight, windowWidth){
  24.     document.getElementById('content').style.height = windowHeight-107;
  25.     window.alert(document.getElementById('content').style.height);
  26. }
The function getWindowSize() is run at "onload" and "onresize" on the body tag.

It works as expected on Internet Explorer 7, however I also need it to work on Firefox. Basically the getWindowSize() function gets the innerWidth and innerHeight of the window and then calls the resizeContent function which resizes the DIV with the ID "content". this is required as the page is for an assignment and I need to rewrite an original frames page (i know, i know, why?????) and i want to make it exactly the same.

The window.alert in the resizeContent function shows that "document.getEl ementById('cont ent').style.hei ght" returns null for firefox and the correct value for IE.

Any help would be appreciated.

Cheers
Sep 4 '07 #1
5 7495
dmjpro
2,476 Top Contributor
Hi All,

Expand|Select|Wrap|Line Numbers
  1. function getWindowSize() {
  2.   var myWidth = 0, myHeight = 0;
  3.   if( typeof( window.innerWidth ) == 'number' ) {
  4.     //Non-IE
  5.     myWidth = window.innerWidth;
  6.     myHeight = window.innerHeight;
  7.   } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  8.     //IE 6+ in 'standards compliant mode'
  9.     myWidth = document.documentElement.clientWidth;
  10.     myHeight = document.documentElement.clientHeight;
  11.   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  12.     //IE 4 compatible
  13.     myWidth = document.body.clientWidth;
  14.     myHeight = document.body.clientHeight;
  15.   }
  16.   window.alert( 'Width = ' + myWidth );
  17.   window.alert( 'Height = ' + myHeight );
  18.  
  19.   //call resize
  20.     resizeContent(myHeight, myWidth)
  21. }
  22.  
  23. function resizeContent(windowHeight, windowWidth){
  24.     document.getElementById('content').style.height = windowHeight-107;
  25.     window.alert(document.getElementById('content').style.height);
  26. }
The function getWindowSize() is run at "onload" and "onresize" on the body tag.

It works as expected on Internet Explorer 7, however I also need it to work on Firefox. Basically the getWindowSize() function gets the innerWidth and innerHeight of the window and then calls the resizeContent function which resizes the DIV with the ID "content". this is required as the page is for an assignment and I need to rewrite an original frames page (i know, i know, why?????) and i want to make it exactly the same.

The window.alert in the resizeContent function shows that "document.getEl ementById('cont ent').style.hei ght" returns null for firefox and the correct value for IE.

Any help would be appreciated.

Cheers

Welcome to TSDN!
Please one thing Do.
Test the value of windowHeight or windowWidth passed as parameters to resizeContent function, using ....
Expand|Select|Wrap|Line Numbers
  1. alert(windowHeight+"\t"+windowWidth);
  2.  
Best of Luck.

Kind regards,
Dmjpro.
Sep 4 '07 #2
dmk
5 New Member
Welcome to TSDN!
Please one thing Do.
Test the value of windowHeight or windowWidth passed as parameters to resizeContent function, using ....
Expand|Select|Wrap|Line Numbers
  1. alert(windowHeight+"\t"+windowWidth);
  2.  
Best of Luck.

Kind regards,
Dmjpro.
Yes, this does work, however it is not what is wrong, you may notice that in the resizeContent function I have the following line

Expand|Select|Wrap|Line Numbers
  1. window.alert(document.getElementById('content').style.height);
This is returning null in Firefox and the correct value in IE... this is where the problem is occuring.
Sep 4 '07 #3
dmjpro
2,476 Top Contributor
Yes, this does work, however it is not what is wrong, you may notice that in the resizeContent function I have the following line

Expand|Select|Wrap|Line Numbers
  1. window.alert(document.getElementById('content').style.height);
This is returning null in Firefox and the correct value in IE... this is where the problem is occuring.
Actually that's why i told you to check before set the value of Height.
Now without set the height you check your own alert, and see what it returns.
If windowHeight and windowWidth returns numeric digit then it would be no problem.
If the windowHeight is String then try to covert it using parseInt.

Kind regards,
Dmjpro.
Sep 4 '07 #4
dmk
5 New Member
Yes, this does work, however it is not what is wrong, you may notice that in the resizeContent function I have the following line

Expand|Select|Wrap|Line Numbers
  1. window.alert(document.getElementById('content').style.height);
This is returning null in Firefox and the correct value in IE... this is where the problem is occuring.
This issue was solved because firefox does not automatically add "px" to the end of the value entered while IE does.

The code was changed to the following:

Expand|Select|Wrap|Line Numbers
  1. function resizeContent(windowHeight, windowWidth){
  2.     pageElement = document.getElementById("content");
  3.     calcHeight = windowHeight - 113;
  4.     pageElement.style.height =  calcHeight + "px";
  5. }
Note that you do not need to write it out on two lines (i.e. you can use document.getEle mentById("conte nt").style.heig ht if you want to.
Sep 4 '07 #5
gits
5,390 Recognized Expert Moderator Expert
hi ...

yep ... this is due to the correct behaviour of firefox here ... the units has to be applied by the programmer ... and IE guesses that you wanted 'px'. So the best way - and i would always recommend that - is to develop with FF/MOZ and then test it in other browsers too ...

initial development with IE mostly gets you to adapt the entire code for standards-compliant browsers (you find a lot of this problems here in the forum too) in case you use IE-specifics in your code ... but using the standards methods that the other browsers support ... will mostly work in IE too ... and you have much less adaptions to do ...

conclusion: develop to the standards and test with standards-compliant browsers such as firefox ... after that ... get it to work with IE and other browsers too ... that will save you a lot of time ... i promise ;)

kind regards
Sep 4 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
1832
by: bradwiseathome | last post by:
I have code that works in IE 6 but does not work in FireFox 1.0.2, how can I change it so it works in both browsers? <html> <head> <script language="JavaScript" type="text/JavaScript"> function AttributeSelected() { var selectVal = document.forms.elements.options;
7
2408
by: Andrea | last post by:
Hi there - I'm hoping someone can help me; I've been struggling with this for a few days! :-) I have a webpage that is comprised of many forms containing questions. As the user answers one of the questions, that form is hidden and the next is displayed. Also, as the user answers each question a "count" variable is updated based on their response. I would like the question to show up on the left side and the answer to show up in the...
2
2240
by: Phlip | last post by:
JavaScripters: Steps to reproduce the bug: Save this in "outer.html": <div id='updateMe'></div> <iframe src='inner.html'/> Now save this into "inner.html":
13
4966
by: RommelTJ | last post by:
Hi, My website (http://www.justiceinmexico.org/indextest.php) looks good in Firefox, but horrible in IE, and I think it's because of an error in the javascript of a free web ticker I got off the internet. When I run Firebug on it, it says: document.getElementById("TICKER") has no properties TICKER_CONTENT = document.getElementById("TICKER").innerHTML; Here is the complete script:
4
1483
by: smartic | last post by:
Hi everyone. I'm having problem with firefox only but it works when i delete document definition that is my code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head>
2
13905
by: jmatos | last post by:
I need to change the style of an element changing its padding-left property (got it in firefox but IE is a different battle..). I can use document.getElementById().style.XX to everything i need, except with padding-left, as it is separated by a ' - ' and it is not correct to use it as it was supposed to: document.getElementById('el').style.padding-left = '5px' will throw 'invalid assignment left-hand side', as expected.. Any help...
29
19290
by: Nick | last post by:
I've seen a few frameworks use the following: function $(id) { return document.getElementById(id); } Then to use: $('something').innerHTML = 'blah'; I'm just trying to roll this out to my site and so far doing this has saved about 8KB of javascript (lots of ajax/dynamic elements). I just
2
3922
by: vegetable21 | last post by:
Hi, I'm relatively new to JavaScript, but not to programming. I'm trying to get a table to expand and collapse within a cell of my master table. However i want to collapse all the other open tables when i expand a new one. I'm nearly there but i get an error that i can't seem to shake. Firefox show me the error as the following: Error: document.getElementById("more" + i) is null Source File:...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.