General function "template"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Perttu Pulkkinen

    General function "template"

    I would like to have a javasript/jscript function template/"framwork"
    instead of checking browsers by name. The shortness of script in my opinion
    is not the goal, but clearness and ease iof development. Could you give your
    points, corrections and ideas about template below?

    And are elemens ids available in form or another in all browsers or was
    there some which accpets only element names? And was it that ancient
    netscape only gives form elements, links, anchors and images available but
    no other elements? In Finland some public organisations with lots of
    machines unfortunately still have netscape 4.7.

    function do_something(el em_id) // or should we other ways of referencing
    too?
    {
    if(document.get ElementById() && e = document.getEle mentById(elem_i d))
    //latest versions
    {
    if(e.needed_met hod()) / if(e.needed_pro perty) do_something_wi th_e
    else if(e.alternativ e_method()) / if(e.alternativ e_property)
    do_something_wi th_e
    ...
    else return false;
    }
    elseif(document .all && e = document.all[elem_id]) // IE 5.5
    { do_something_wi th_e }
    elseif( e = document.elem_i d) // Netscape 4.x
    { do_something_wi th_e_or_give_up _hope }
    else return false;
    }


  • Grant Wagner

    #2
    Re: General function "template& quot;

    Perttu Pulkkinen wrote:
    [color=blue]
    > I would like to have a javasript/jscript function template/"framwork"
    > instead of checking browsers by name. The shortness of script in my opinion
    > is not the goal, but clearness and ease iof development. Could you give your
    > points, corrections and ideas about template below?
    >
    > And are elemens ids available in form or another in all browsers or was
    > there some which accpets only element names? And was it that ancient
    > netscape only gives form elements, links, anchors and images available but
    > no other elements? In Finland some public organisations with lots of
    > machines unfortunately still have netscape 4.7.
    >
    > function do_something(el em_id) // or should we other ways of referencing
    > too?
    > {
    > if(document.get ElementById() && e = document.getEle mentById(elem_i d))[/color]

    Here you are testing the result of calling document.getEle mentById() without any
    parameters, not testing if "getElementById " is a property of document. I think
    what you want is:

    if(document.get ElementById && e = document.getEle mentById(elem_i d))

    Note no brackets.
    [color=blue]
    > //latest versions
    > {
    > if(e.needed_met hod()) / if(e.needed_pro perty) do_something_wi th_e[/color]

    Again, you test for the existance of a property with:

    if (e.needed_metho d) or if (typeof e.needed_method != 'undefined')
    [color=blue]
    > else if(e.alternativ e_method()) / if(e.alternativ e_property)
    > do_something_wi th_e
    > ...
    > else return false;
    > }
    > elseif(document .all && e = document.all[elem_id]) // IE 5.5
    > { do_something_wi th_e }
    > elseif( e = document.elem_i d) // Netscape 4.x
    > { do_something_wi th_e_or_give_up _hope }
    > else return false;
    > }[/color]

    There is no way to generate a "generic" template. Attempts have been made, but
    the code quickly bloats due to the significant differences in the DOM of various
    browsers. I prefer smaller atomic methods to perform the tasks I want in
    predictable ways:

    function setVisibility(i d, visible) {
    var o;
    if (document.getEl ementById) {
    o = document.getEle mentById(id).st yle;
    } else if (document.layer s) {
    o = document.layers[id];
    } else if (document.all) {
    o = document.all[id].style;
    }
    if (o) {
    o.visibility = (visible ? 'visible' : 'hidden');
    }
    }

    or better yet:

    if (document.getEl ementById) {
    window.setVisib ility = function(id, visible) {
    var o = document.getEle mentById(id);
    if (o && o.style) {
    o.style.visibil ity = (visible ? 'visible' : 'hidden');
    return (o.style.visibi lity == (visible ? 'visible' : 'hidden'));
    }
    }
    } else if (document.layer s) {
    window.setVisib ility = function(id, visible) {
    var o = document.layers[id];
    if (o) {
    o.visibility = (visible ? 'visible' : 'hidden');
    return (o.visibility == (visible ? 'show' : 'hide');
    }
    }
    } else if (document.all) {
    window.setVisib ility = function(id, visible) {
    var o = document.all[id];
    if (o && o.style) {
    o.style.visibil ity = (visible ? 'visible' : 'hidden');
    return (o.style.visibi lity == (visible ? 'visible' : 'hidden'));
    }
    }
    } else {
    window.setVisib ility = function(id, visible) {
    return false;
    }
    }

    Then you just do:

    if (setVisibility( 'myId', true)) {
    // visibility *appears* to have been changed successfully
    } else {
    // visibility definitely wasn't changed
    }

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    Working...