Javascript

Capture page title

12.13.07

Example

Submitting this form will use Javascript to assign the current page's title as it's value.

Code

Whoop dee doo, you say? This technique can be useful when capturing values that aren't available using client side variables. For example, the title of the page is not as easily accessible with ASP alone.

The function should ideally be called using unobtrusive Javascript.

function doExampleForm()
{
	// initialize gotexamplesubmit as what submits the form
	var gotexamplesubmit = document.getElementById("examplesubmit");

	if (gotexamplesubmit)
	{
		gotexamplesubmit.onclick = function()
		{
			submitExampleForm();
		}
	}
}

function submitExampleForm()
{
	// initialize gotexampleform as the form
	var gotexampleform = document.getElementById("exampleform");

	gotexampleform.examplevalue.value = document.title;

	gotexampleform.submit();
}

window.onload = function()
{
	// place any other onload functions here
	doExampleForm();
};

Calling the Javascript function unobtrusively allows for graceful degradation. Notice no inline Javascript in sight!

<form id="exampleform" method="post" action="">
	<fieldset>
		<input type="hidden" name="examplevalue" />
		<input type="button" name="examplesubmit" id="examplesubmit" value="Submit" />
	</fieldset>
</form>

Credits

"submit is not a function" javascript error