472,372 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,372 software developers and data experts.

id and problem in Firefox

In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).

So, my question is: is there anyway to make the script work in all
browser *without* any browser fix ie, without adding any browser
detection check? Is this behavior is only for Firefox or any other
browsers also work like this (I don't have access to many browsers to
test)? TIA

<snippet>

<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateMonths()
{
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)
{
opt = new Option();
opt.text = i;
opt.value = i;
month.options[i-1] = opt;
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<!--month options-->
<SELECT name="month" id="month">
</SELECT>
<!--calling script-->
<SCRIPT>
PopulateMonths(); //calling populate for the above options
</SCRIPT>
</BODY>

</snippet>

--
Email: rrjanbiah-at-Y!com
Jul 23 '05 #1
6 2362
R. Rajesh Jeba Anbiah wrote:
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).
<snip> <HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateMonths()
{
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)
{
opt = new Option();
opt.text = i;
opt.value = i;
month.options[i-1] = opt;
}
} <snip> <SELECT name="month" id="month">
</SELECT>
<!--calling script-->
<SCRIPT>
PopulateMonths(); //calling populate for the above options
</SCRIPT>


Odd, similar works OK for me across both IE and Firefox. Maybe IE is getting
upset because your assigning the widget to a variable with the same name as
an HTML Element in the same DOM scope? Try enclosing the select in
<form>...</form> or change the JS variable name.

HTH

C.
Jul 23 '05 #2
R. Rajesh Jeba Anbiah wrote:
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below). [snip]

<snippet>

<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function PopulateMonths()
{
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)

[snip]

I have no problem using code like yours in MSIE - perhaps you have an
older version of IE where the problem arises? Certainly in modern
versions, the W3C standard DOM and object methods like getElementById
sholud work perfectly well.

Don't bother to "detect browsers" though - check the objects and methods
themselves so you can do something like:-

if (document.getElementById) month = document.getElementById ("month")
else {
if (document.all && <your MSIE object/method>) {
// do MSIE code here
}
}
Jul 23 '05 #3
> <SELECT name="month" id="month">

Also, I'd make these variables different.

Robert
Jul 23 '05 #4
DU
R. Rajesh Jeba Anbiah wrote:
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).

So, my question is: is there anyway to make the script work in all
browser *without* any browser fix ie, without adding any browser
detection check? Is this behavior is only for Firefox or any other
browsers also work like this (I don't have access to many browsers to
test)? TIA

<snippet>

<HEAD>
<SCRIPT LANGUAGE=javascript>
<script type="text/javascript">
<!--
function PopulateMonths()
{
//month is id used with select
But you also use name="month" in your code. So, there is a confusion
already here.
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)
{
opt = new Option();
opt.text = i;
opt.value = i;
month.options[i-1] = opt;
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<!--month options-->
You need to insert <form action=""> element here.
<SELECT name="month" id="month">
</SELECT>
<!--calling script-->
<SCRIPT>
PopulateMonths(); //calling populate for the above options
</SCRIPT>
</BODY>

</snippet>


I would validate your HTML markup code first of all, preferably with a
strict doctype decl. because that will trigger MSIE 6 into web standards
compliant rendering mode.

List of valid DTDs you can use in your document.
http://www.w3.org/QA/2002/04/valid-dtd-list.html

Like someone else in the thread said, you first need to embed that
<select> inside a <form>. <form> also requires an action attribute.

Your populate select function could be streamlined a bit and clarified also:

function PopulateMonths()
{
var objMonthSelect;
if(document.getElementById)
{
objMonthSelect = document.getElementById("month");
}
else if(document.all && !document.getElementById) // MSIE 4 only
{
objMonthSelect = month;
};
objMonthSelect.options.length = 0; //clear options
for(var i=0; i<12; i++)
{
opt = new Option();
opt.text = i;
opt.value = i;
objMonthSelect.options[i] = opt;
};
}

You should also always use differential (and preferably meaningful)
identifiers for name attribute and id attribute for several reasons:
code reviewing by others, better discrimination of how your code
accesses nodes, better understanding of your code inner logic. <select
name="month" id="month"> just confuses the way certain browser actually
accesses nodes when one does
month.[some attribute]

Since above 96% of all browsers in use out there support flawlessly
getElementById method, then there is no problem with replacing
month
by
document.getElementById("month")
with proper object support for that method

Finally, I recommend you always call functions like PopulateMonths() on
the onload event of the body element, not as the document is being
parsed and rendered for several reasons.

DU
Jul 23 '05 #5
Colin McKinnon <co**************@andthis.mms3.com> wrote in message
In IE, I could be able to directly refer the "id", but it isn't
possible in Firefox. Somewhere I read the solution is to refer the id
like document.getElementById("month") in Firefox. If I do so, the
script works well in Firefox, but IE throws error. (I have added the
code snippet below).
<snip> Odd, similar works OK for me across both IE and Firefox. Maybe IE is getting
upset because your assigning the widget to a variable with the same name as
an HTML Element in the same DOM scope? Try enclosing the select in
<form>...</form> or change the JS variable name.


Great! Your solutions work like a charm! Thanks for your time and help.

--
Email: rrjanbiah-at-Y!com
Jul 23 '05 #6
Mark Preston <us****@mpreston.demon.co.uk> wrote in message news:<ce*******************@news.demon.co.uk>...
<snip>

Many thanks to all the experts who answered in this thread. Your
solutions and answers are really helpful.
//month is id used with select
//month = document.getElementById("month"); //<-- for Firefox
month.options.length = 0; //clear options
for(var i=1; i<=12; ++i)

[snip]

I have no problem using code like yours in MSIE - perhaps you have an
older version of IE where the problem arises?


I'm using new versions of IE and Firefox. Colin McKinnon, Robert
and DU solutions are working great. Many thanks again.

--
Email: rrjanbiah-at-Y!com
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: tagdance | last post by:
Hello friends, I have a very interesting problem with my css files and firefox - I guess ! Listen: I have set up my new website www.tagdance.de For testing purpose I have mirrored this...
7
by: Merlin | last post by:
Hi there, I have a serious problem with opening a new window with the help of JavaScript. The problem only occures with Firefox. Once you click on the item which fires up the open function, the...
3
by: cwdjrxyz | last post by:
I suggest that everyone using a recent version of Firefox read http://forums.mozillazine.org/viewtopic.php?t=315656&sid=b1db62861b8da29535c43e9c5c527c3b at once. This new security problem was...
13
by: Giggle Girl | last post by:
Hi there, I am having a problem with the behavior of Firefox, where lefthand column content is not resized properly after it is "collapsed" and then "re-expanded". An online demo is available...
7
by: Xah Lee | last post by:
Look at this page http://xahlee.org/emacs/wrap-url.html Look at it in Firebox, look at it in Safari, in Opera, and look at it in Microsoft Internet Explorer. The only fucked up case, is...
1
by: tinnews | last post by:
I'm running a python script via the apache ExtFilterDefine directive, it works basically as expected *except* that when I change the script apache/firefox continue to run the old version of the...
3
by: willyWEB66 | last post by:
Hi everyone, I'm having problem with the sequence of execution for xml.onload. It works fine if your not passing parameters to onload event but my code needs to pass parameter to its function. I'm...
8
by: Atemporal | last post by:
When I click the link of this group, it shows the followings errors. Cannot find comp.lang.c%2B%2B There is no group named comp.lang.c%2B%2B. * The link you followed may be broken or...
15
by: Mike Driscoll | last post by:
Hi, I've had this niggling issue from time to time. I want to create a shortcut on the user's desktop to a website that specifically loads Firefox even if Firefox is not the default browser. ...
6
by: Dennis | last post by:
I am in the process of converting some program output from HTML to XML/XSLT. I have an XSL stylesheet that inserts some javascript in the output that gets called further down to pop up a window)....
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.