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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS