add a text in the same HTML page

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

    add a text in the same HTML page

    Rather than opening a new window, how can I add a text (or HTML code)
    in the same HTML page, in a specific location?

    The code I tried is the following:

    <head>
    <script language="JavaS cript">
    function displayText()
    { document.write( "qwertyuiop ");
    }
    </script>
    </head>

    <body>
    <a href="javascrip t:displayText() ">Text</a>
    </body>
  • Laurent Bugnion, GalaSoft

    #2
    Re: add a text in the same HTML page

    Hi,

    Caroline wrote:
    [color=blue]
    > Rather than opening a new window, how can I add a text (or HTML code)
    > in the same HTML page, in a specific location?
    >
    > The code I tried is the following:
    >
    > <head>
    > <script language="JavaS cript">
    > function displayText()
    > { document.write( "qwertyuiop ");
    > }
    > </script>
    > </head>
    >
    > <body>
    > <a href="javascrip t:displayText() ">Text</a>
    > </body>[/color]

    First, you should not use the javascript: pseudo protocol in a link.
    Rather use the ONCLICK event handler, and use the HREF attribute to lead
    to a page handling users without JavaScript.

    <A HREF="nojs.html "
    ONCLICK="displa yText();return false;">Text</A>

    That said, what you try to do is not possible for a simple reason: After
    the page is rendered, the document is closed. Every attempt to write to
    this closed document will reset it fully, and erase everything on the
    page (including the script currently being executed).

    So you will end up with a blank page with the text "qwertyuiop " on it.

    YOu will need to store the document content to some place (or to have a
    way to recreate/access it), add the text to this String and then write
    the whole document back using document.write( ).

    Note also that after you're done writing, you should close the document
    with document.close( )

    Hope that helps,

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch

    Comment

    • Laurent Bugnion, GalaSoft

      #3
      Re: add a text in the same HTML page

      Hi again,

      Caroline wrote:
      [color=blue]
      > Rather than opening a new window, how can I add a text (or HTML code)
      > in the same HTML page, in a specific location?
      >
      > The code I tried is the following:
      >
      > <head>
      > <script language="JavaS cript">
      > function displayText()
      > { document.write( "qwertyuiop ");
      > }
      > </script>
      > </head>
      >
      > <body>
      > <a href="javascrip t:displayText() ">Text</a>
      > </body>[/color]

      Forgot to add that you also can use the DOM Level 2 to access the text
      node you want to modify. However, I am not sure if this is soemthing you
      want/can do (it only works on the newest browsers and requires some
      programming knowledge).

      Laurent
      --
      Laurent Bugnion, GalaSoft
      Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
      Private/Malaysia: http://mypage.bluewin.ch/lbugnion
      Support children in Calcutta: http://www.calcutta-espoir.ch

      Comment

      • G Roydor

        #4
        Re: add a text in the same HTML page

        <div id="selection" class="par3"></div>
        <div id="compteurs" class="par3"></div>


        document.getEle mentById('selec tion').innerHTM L = "Sélection auteur : ["
        + VU + "]"

        définissez des calques <div> puis utilisez la propiétés innerHTML



        Caroline a écrit:[color=blue]
        > Rather than opening a new window, how can I add a text (or HTML code)
        > in the same HTML page, in a specific location?
        >
        > The code I tried is the following:
        >
        > <head>
        > <script language="JavaS cript">
        > function displayText()
        > { document.write( "qwertyuiop ");
        > }
        > </script>
        > </head>
        >
        > <body>
        > <a href="javascrip t:displayText() ">Text</a>
        > </body>[/color]

        Comment

        • Csaba2000

          #5
          Re: add a text in the same HTML page

          My utilitarian view of things:

          "Caroline" <plize@letsdoth atagain.com> wrote in message news:da07e661.0 309240613.2282f c2b@posting.goo gle.com...[color=blue]
          > Thank you Laurent and Guy for your quick replies;
          > However, for didactive purposes, I would like to understand the details:
          > - What is wrong with the javascript: pseudo protocol in a link?[/color]

          If you have this and a browser without javascript (or javascript
          turned off) will have no idea what to do with the link. Thererfore,
          even if you are coding for javascript, it is good general practise
          to use onClick.
          [color=blue]
          > - what is 'return false;' used for excactly?[/color]

          return false tells the browser to stop doing what it would
          normally do in handling the event. In the case of a link,
          it would tend to follow the link, but the "return false;" should
          suppress this tendancy.
          [color=blue]
          > - What do you mean exactly by 'use the DOM Level 2 to access the text
          > node you want to modify?'
          >[/color]

          This is not your exact answer but an indication:
          In early browser history people tended to view a page as
          basically text that you marked up for display.
          More recent browsers acknowledge that a document
          is actually a graph where each all of the pieces of the
          document are nodes in the graph with all sorts of
          attributes that determine how the pieces display and
          what can happen to/with them.
          'use DOM Level 2' means to basically acknowledge
          this view and explicitly operate on nodes (as opposed
          to spewing HTML (with .innerHTML, say) and leaving
          the browser to implicitly create the nodes) by using
          constructs provided for this purpose
          (such as document.create Element or
          document.create TextNode and insertBefore).

          Csaba Gabor from New York
          [color=blue]
          > Thank you
          >
          > Caroline[/color]


          Comment

          Working...