Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multipal document.write() 3

Status
Not open for further replies.

rhcp

Programmer
Dec 27, 2003
11
IL
I really need help people. I have checked my script from all ways it can be checked and my problem is with the document.write() code. I start off by writing my whole 'body' with the document.write() . at this stage all is fine!. but now, I want to rewrite the whole document again completly from top to bottom- so I call a function that calls another function that contains the 2nd document with all the document.write() in it. well...... There is an error and the second document is functioning like it should. actually the second document is much like the first one accept a change in 1 of the images location.
here's the code ("bu()" is the first document and "bu2()" is the 2nd):

<html>
<head>
<title>
project
</title>
<script language=&quot;javascript&quot;>
var x=1;var y=10; var z=1;
bu();
function good()
{
bu2();
alert(&quot;1&quot;);
getRandom();
}
function bad()
{
alert(&quot;2&quot;)
}
function getRandom()
{
x=(Math.round(Math.random()*(6-1)))+1

if (x==y)
{ if (x==6)
{x=x-1}
x=x+1



}
y=x;

b4.src=x+&quot;/8.jpg&quot;;
b3.src=x+&quot;/2.jpg&quot;;
b2.src=x+&quot;/3.jpg&quot;;
b1.src=x+&quot;/4.jpg&quot;;

}



function bu()
{
document.write(&quot;<table hspace='0' vspace='0' cellSpacing=5 cellPadding=0 border=0 vAlign=top align=center>&quot;)

document.write(&quot;<tr>&quot;)

document.write(&quot;<td align='center'> <img name='b1' src=1/2.jpg WIDTH='100' HEIGHT='100' onClick='bad()' > &quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;<td align='center'> <img name='b2' src=1/3.jpg WIDTH='100' HEIGHT='100' onClick='bad()' >&quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;<td align='center'> <img name='b3' src=1/4.jpg WIDTH='100' HEIGHT='100' onClick='bad()' > &quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;<td align='center'> <img name='b4' src=1/8.jpg WIDTH='100' HEIGHT='100' onClick='good()'>&quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;</tr>&quot;)






document.write(&quot;</table>&quot;)

}

function bu2()
{
document.close()
document.open()
document.write(&quot;<table hspace='0' vspace='0' cellSpacing=5 cellPadding=0 border=0 vAlign=top align=center>&quot;)

document.write(&quot;<tr>&quot;)

document.write(&quot;<td align='center'> <img name='b1' src=1/2.jpg WIDTH='100' HEIGHT='100' onClick='bad()' > &quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;<td align='center'> <img name='b2' src=1/3.jpg WIDTH='100' HEIGHT='100' onClick='bad()' >&quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;<td align='center'> <img name='b4' src=1/8.jpg WIDTH='100' HEIGHT='100' onClick='good()' >&quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;<td align='center'> <img name='b3' src=1/4.jpg WIDTH='100' HEIGHT='100' onClick='bad()' > &quot;)

document.write(&quot;</td>&quot;)

document.write(&quot;</tr>&quot;)





document.write(&quot;</table>&quot;)


}
</script>
</head>
<body>




</body>
</html>

THX A LOT!
 
Well I'm not too good with fixing other peoples scripts but some times the problem might be with semi-colons ( ; ) just put those on the end of each document.write statement and see if that helps.
Jammer1221
 
Welp I just checked and the semi colons did nothing so scratch that idea
sorry
 
ok as far as I know when u rewrite the document it loses as the functions an varibles so what you do is create a js file with all the functions an varibles you need an have the bu2() write it in so it has all the functions in the 'second' page you write...the next problem i had after that was that b4 was undefined but, I have to go I am sorry to leave ya in the middle of a problem
jammer1221
 
rhcp, I looked at your code and realized that there is nothing wrong with your document.write() statements...

The problem lies in calling &quot;bu2()&quot;. When you click on document.write(&quot;<td align='center'> <img name='b4' src='1/8.jpg' WIDTH='100' HEIGHT='100' onClick='good()'>&quot;); it calls function bu2(), and that's where you get an error &quot;Object Expected&quot;, if you switch &quot;bu();&quot; and bu2()&quot; you'll get the same error.

Here's a good hint on why you're getting that &quot;object expected&quot; error:
I played around with your code, and from all my fixings, i'm sure the problem lies in calling of the functions. You might calling an object before its loaded.

Reality continues to ruin my life...
 
Try the following. This no loonger uses document.write, which means that you wont lose any of your page-side code:

Code:
<html>
<head>
<title>project</title>
<script language=&quot;javascript&quot;>
var x=1;var y=10; var z=1;

function good()
{
	bu2();
	alert(&quot;1&quot;);
	getRandom();
}

function bad()
{
	alert(&quot;2&quot;);
}

function getRandom()
{
	x=(Math.round(Math.random()*(6-1)))+1;
	if (x==y && x!=6) x++;
	y=x;
	document.images.b4.src=x+&quot;/8.jpg&quot;;
	document.images.b3.src=x+&quot;/2.jpg&quot;;
	document.images.b2.src=x+&quot;/3.jpg&quot;;
	document.images.b1.src=x+&quot;/4.jpg&quot;;
}

function bu()
{
	var htmlStr = '';
	htmlStr += '<table hspace=&quot;0&quot; vspace=&quot;0&quot; cellSpacing=&quot;5&quot; cellPadding=&quot;0&quot; border=&quot;0&quot; vAlign=&quot;top&quot; align=&quot;center&quot;>';
	htmlStr += '<tr>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b1&quot; src=&quot;1/2.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;bad();&quot;>';
	htmlStr += '</td>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b2&quot; src=&quot;1/3.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;bad();&quot;>';
	htmlStr += '</td>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b3&quot; src=&quot;1/4.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;bad();&quot;>';
	htmlStr += '</td>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b4&quot; src=&quot;1/8.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;good();&quot;>';
	htmlStr += '</td>';
	htmlStr += '</tr>';
	htmlStr += '</table>';
	document.getElementById('bodyContent').innerHTML = htmlStr;
}

function bu2()
{
	var htmlStr = '';
	htmlStr += '<table hspace=&quot;0&quot; vspace=&quot;0&quot; cellSpacing=&quot;5&quot; cellPadding=&quot;0&quot; border=&quot;0&quot; vAlign=&quot;top&quot; align=&quot;center&quot;>';
	htmlStr += '<tr>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b1&quot; src=&quot;1/2.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;bad();&quot;>';
	htmlStr += '</td>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b2&quot; src=&quot;1/3.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;bad();&quot;>';
	htmlStr += '</td>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b4&quot; src=&quot;1/8.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;good();&quot;>';
	htmlStr += '</td>';
	htmlStr += '<td align=&quot;center&quot;> <img name=&quot;b3&quot; src=&quot;1/4.jpg&quot; WIDTH=&quot;100&quot; HEIGHT=&quot;100&quot; onClick=&quot;bad();&quot;>';
	htmlStr += '</td>';
	htmlStr += '</tr>';
	htmlStr += '</table>';
	document.getElementById('bodyContent').innerHTML = htmlStr;
}
</script>
</head>
<body onLoad=&quot;bu();&quot;>
	<div id=&quot;bodyContent&quot;></div>
</body>
</html>

I hope this helps!

Dan
 
Thx you all-
jammer1221 : Thx for giving it a try! I think you're right about the JS file.

GUJUm0deL :I too think the problem lies in calling of the functions and it has to do with some basics of javascript document.() functions. I don't think I would have an error if it was made in C++. and Thx for the link 2.

BillyRayPreachersSon : ur code works! I never really used the htmlstr, but now I'll give it more respect.
&quot; document.getElementById('bodyContent').innerHTML = htmlStr&quot; clears the body and that's another thing I should keep in mind accept the document.clear/open(). Thx! :)
btw- is htmlStr compatible with all broswers and versions?
 
rhcp,

htmlStr isn't a browser method/function/property. It is simply the name of a string variable I defined using JavaScript. I could just have easily called it &quot;pinkBananas&quot; and referred to it as:

Code:
var pinkBananas = '';
    pinkBananas += '<table hspace=&quot;0&quot; vspace=&quot;0&quot; cellSpacing=&quot;5&quot; cellPadding=&quot;0&quot; border=&quot;0&quot; vAlign=&quot;top&quot; align=&quot;center&quot;>';
    document.getElementById('bodyContent').innerHTML = pinkBananas;

Also, similarly with &quot;bodyContent&quot;, that was purely the ID I gave the DIV tag. I could just have easily called it &quot;mintySludge&quot;, and used:

Code:
document.getElementById('mintySludge').innerHTML = pinkBananas;

I'm not being factetious - just showing that I really could have called those variables anything. Normally, however, variables are given meaningful names that portray their intended use.

Hope this clarifies things,

Dan

 
try removing
document.close()
document.open()
from bu2 but just an idea. mabye you could make a script element outside of the opening and closing part of the document with script that just has varialbles set for saving data.
this is where naming the variable comes in
as said above come into play
Example:
<script>
var innerScript1=&quot;&quot;;
var innerScript2=&quot;&quot;;
so so forth...</script>
example script in the document <script id=&quot;RewriteBody1&quot;></script>
then in your public area of code Set the variables to equal the script ID <!-- <script>innerScript1=document.getElementByID('IDOfTheScript');</script>-->
to make a virtual copy of it, then call it using window.execScript(&quot;NameOfVaraibleScriptToExecute&quot;)
 
I didn't look at the script all to much when I replied I just saw it works and now know why my reply was stupid cause you can use &quot;pinkBananas&quot;. Thx again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top