advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 4/5 | Rate this item | 1 user has rated this item.
Expertise: Beginner
Language: JavaScript
May 18, 2004
Execute Form Processing Code Without the BODY Tag
I've had two instances recently where I had to execute some form processing code once the page load event was fired. However, I didn't have access to the BODY tag because it was contained in a global header include. Obviously, adding my onload event handler in a global header would throw 'Object does not exist' errors all over the site.

There is the non-standard option of placing your function call in the onload event of an IMG, SCRIPT, or FORM tag, but in my experience, this method is less than perfect or consistent in inconsistently working. A browser can load a form at the bottom of a page before it loads an image at the top of your page. And throwing unfriendly errors, let alone any error, is bad for business. So I came up with a first grade-type solution. Keep trying until you get it right. A perpetual loop that won't throw errors.

Let's say you need to submit a form once a page has loaded and you dont have access to the BODY tag. Create a JavaScript function that consists of a TRY CATCH block, that calls itself when it doesn't work. Basically a recursive function:


function submitOnFormLoad()
	{
	try
		{
		document.myForm.submit();
		}
	catch(e)
		{
		submitOnFormLoad();
		}
	}
submitOnFormLoad();
After and outside of the function block, call the function. It tries to submit the form. If it doesn't work for any reason, it catches the error. Once it catches an error, its instructions are to call itself, which starts the process all over.

Between the recursivness and catch functionality, you have a perpetual loop that won't throw errors. Be careful how you use this. As simple as it seems, in the wrong logical conditions, it could run forever and hang your client's browser.

Michael Sanchez
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs