HighDots Forums  

add a text in the same HTML page

Javascript JavaScript language (comp.lang.javascript)


Discuss add a text in the same HTML page in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Caroline
 
Posts: n/a

Default add a text in the same HTML page - 09-23-2003 , 03:14 AM






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>

Reply With Quote
  #2  
Old   
Laurent Bugnion, GalaSoft
 
Posts: n/a

Default Re: add a text in the same HTML page - 09-23-2003 , 03:44 AM






Hi,

Caroline wrote:

Quote:
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



Reply With Quote
  #3  
Old   
Laurent Bugnion, GalaSoft
 
Posts: n/a

Default Re: add a text in the same HTML page - 09-23-2003 , 03:45 AM



Hi again,

Caroline wrote:

Quote:
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



Reply With Quote
  #4  
Old   
G Roydor
 
Posts: n/a

Default Re: add a text in the same HTML page - 09-23-2003 , 11:34 AM



<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:
Quote:
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


Reply With Quote
  #5  
Old   
Caroline
 
Posts: n/a

Default Re: add a text in the same HTML page - 09-24-2003 , 09:13 AM



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?
- what is 'return false;' used for excactly?
- What do you mean exactly by 'use the DOM Level 2 to access the text
node you want to modify?'

Thank you

Caroline

Reply With Quote
  #6  
Old   
G Roydor
 
Posts: n/a

Default Re: add a text in the same HTML page - 09-24-2003 , 10:57 AM





Caroline a écrit:
Quote:
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?
- what is 'return false;' used for excactly?
return false; interrompt la séquence d'exécution.

Quote:
- What do you mean exactly by 'use the DOM Level 2 to access the text
node you want to modify?'
DOM level 2 est la méthode que je vous ai indiquée. ...innerHTML = "text";

ce niveau requiert des navigateurs récents;(ex : IE5 et Netscape 7)

Quote:
Thank you

Caroline


Reply With Quote
  #7  
Old   
Csaba2000
 
Posts: n/a

Default Re: add a text in the same HTML page - 09-25-2003 , 02:10 AM



My utilitarian view of things:

"Caroline" <plize (AT) letsdothatagain (DOT) com> wrote

Quote:
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.

Quote:
- 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.

Quote:
- 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

Quote:
Thank you

Caroline



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.