Why doesn't this work?

C

ciaran

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".

Thanks for any ideas.
 
M

M

Hello,

You have to had "<script language..." in the document.write() parameter.

Look at your source code after your first clic, you'll understand your
mistake.

M
 
L

Lasse Reichstein Nielsen

Can anyone tell me what it wrong with this code, please?

<script language="JavaScript">

To be valid HTML, it should use the type attribute:
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".

When you click "here", you do a document.write on a document that
has finished loading. That clears the page and starts a new one
and writes into that.
I click on "A" and, instead of seeing the page with link B as expected,
I get "Error in page".

On the new page, there is no script defining a function called
"myFunction1".


/L
 
E

Evertjan.

wrote on 04 mei 2006 in comp.lang.javascript:
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".

No, you don't.

You see "a" page, but not "the" page,
since the html is completely overwritten, and even the script is gone.
I click on "A" and, instead of seeing the page with link B as expected,
I get "Error in page".

There is no stript anymore.

================

document.write() after the page is finished,
issues an implicit document.open() and clears the page source.
 
A

ASM

(e-mail address removed) a écrit :
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".

function myFunction2(){
document.write ('
'<script type="text/javascript">'+
'function myFunction1(){document.write ("that\'s gone");}'+
'<\/script>'+
' Link <a href="javascript:myFunction1()">A<\/a>')
}

because when you write by JS, your old file is forgotten
with its own JS too



<html>
<script type="text/javascript">
function myFunction(what) {
var lnk = what;
what = what.href.toString();
what = what.substring(what.lastIndexOf('/')+1);
if(what=='A') { lnk.href= 'B'; lnk.innerHTML='B'; }
else { lnk.href='A'; lnk.innerHTML='A';}
}
</script>
<p><a href="A" onclick="myFunction(this);return false;">A</a></p>
<p><a href="B" onclick="myFunction(this);return false;">B</a></p>
</html>
 
C

ciaran

Thanks to all who responded. I've learned a bit, but have still not
solved the problem. Here's where I'm at now.

File temp.js

function myFunction2(){
document.writeln ('<script src="temp.js" language="Javascript"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction1()">A</a></body>')}
function myFunction1(){
document.writeln ('<script src="temp.js" language="Javascript"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction2()">B</a></body>')}

Main page:

<script src="temp.js" language="Javascript"
type="text/javascript"></script>
<body>
Start <a href="javascript:myFunction2()">here</a>
</body>

It behaves almost exactly as before, actually. Click "here", page with
"Link A" appears, click "A", nothing happens (I expect to see a page
with "Link B"). The main difference is that the source of the "page
with Link A" now looks correct to me, but still doesn't work.

Any more hints, please?

To Stephane: thanks for good ideas, but I can't see how it will do what
I want. I want to show other things on the page besides the link (A or
B), different things for A than for B. Suppose we use your function
with this body:

<p><a href="A" onclick="myFunction(this);return false;">A</a><p>Text
for A</p>

Clicking the link will change A to B and back to A, but will not change
"Text for A". For that, I think I cannot avoid opening a new page.
 
V

VK

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).
 
C

ciaran

Thanks, that's something I didn't understand a few days ago.

In fact I *do* want the document.writes in my functions to start a new
page. I'm not trying to restructure an existing page. I'm trying to
generate a pair of pages, linked to each other, but each will have its
own text and images.

It's easy if I avoid Javascript, and just make the two pages. A user
can switch forward and back between them all day long! But when I get
into production, there will be hundreds of pages, not just two. With
mutually recursive Javascript functions to document.write the pages, I
think I should be able to keep it tidy. But I can't get the links to
work as I expected. Any comment on my latest attempt would be very
welcome - I reproduce it again below.

<script src="temp.js" language="Javascript"
type="text/javascript"></script>
<body>
Start <a href="javascript:myFunction2()">here</a>
</body>

and file temp.js:

function myFunction2(){
document.writeln ('<script src="temp.js" language="Javascript"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction1()">A</a></body>')}

function myFunction1(){
document.writeln ('<script src="temp.js" language="Javascript"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction2()">B</a></body>')}

After clicking on "here", the page source is generated by IE5.5:

<script src="temp.js" language="Javascript"
type="text/javascript"></script>
<body>Link <a href="javascript:myFunction1()">A</a></body>

but clicking on "A" produces no effect, even though myFunction1 is
defined in temp.js and should generate a page containing a link "B".

Thanks again for any help.
 
R

Richard Cornford

Thanks to all who responded. I've learned a bit, but have
still not solved the problem. Here's where I'm at now.

An observance of the accepted Usenet post formatting conventions will
increase the chances of your getting helpful responses here. See: <URL:
http://jibbering.com/faq/ >
File temp.js

function myFunction2(){
document.writeln ('<script src="temp.js" language="Javascript"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction1()">A</a></body>')}
<snip>

When you have navigated your page to the URL "javascript:myFunction1()"
and then try to load a javascript file with the relative URL "temp.js",
what absolute URL do you expect the browser to construct for the
resource, and do you expect it be found on the Internet?

Also, when you have finished writing to a document it is a good idea to
call - document.close(); - so that the browser knows you have finished.

Incidentally, your proposed scheme is insane and will get you into hot
water pretty quickly if you pursue it.

Richard.
 
A

ASM

(e-mail address removed) a écrit :
Thanks, that's something I didn't understand a few days ago.

Your code with some fiew correction :

- file 'index1.htm' =
<html>
<script src="temp1.js" type="text/javascript"></script>
Start <a href="javascript:myFunction2()">here</a>
</html>

- file 'temp1.js' =
function myFunction2(){
document.write ('<html><script src="temp1.js" type="text/javascript">'+
'<\/script>')
document.write ('Link <a href="javascript:myFunction1()">A<\/a>'+
'<\/html>')
}
function myFunction1(){
document.write ('<html><script src="temp1.js" type="text/javascript">'+
'<\/script>')
document.write ('Link <a href="javascript:myFunction2()">B</a>'+
'<\/html>')
}

the links A and B are wrote one after other (in my Firefox)
You'll must close the document


Try this :

- file 'index.htm' =
<html>
<script src="temp.js" type="text/javascript"></script>
Start <a href="#" onclick="newPage('A');return false;">here</a>
</html>

- file 'temp.js' =
var txt = '';
txt += '<html>\n<head>\n';
txt += '<script src="temp.js" type="text/javascript"><\/script>\n';
txt += '<\/head>\n<body>\n';

function newPage(myLink) {
var page = txt + '<p><a href="#" onclick="';
if (myLink == 'A')
page += "newPage('B');return false;\">B<\/a>";
else
page += "newPage('A');return false;\">A<\/a>";
page += '<\/p>\n<\/body>\n<\/html>';
with (document) {
open();
write(page);
close();
}
}
 
C

ciaran

ASM said:
the links A and B are wrote one after other (in my Firefox)
You'll must close the document

and Richard Cornford wrote
Also, when you have finished writing to a document it is a good idea to
call - document.close(); - so that the browser knows you have finished.

That is the answer. The functions work when I add "document.close()" to
the end of each function. So the functions look like this:

function myFunction2(){
document.writeln ('<script src="temp.js"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction1()">A</a></body>')
document.close()}

function myFunction1(){
document.writeln ('<script src="temp.js"
type="text/javascript"></script>')
document.write ('<body>Link <a
href="javascript:myFunction2()">B</a></body>')
document.close()}

Without "document.close()", Firefox keeps adding to the same page (as
Stephane says), while IE stops doing anything after two clicks. With
"document.close()", both browsers work as expected, for as many clicks
as we like to make.

Many thanks again for your help. Now I can get on with my "insane"
scheme. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top