PDA

View Full Version : using document.write() and calling a function


WillowFae
11-15-2002, 12:53 PM
Whenever I do a form that needs to be checked before submitted I call a JavaScript function to perform this. I use the following within the form tag.

onSubmit="return checkForm(this)";

So, I know this works.

However, what the form I am trying to call it from at the moment, is a dynamic one created in JavaScript using document.write() and it is not calling the function.

Here is the document.write() line of code

document.write("<form name='login' action='javascript:window.location.reload()' method='post' onSubmit='return setCookies(this)';>");

And here is my setCookies() function

function setCookies(objForm)
{
alert("boo");
}


Both pieces of code are in the same js file.

Any thoughts on why it isn't working?

x_goose_x
11-15-2002, 01:41 PM
At a quick glance it seems to work in IE6.


<script>
function setCookies(objForm)
{
alert("boo");
}

document.write("<form name='login' action='javascript:window.location.reload()' method='post' onSubmit='return setCookies(this)' ;><input type='submit'></form>");
</script>

WillowFae
11-15-2002, 01:48 PM
Because I'm using IE6 and it doesn't work for me.

I've tried using exactly the same code but taking out out of the js file and doing it as straight html and it does work. Therefore it must be something in the document.write()

x_goose_x
11-15-2002, 01:55 PM
odd very odd.

What exactly happens? Does the button not appear? Does it not submit?

WillowFae
11-15-2002, 01:57 PM
Oh the form is all there, and it submits, it just ignores the call to the function and doesn't display the alert box.

x_goose_x
11-15-2002, 02:03 PM
Can you post your entire page? There's not much more I can do without it. My only other thoughts are misplaced form tags that are conflicting, or a typo.

WillowFae
11-15-2002, 02:09 PM
This is the whole function

function login()
{
document.write("<center>");
document.write("<table><tr><td height='150'>&nbsp;</td></tr><tr><td align='center'>");
document.write("<p>This page has restricted access. Please enter the username and password below</p>");
document.write("<form name='login' action='javascript:window.location.reload()' method='post' onSubmit='return setCookies(this)';>");
document.write("<table width='300' bgcolor='#000000' border='0' cellpadding='1' cellspacing='0'><tr><td>");
document.write("<table width='300' bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0'>");
document.write("<tr><td colspan='4' height='10'>&nbsp;</td></tr>");
document.write("<tr><td width='10'>&nbsp;</td><td>Username:</td><td><input type='text' name='username'></td><td width='10'>&nbsp;</td></tr>");
document.write("<tr><td width='10'>&nbsp;</td><td>Password:</td><td><input type='text' name='password'></td><td width='10'>&nbsp;</td></tr>");
document.write("<tr><td height='5' colspan='4'>&nbsp;</td></tr>");
document.write("<tr><td colspan='4' align='center'><input type='submit' value='Submit'>&nbsp;&nbsp;<input type='reset' value='Clear'></td></tr>");
document.write("<tr><td colspan='4' height='10'>&nbsp;</td></tr>");
document.write("</table>");
document.write("</form>");
document.write("</td></tr></table>");
document.write("</td></tr></table>");
document.write("</center>");
}

The only bit of code in the main page is

<body onLoad="login()">

Thanks for looking at this :)

x_goose_x
11-15-2002, 02:20 PM
The problem is you're calling a write function after the page is already loaded. When you do so, it overwrites the current page. If it is called during the load, it is just added to the current page. Whats happenening is the seCookies function is getting written over. Try changing the .js file to:


function setCookies(objForm)
{
alert("boo");
}

document.write("<center>");
document.write("<table><tr><td height='150'> </td></tr><tr><td align='center'>");
document.write("<p>This page has restricted access. Please enter the username and password below</p>");
document.write("<form name='login' action='javascript:window.location.reload()' method='post' onSubmit='return setCookies(this)';>");
document.write("<table width='300' bgcolor='#000000' border='0' cellpadding='1' cellspacing='0'><tr><td>");
document.write("<table width='300' bgcolor='#ffffff' border='0' cellpadding='0' cellspacing='0'>");
document.write("<tr><td colspan='4' height='10'> </td></tr>");
document.write("<tr><td width='10'> </td><td>Username:</td><td><input type='text' name='username'></td><td width='10'> </td></tr>");
document.write("<tr><td width='10'> </td><td>Password:</td><td><input type='text' name='password'></td><td width='10'> </td></tr>");
document.write("<tr><td height='5' colspan='4'> </td></tr>");
document.write("<tr><td colspan='4' align='center'><input type='submit' value='Submit'> <input type='reset' value='Clear'></td></tr>");
document.write("<tr><td colspan='4' height='10'> </td></tr>");
document.write("</table>");
document.write("</form>");
document.write("</td></tr></table>");
document.write("</td></tr></table>");
document.write("</center>");


and just forget about the onload. My question is, why are you doing this? Why not just enter the html without using document.write()?

WillowFae
11-15-2002, 02:23 PM
I'm doing it that way because I will be adding an another function before it to check for cookies (but I'm writing it one bit at a time so I haven't done that bit yet). onload will call a function to check for cookies and if the cookies are non-existant or wrong, then it will call the login() function to display the form.

x_goose_x
11-15-2002, 03:56 PM
why not have the user load the login page regardless and once loaded check for a cookie. If it finds a cookie on that page forwards them right away. It's a small page and will load quickly, the user won't even notice.

Or make a redirect page.

index.htm

if (document.cookie) {
location.replace("welcomeback.htm")
}else{
location.replace("singing.htm")
}

WillowFae
11-15-2002, 04:07 PM
Thanks for your help - it appears to be working now :)

Now I've just got to get this cookie thing sorted out ... *wanders off muttering*

;)