Why does this particular javascript code not work in Firefox?

Oct 19, 2000
17,860
4
81
I have two lines of javascript code that works great in IE8, but does not work at all in the latest Firefox.

ONLOAD=document.getElementById('scriptchoice').select();
ONCHANGE=document.getElementById('scriptchoice').select();

These are basically selecting a text form element when a user clicks on certain links. When that text form element is selected, magical things happen in the rest of the page that uses other (advanced) javascript that works great. If the user manually selects the text form element manually, everything works, but I need this to work so that a user doesn't have to do this meaningless task, I want javascript to select it automatically, since it needs to be done.

Firefox refuses to select the box, however. Some google searching hasn't done much for me. I found a thing or two, but I'm familiar enough with javascript to know what any of the fixes mean, or if it even applies to my situation.

How can I get Firefox to work with me on this?
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
try it like this

ONLOAD=setTimeout("document.getElementById('scriptchoice').select()", 5);

It has something to do with your page doctype. I dont' write raw javascript anymore (I use jquery) so I don't remember exactly why. I do remember having that problem and the above fixing it.
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Doing the above would invoke eval, which is very slow and feeding an actual string as the first parameter to setTimeout is completely not the proper method but user agents basically have to support legacy oldschool code that people who did not take the time to actually learn the language got accustomed to.

'load' is an event, if we are in a global context 'onload' is the property of the 'window' object and therefore we are assigning something to do when the page loads. This would mean we need to feed an actual function reference/literal, not the result of a function invocation unless the function invocation returns a function. So in other words, do this:

window.onload = function() {
// Your code here
}

Basic JS 101 :) As far as I know the doctype of the page which is meant to determine whether the page is in standards or quirks mode for stylistic purposes but there are a few caveats with certain user agents and how they pre-populate the DOM, so it's possible but I doubt it.

I would recommend learning *proper* Javascript before moving on to jQuery because as soon as you run into a script error that you have no clue about, you have no idea what to do and will always rely on other people who know JS whereas if you had learned the core language and got accustomed to some level of real DOM scripting you'd know more about what's going on behind the framework.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
Like I said I'd never do it, but it works. I spent maybe a week on writing 'pure' javascript before I said screw this and moved on to jquery. It just wasn't worth the effort to figure out all the little browser quirks.