Problem: script works in IE but not FF

Discussion in 'Javascript' started by Martin, May 16, 2011.

  1. Martin

    Martin Guest

    I have the javascript shown below in the <head> section of a web page.

    In IE, both of these functions execute properly (they are both being
    triggered by the onchange event in several different inputs)
    However, in FF, the 2nd function does not execute. The "toggleState"
    function works but "markChange" does not. To execise this, I added
    an alert('It Works!') to the 2nd function - it pops up in IE but not
    in FF.

    Yes, javascript is enabled in FF. Like I said, the 1st function works
    ok (along with some other functions that are in page. Only the
    markChange function doesn't work. And, yes, I have verified that the
    function name is spelled and capitalized correctly everywhere.

    Any thoughts as to what I need to look for?

    Thanks.

    ---------------------------------------------------------------------------------------------------
    <script type='text/javascript'>

    function toggleState(item){
    x='ONOFF'+item.name.substring(3);
    if(item.className == 'on') {
    item.className='off';
    item.value='OFF';
    document.getElementById(x).value='COFF';
    } else {
    item.className='on';
    item.value='ON';
    document.getElementById(x).value='CON';
    }
    }

    function markChange(fldNum){
    x=document.all['CHANGED'].value;
    document.all['CHANGED'].value = x.substring(0,fldNum-1) +'1'+
    x.substring(fldNum);
    }
    </script>
     
    Martin, May 16, 2011
    #1
    1. Advertisements

  2. Well document.all originates from the IE/MSHTML-DOM and Firefox only
    supports it in quirks mode I think.
    Check the error console in Firefox, if nothing happens at all with the
    function then it is likely to show you the reason.
     
    Martin Honnen, May 16, 2011
    #2
    1. Advertisements

  3. Martin

    Martin Guest

    Thanks, Martin.

    The error console shows: document.all is undefined

    Any suggestions as to how I can make this work?

    This "CHANGED" field is a hidden input in which I'm setting a flag
    (changing a 0 to a 1) that is used on the server side to control some
    activity.

    Thanks.
     
    Martin, May 16, 2011
    #3
  4. Martin

    Luuk Guest

    if your page looks like this:
    <form name="From">
    <input type="hidden"name="field">
    </form>

    Then this should work:
    document.forms['From'].field.value = '1';
     
    Luuk, May 16, 2011
    #4
  5. For maximal interoperability, you might use document.getElementById when
    it is defined and document.all otherwise (and give up, making horrible
    noises, when neither is defined), but that would probably be overkill
    nowadays - especially since other parts of your code assume that
    document.getElementById is defined.
    The simplest way over this problem is to add id="CHANGED" (unless
    "CHANGED" is already in use as an id value, in which case you need to
    invent some other value that is not in use yet) to that field and use
    document.getElementById("CHANGED").

    This being said, it is inherently unsafe to rely on JavaScript for
    tracking down changes. The server-side form handler should know the data
    that was sent in the form and should then compare the data received
    against that. Remember that users or systems may switch off JavaScript
    and may use a modified version of your page, with modified markup and/or
    JavaScript.
     
    Jukka K. Korpela, May 16, 2011
    #5
  6. In comp.lang.javascript message <[email protected]
    You have been told how to fix it.

    Additionally, in that function there should be a 'var' before 'x=', and
    the function could better have been written as something like

    function markChange(fldNum) {
    var x = document.all['CHANGED'], t = x.value
    x.value = t.substring(0, fldNum-1) + '1' + t.substring(fldNum);
    }


    Personally, rather than accessing an element by using one of the global
    collections accessed by a string representing name or ID, I would prefer
    to use a more localised route to the element. For example, a button in
    a form can be given onclick-"Func(this.form)" to call
    function Func(F) { ... }
    in which controls and other elements in the form can be addressed more
    directly by using the argument F.

    Global addressing may clash with something else on the page; local
    addressing can at most clash locally.
     
    Dr J R Stockton, May 17, 2011
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.