Click to See Complete Forum and Search --> : calling a function from document.write html
spoonshoe
07-17-2003, 10:12 PM
Hi, I am having a problem calling a function. I have a function that loops,
and writes HTML, in this HTML I placed a button and a different function, when i click on the button i get "object expected"....i have tried several different functions, buttons etc......any help would be GREATLY appreciated.....as would a basket of sausage................................
Exuro
07-17-2003, 10:38 PM
I don't get how you people expect us to help you when we don't know what you're working with... If you want specific help like this you really need to either post the code or a link to the code. Anyway, I'd say it's probably a syntax error... If you're writing a new function from inside a function, you've probably put in a ' or " or ) where you weren't supposed to and it killed the whole script...
spoonshoe
07-18-2003, 10:08 PM
sorry about that...i was at work and didn't have access to it...but here it is (recreated)
<script language=javascript>
function test()
{
alert("what");
}
function generate(count)
{
document.write("<div style='position:absolute; left:0px; top:0px;'><form name=dog><input type='button' value='New' onclick='test();'></form><br><table border=1 width=630 cellspacing=0 cellpadding=0 style='position:absolute; top:150px; left:px;'> <th>heading</th></tr></br>");
var line=new Array();
for(i=0;i<count; i++)
{
line[i]=" <tr>text<td></td></tr></br>"
document.write(line[i]);
}
{
document.write("</table>");
}
}
</script>
</head>
<body>
<input type="button" value="click" onclick="generate(4);">
</body>
</html>
Exuro
07-19-2003, 02:43 PM
Okay, the problem with your script had to do with using document.write (document.writeln is the exact same thing but it puts a "\n" at the end automatically). When you call document.write or document.writeln, it actually erases your entire document and starts to write a new one. You can see this by selecting View Source on the page after you've hit your "click" button. Since the entire old document had been overwritten, the function test() was no longer there to call, hence your "object expected" error. To fix this you have to include the test() function inside the part of generate() where you're writing the new document. I fixed the code for your table a little bit too... Here's how I did it:
<html>
<head>
<script language="javascript">
function generate(count) {
document.writeln("<html><head><script language='javascript'>");
document.writeln("function test() {");
document.writeln("alert('what'); }");
document.writeln("<\/script></head><body>");
document.writeln("<div style='position:absolute; left:0px; top:0px;'>");
document.writeln("<form name=dog><input type='button' value='New' onclick='test();'>");
document.writeln("</form><br><table border=1 width=630 cellspacing=0 cellpadding=0");
document.writeln(" style='position:absolute; top:150px; leftx;'><tr><th>heading</th></tr>");
var line=new Array();
for(i=0;i<count; i++) {
line[i]="<tr><td>text</td></tr>";
document.writeln(line[i]);
}
document.writeln("</table></body></html>");
}
</script>
</head>
<body>
<input type="button" value="click" onclick="generate(4);">
</body>
</html>
spoonshoe
07-19-2003, 08:18 PM
Thank You SOOOO Much.....you rock................