By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

add a text in the same HTML page

P: n/a
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="JavaScript">
function displayText()
{ document.write("qwertyuiop");
}
</script>
</head>

<body>
<a href="javascript:displayText()">Text</a>
</body>
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,

Caroline wrote:
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="JavaScript">
function displayText()
{ document.write("qwertyuiop");
}
</script>
</head>

<body>
<a href="javascript:displayText()">Text</a>
</body>


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="displayText();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

Jul 20 '05 #2

P: n/a
Hi again,

Caroline wrote:
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="JavaScript">
function displayText()
{ document.write("qwertyuiop");
}
</script>
</head>

<body>
<a href="javascript:displayText()">Text</a>
</body>


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

Jul 20 '05 #3

P: n/a
<div id="selection" class="par3"></div>
<div id="compteurs" class="par3"></div>
document.getElementById('selection').innerHTML = "Sélection auteur : ["
+ VU + "]"

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

Caroline a écrit:
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="JavaScript">
function displayText()
{ document.write("qwertyuiop");
}
</script>
</head>

<body>
<a href="javascript:displayText()">Text</a>
</body>


Jul 20 '05 #4

P: n/a
My utilitarian view of things:

"Caroline" <pl***@letsdothatagain.com> wrote in message news:da**************************@posting.google.c om...
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?
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.
- what is 'return false;' used for excactly?
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.
- What do you mean exactly by 'use the DOM Level 2 to access the text
node you want to modify?'

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.createElement or
document.createTextNode and insertBefore).

Csaba Gabor from New York
Thank you

Caroline

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.