FAQ Topic - How do I modify the current page in a browser? (2008-04-30)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FAQ server

    FAQ Topic - How do I modify the current page in a browser? (2008-04-30)

    -----------------------------------------------------------------------
    FAQ Topic - How do I modify the current page in a browser?
    -----------------------------------------------------------------------

    Using the DOM the non-standard but widely implemented innerHTML
    extension, the following would be sufficient to modify the content
    of any element that can have content:
    ` <div id="anID">Some Content</div` with script of
    ` document.getEle mentById("anID" ).innerHTML=
    "Some <em>new</emContent"; `
    Where "anID" is the (unique on the HTML page) ID attribute value
    of the element to modify.

    The script below adds support for ` document.all ` capable browsers.
    Support for NN4 is also possible, but certain issues mean that
    it is not listed here. Using the example above, the call would
    be written:
    ` DynWrite('anID' ,"Some <em>new</emContent") `
    With the below code also in the page:

    DocDom = (document.getEl ementById?true: false);
    DocAll = (document.all?t rue:false);
    DocStr=''
    if (DocAll) DocStr="return document.all[id]"
    if (DocDom) DocStr="return document.getEle mentById(id)"
    GetRef=new Function("id", DocStr)
    if (DocStr=='') { DynWrite=new Function("retur n false") } else {
    DynWrite=new Function("id", "S", "GetRef(id).inn erHTML=S; return true")
    }

    An alternative DynWrite function:



    The innerHTML property of the Element interface gets or sets the HTML or XML markup contained within the element, omitting any shadow roots in both cases.


    The innerHTML property of the Element interface gets or sets the HTML or XML markup contained within the element, omitting any shadow roots in both cases.



    --
    Postings such as this are automatically sent once a day. Their
    goal is to answer repeated questions, and to offer the content to
    the community for continuous evaluation/improvement. The complete
    comp.lang.javas cript FAQ is at http://jibbering.com/faq/index.html.
    The FAQ workers are a group of volunteers. The sendings of these
    daily posts are proficiently hosted by http://www.pair.com.

  • Dr J R Stockton

    #2
    Re: FAQ Topic - How do I modify the current page in a browser? (2008-04-30)

    In comp.lang.javas cript message <4817a877$0$902 62$14726298@new s.sunsite.
    dk>, Tue, 29 Apr 2008 23:00:02, FAQ server <javascript@dot internet.be>
    posted:
    >FAQ Topic - How do I modify the current page in a browser?
    It appears that

    function Wryt(ID, S) { document.getEle mentById(ID).in nerHTML = S }

    can be used, instead of the code in the current answer, with all current
    browsers; and that, if getElementById is not natively present, then

    if (document.all && !document.getEl ementById) {
    document.getEle mentById = function(id) {
    return document.all[id] } }

    will provide a version adequate for this purpose. To avoid declaring a
    non-standard global function, one might use (needs test) something like

    function Wryt(ID, S) { with (document)
    ( getElementById ? getElementById( ID) : all[ID] ).innerHTML = S }

    instead.

    OTOH, IIRC, it has been stated that it is better to use newer DOM
    methods rather than innerHTML. If someone were to suggest suitable
    tested code, perhaps the FAQ entry might get changed?

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
    <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
    <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
    <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.

    Comment

    Working...