Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Why doesn't this work?

Reply
Thread Tools

Why doesn't this work?

 
 
ciaran@oduibhin.freeserve.co.uk
Guest
Posts: n/a
 
      05-04-2006
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.

 
Reply With Quote
 
 
 
 
M
Guest
Posts: n/a
 
      05-04-2006
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

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

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      05-04-2006
writes:

> Can anyone tell me what it wrong with this code, please?
>
> <script language="JavaScript">


To be valid HTML, it should use the type attribute:
<script type="text/javascript">
It's not the source of your problems though.

> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      05-04-2006
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.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
ASM
Guest
Posts: n/a
 
      05-04-2006
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>
--
Stephane Moriaux et son [moins] vieux Mac
 
Reply With Quote
 
ciaran@oduibhin.freeserve.co.uk
Guest
Posts: n/a
 
      05-06-2006
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.

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      05-06-2006

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

 
Reply With Quote
 
ciaran@oduibhin.freeserve.co.uk
Guest
Posts: n/a
 
      05-06-2006
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.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      05-06-2006
wrote:
> 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.


 
Reply With Quote
 
ASM
Guest
Posts: n/a
 
      05-06-2006
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();
}
}


--
Stephane Moriaux et son [moins] vieux Mac
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57