wrote:
> Can anyone tell me what it wrong with this code, please?
>
> <script language="JavaScript">
> function myFunction2(){
> document.write ('Link <a href="javascript:myFunction1()">A</a>')}
>
> function myFunction1(){
> document.write ('Link <a href="javascript:myFunction2()">B</a>')}
> </script>
>
> <body>
> Start in
> <a href="javascript:myFunction2()">here</a>
> </body>
>
> I click on "here" and I see the page with link "A".
> I click on "A" and, instead of seeing the page with link B as expected,
> I get "Error in page".
That's a <FAQENTRY>
document.write method has two absolutely different behaviors depending
on when do you use it.
1) During the page load:
....
<body>
<script type="text/javascript">
document.write("<p>Hello World!</p>");
</script>
....
</body>
</html>
In this case document.write acts like an alternate input stream. You
browser parser stops taking data from server and takes it from the
write() string. As soon as these strings are parsed, it switches back
to the default input stream.
2) After the page load (this formal moment happens them window object
fires "load" event).
>From this moment and any further document.write acts as a "page
replacer".
document.write("<p>Hello World!</p>"); now real means:
// clear current page including any scripts in it:
document.clear();
// open default "text/html" input stream for write method:
document.open("text/html");
// send content to the input stream:
document.write(yourString);
// close input stream:
document.close();
The above means that document.write method cannot be used for anything
but conditional content generation *during the loading time*.
So your solution cannot work by definition because write() called in
one function clears the page and kills all other functions.
You have to use DOM methods (document.createElement /
document.body.appendChild) or innerHTML (though it is not reliable in
all circumstances).