473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why doesn't this work?

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.

May 4 '06 #1
10 1701
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
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.

May 4 '06 #2
ci****@oduibhin.freeserve.co.uk 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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 4 '06 #3
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)
May 4 '06 #4
ASM
ci****@oduibhin.freeserve.co.uk 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
May 4 '06 #5
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.

May 6 '06 #6
VK

ci****@oduibhin.freeserve.co.uk 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).

May 6 '06 #7
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.

May 6 '06 #8
ci****@oduibhin.freeserve.co.uk 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.
May 6 '06 #9
ASM
ci****@oduibhin.freeserve.co.uk 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
May 6 '06 #10
ASM wrote:
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. :-)

May 7 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: AnnMarie | last post by:
My JavaScript Form Validation doesn't work at all in Netscape, but it works fine in IE. I made some of the suggested changes which enabled it to work in IE. I couldn't make all the changes...
39
by: Mark Johnson | last post by:
It doesn't seem possible. But would the following also seem a violation of the general notions behind css? You have a DIV, say asociated with class, 'topdiv'. Inside of that you have an anchor...
3
by: Matt | last post by:
I want to know if readOnly attribute doesn't work for drop down list? If I try disabled attribute, it works fine for drop down list. When I try text box, it works fine for both disabled and...
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
3
by: Dave Moore | last post by:
Hi All, Ok, here's my problem. I want to open a file and process its contents. However, because it is possible that the file may not exist, I also want to check whether the file() function is...
10
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET...
6
by: Johnny Jörgensen | last post by:
I've got a usercontrol derived from a normal ComboBox that contains some special formatting code. On my main form I've got a lot of my custom comboboxes. I discovered a bug in the derived...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.