From: Dr J R Stockton on 11 May 2010 12:37 In comp.lang.javascript message <hs86gv$eq7$1(a)news.eternal- september.org>, Sun, 9 May 2010 22:44:48, Garrett Smith <dhtmlkitchen(a)gmail.com> posted: >>>> The above code should not be used >>>> multiple times, but should be made a function : >>>> >>>> function Wryt(ID, S) { document.getElementById(ID).innerHTML = S } >Either you have a bunch of little functions that each call >getElementById or you have a bunch of little functions that expect an >element reference, or you have a decorator that does a bunch of things >and takes either an element reference or an id. > >Most times you want to do so much more than set innerHTML. No. Most times I just want to set innerHTML. Consider, for example, my late-draft page $lag-pts.htm. The above code did no more. If it had done more to the element, then either it would have been bad code for repeating gEBI in the same element when the result could have been saved, or it would not have contained the quoted line. If I wanted, in several parts of the code, to set both test and colour, then I would write a routine WrytHue(ID, Str, Hue) . -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7) Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Dr J R Stockton on 16 May 2010 13:38 In comp.lang.javascript message <hsdo99$ha$1(a)news.eternal- september.org>, Wed, 12 May 2010 01:18:44, Garrett Smith <dhtmlkitchen(a)gmail.com> posted: >Dr J R Stockton wrote: >> In comp.lang.javascript message <hs86gv$eq7$1(a)news.eternal- >> september.org>, Sun, 9 May 2010 22:44:48, Garrett Smith >> <dhtmlkitchen(a)gmail.com> posted: >> >>>>>> The above code should not be used >>>>>> multiple times, but should be made a function : >>>>>> >>>>>> function Wryt(ID, S) { document.getElementById(ID).innerHTML = S } >> >>> Either you have a bunch of little functions that each call >>> getElementById or you have a bunch of little functions that expect an >>> element reference, or you have a decorator that does a bunch of things >>> and takes either an element reference or an id. >>> >>> Most times you want to do so much more than set innerHTML. >> No. Most times I just want to set innerHTML. Consider, for >>example, my >> late-draft page $lag-pts.htm. >> > >I see some innerHTML in PopAll and DrawTall. The line in include1.js PopAll is reading innerHTML, not writing it : Text = document.body ; Text = Text.textContent || Text.innerHTML In gravity4.htm DrawTall, a reference Tl to the target element is used to set a reference Ts to its style, as well as being used in Tl.innerHTML = SetSpans( ... . There is no need there for Wryt, which would wastefully re-determine that reference. I concede that using Wryt would require one character fewer in source code. In the HTML source of my site, "Wryt" appears about 200 times, and innerHTML only about 50 times. Some of those 50 are in fact used in defining Wryt for pages not using include1.js. Some are in text. Five are in VBscript pages. My "most times" is justified, and does not mean "always". >For the purpose of the FAQ entry, the focus is on answering the >question, not organizing abstractions. Examples in FAQ answers should not show bad practice, unless that is explicitly indicated. Look into US technical sites, and you will commonly see unnecessary code repetition. If an opportunity for a counter-example appears, it should be taken. >Wryt is a global function that takes an id and a string and tries to >modify innerHTML of an element. The identifier "Wryt" is less >descriptive than innerHTML. It has nothing to do with the global >object, other than expecting that the element is in the same document >that is loaded in that window, and it does not even check to make sure >that it is before accessing its innerHTML. If the reference does not exist, that will generally be found on testing the page and eliminated. Those who want to compute ID values in an unreliable fashion can add a check, and something else to do instead. But remember what I said - that a set of five document.getElementById(String).innerHTML = string would better be done with the equivalent five Wryt calls. If you want the element reference to be checked in Wryt, you must want it also to be checked in the original code - which makes the saving of using an appropriate function so much the greater. >For your site, it is probably easy to catch the problem of missing >element. However, for a page with entire sections of server-generated >or included content, it is a good idea to put in a feature test so that >if the element happens to be not included on the page, there is no >error in that case. Only if the action taken if the reference is not found will be satisfactory. Merely omitting an action can be a grave error. The absence of an appropriate error action is itself an error. >For elements that the program does not create, it is a good idea for >the program to first test to determine if the element actually exists. >For example: > >var el = document.getElementById("cassia"); >if(el) { > el.innerHTML = "Not real cinnamon and carcinogenic."; >} That is a fine example of hiding an error; the reader will be unaware that the displayed page is not as its author hoped it would be. The pointy-headed manager who tests the page may well miss the error. That is indeed bad practice - common as it may be in your commercial sites. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7) Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
First
|
Prev
|
Pages: 1 2 3 Prev: "INSERT INTO Next: Are Unsecured Debt Consolidation Loans the Right Solution? |