2 Submit Buttons

Keywords: javascript, submit, button

<script language="JavaScript">
<!--
function NavSubmit(in1)
{
document.navigate.nav_button.value = in1;
document.navigate.submit();
}
// -->
</script>

<input type="submit" value="Add" onClick="NavSubmit('add')">
<input type="submit" value="View" onClick="NavSubmit('view')">

<input type="hidden" name="nav_button">

Ad Rotator

Keywords: ad, rotator
<layer id="placeholderlayer" class="cycle_image_anchor"></layer><div id="placeholderdiv"></div>


<script language="JavaScript1.2">
var howOften = 5; //number often in seconds to rotate
var current = 0; //start the counter at 0
var ns6 = document.getElementById&&!document.all; //detect netscape 6

// place your images, text, etc in the array elements here
var items = new Array();
    items[0]="<a href='link.htm' ><img alt='image0 (9K)' src=' /Images/Rotator/image0.jpg' height='300' width='300' border='0' /></a>"; //a linked image
    items[1]="<a href='link.htm'><img alt='image1 (9K)' src='/Images/Rotator/image1.jpg' height='300' width='300' border='0' /></a>"; //a linked image
    items[2]="<a href='link.htm'><img alt='image2 (9K)' src='/Images/Rotator/image2.jpg' height='300' width='300' border='0' /></a>"; //a linked image
function rotater() {
    document.getElementById("placeholder").innerHTML = items[current];
    current = (current==items.length-1) ? 0 : current + 1;
    setTimeout("rotater()",howOften*1000);
}

function rotater() {
    if(document.layers) {
        document.placeholderlayer.document.write(items[current]);
        document.placeholderlayer.document.close();
    }
    if(ns6)document.getElementById("placeholderdiv").innerHTML=items[current]
        if(document.all)
            placeholderdiv.innerHTML=items[current];

    current = (current==items.length-1) ? 0 : current + 1; //increment or reset
    setTimeout("rotater()",howOften*1000);
}
window.onload=rotater;
//-->
</script>

Alert

Keywords: javascript, alert
<script language="javascript">
<!--
alert("Hello there!");
//-->
</script>

<script language="javascript">
<!--
alert("Hello there!");window.location="DataEntry.asp"
//-->
</script>

ASP

Keywords: javascript, asp
<%
Response.write "<Script Language=" & chr(34) & "JavaScript" & chr(34) & ">"
Response.write "alert(" & chr(34) & "Access Denied!" & chr(34) & ");" 
Response.write "</Script>"
%>

<%
Response.write "<Script Language=" & chr(34) & "JavaScript" & chr(34) & ">"
Response.write "alert(" & chr(34) & "Access Denied!" & chr(34) & ");" 
Response.write "window.location = " & chr(34) & "DataEntry.asp" & chr(34) & ""
Response.write "</Script>"
%>

Banner

Keywords: javascript, banner
<script language="JavaScript">
<!--
images = new Array(2);

images[0] = "<a href='http://www.one.com'><img src='images/banner1.gif' border=0></a>";

images[1] = "<a href='http://www.two.com'><img src='images/banner2.gif' border=0></a>";

index = Math.floor(Math.random() * images.length);
// -->
</script>		

<script language="JavaScript">
<!--
document.write(images[index]);
//-->
</script>
<noscript><img src="images/banner1.gif" border="0"></noscript>

Browser Detection

Keywords: javascript, browser detection
<script language="JavaScript">
<!--
function whatbrowser
{
        if(document.layers)
        {
            thisbrowser="NN4";
        }
        if(document.all)
        {
             thisbrowser="IE";
        }
        if(!document.all && document.getElementById)
        {
             thisbrowser="NN6";
        }
}
// -->
</script>

Button: Cancel

Keywords: javascript, button, cancel, asp, onClick, HTTP_REFERER
<%
v_sReferer = Request.ServerVariables("HTTP_REFERER")
%>

<script language="JavaScript">
<!--
function Cancel()
{
location = '<%= v_sReferer %>'
}
// -->
</script>

<input type="button" value="Cancel" onClick="Cancel()">

OR

<input type="button" value="Back" onClick='history.go(-1);'>

Checkbox: Check All

Keywords: javascript, checkbox, check all
<script language="JavaScript">
<!--
var checked = false;

function checkAll(field) {
if (field) {
if (!checked) {
for (i = 0; i < field.length; i++)
field[i].checked = true;
checked = true;
}
else {
for (i = 0; i < field.length; i++)
field[i].checked = false;
checked = false;
}
}
}
// -->
</script>

<input type="checkbox" name="CheckAll" onClick="checkAll(document.form1.checkbox_delete)">

Count Characters

Keywords: javascript, count, length, limit, validation
<script language="JavaScript">
<!--
function ShowCount()
{
var count = document.form1.Message.value.length;
eval("document.form1.CharacterCount").value = count;
if (count > 120)
{
alert("Please limit your message to under 120 characters.");
}
}
// -->
</script>

Date Set

Keywords: javascript, date
<script language="JavaScript">
<!--
function SetDate()
{
var today = new Date();
document.form1.date.value = today;
}
// -->
</script>

Dropdown: Navigation

Keywords: javascript, dropdown, navigation, jump to page, location
<script language="JavaScript">
<!--
function JumpToPage()
{
	var myindex = document.welcome.NumPageList.selectedIndex;
	var iPage   = document.welcome.NumPageList.options[myindex].value;

	var sURL    = "/lite/inbox.asp?Page=" + iPage + "&Folder=<%= Escape(m_sFolder) %>"
	
	location = sURL;
}
// -->
</script>

Dropdown: Populate from 2 Textboxes

Keywords: javascript, dropdown, select, populate

Select value from 2 textboxes and populate a dropdown (check if exists already first). This example allows the user to enter a name in one textbox and an email in another, click a button and both will show up in the expanded dropdown. The expanded dropdown is a selection list for later submission.


<script language="JavaScript">
<!--
function AddAttendee()
{
 // loop through attendee list
 var lastone = 1;
 while (document.form1.attendee_list.options[lastone])
 {
  // exists already?
  if (document.form1.attendee_list.options[lastone].text.toLowerCase().indexOf
   ("(" + document.form1.emailadd.value.toLowerCase() + ")") > 0)
  {
   alert("This attendee already exists.");
   return;
  }
  else
  {
   lastone ++;
  }
 }
 // add to bottom
 document.form1.attendee_list.options[lastone] = new Option(document.form1.nameadd.value + "
  (" + document.form1.emailadd.value.toLowerCase() + ")","!" + document.form1.nameadd.value +
  "!" + document.form1.emailadd.value.toLowerCase() + "!2");
 document.form1.emailadd.value = ""; //BLANK THE EMAIL FIELD
 document.form1.nameadd.value = ""; //BLANK THE NAME FIELD
}
// -->
</script>

<form method="post" action="test.asp" name="form1">
<input type="text" size="30" name="nameadd" maxlength="150"><br><br>
<input type="text" size="30" name="emailadd" maxlength="150"><br><br>
<select name="attendee_list" size="11" style="font-size:11px">
 <option>-- Add Attendees --</option>
</select>
<br><br>
<input type="button" value="Add" onClick="AddAttendee()">
</form> 

Enter Key: Submit Form

Keywords: javascript, enter key, submit form, KeyCode

<script language="JavaScript">
<!--
NS4 = (document.layers) ? true : false;

function checkEnter(event)
{
var code = 0;

if (NS4)
code = event.which;
else
code = event.keyCode;
if (code==13)
document.myform.submit();
}
// -->
</script>

<input type="text" name="User" size="15" onKeyPress="checkEnter(event)">

Eval

Keywords: javascript, eval
<script language="JavaScript">
<!--
function ShowCode() 
{
	if ((document.main_form.CCode.options[0].selected) !== true)
	{
		var code = eval("document.main_form.CCode").value;
		eval("document.main_form.CountryCode").value = code;
	}
}
// -->
</script>	

<select name="CCode" onChange="ShowCode()"></select>

Expand/Collapse

Keywords: javascript, expand, collapse
<script language="JavaScript1.2">
<!--
var ns6=document.getElementById&&!document.all?1:0

var head="display:''"
var folder=''

function expandit(curobj){
	folder=ns6?curobj.nextSibling.nextSibling.style:document.all[curobj.sourceIndex+1].style
	if (folder.display=="none")
		folder.display=""
	else
		folder.display="none"
}
//-->
</script>
<h3 style="cursor:hand" onClick="expandit(this)">Header 1</h3>
<span style="display:none" style=&{head};>
	This is content under Header 1.
</span>
	
<h3 style="cursor:hand" onClick="expandit(this)">Header 2</h3>
<span style="display:none" style=&{head};>
	This is content under Header 2.
</span>

Focus

Keywords: javascript, focus, body, onload
<body onload="document.Form1.txtUserName.focus()">

Format Number

Keywords: javascript, format, number
<script language="JavaScript">
<!--
function PaymentCalculate(form)
{
var pmtValue;
pmtValue = form.price.value / form.ir.value;
form.pmt.value = pmtValue.toFixed(2);
}
// -->
</script>

Images: Preload

Keywords: javascript, images, preload
<script language="JavaScript">
<!--
if (document.images)
{
  pic1= new Image(100,25);
  pic1.src="http://someplace.com/image1.gif";

  pic2= new Image(240,55);
  pic2.src="http://someplace.com/image2.gif";

  pic3= new Image(88,31);
  pic3.src="http://someplace.com/image3.gif";
}
//-->
</script>

JQuery: Hide/Show

Keywords: jquery, hide show
onclick="showPinBox()"


<script type="text/javascript">
    function showPinBox()
    {
        $('#pinBox').show();
    }
</script>

Popup Window

Keywords: javascript, popup, window

Popup a new window
<script language="javascript">
<!--
var window_name = window.open("window_test.htm", "window_name", "width=400, height=350");
//-->
</script>   

 
Close window
<a href="javascript:close()">Close Window</a>

Popup: Confirm

Keywords: javascript, popup, confirm
<script language="Javascript">
<!--
function DeleteConfirm()
{
// confirm prompt
if (confirm("Are you sure? \n\n Click OK to continue."))
{
return true;
}
return false;
}
//-->
</script>

<form name="delete" action="admin.asp" method="post" onSubmit="return DeleteConfirm(this)">

Popup: Prompt

Keywords: javascript, popup, prompt
<script language="javascript">
<!--
var the_name = prompt("What's your name?", "Enter your name here");

document.write(the_name);
//-->
</script>

Print

Keywords: javascript, print


<script language="JavaScript">
<!--
function printWindow()
{
   bV = parseInt(navigator.appVersion)
   if (bV >= 4) window.print()
}
// -->
</script>

<a href="javascript printWindow()">Print This Page</a>     

OR
 
<script language="JavaScript">
<!--
function printthis()
{
 print();
}
// -->
</script>

<a href='#' onclick='javascript:printthis();'><img src="print.gif"></a>

RadioButton: Selected

Keywords: javascript, radiobutton, selected

<script language="javascript">
<!--
function radioLoop()
{
// get card type radio value
var selected_card = "";
for (var loop=0; loop < window.document.form1.card_type.length; loop++)
{
if (window.document.form1.card_type[loop].checked == true)
{
selected_card = window.document.form1.card_type[loop].value;
}
}

// pass values to new window
var popupURL = "step1.asp?Option=" + selected_card

// open new window
var step1_window = window.open(popupURL, "_parent")
}
//-->
</script>

Redirect

Keywords: javascript, redirect
<script language="JavaScript">
<!--
setTimeout('Redirect()', 5000);

function Redirect()
{
location.href = 'http://www.mysite.com';
}
// -->
</script>

Set Values

Keywords: javascript
<%
v_sBox1 = "close"
v_sBox2 = "close"
v_sBox3 = "close"
%>

<script language="JavaScript">
<!--
function setValues(in1,in2,in3)
{
	document.form1.box1.value = in1;
	document.form1.box2.value = in2;
	document.form1.box3.value = in3;
	document.form1.submit();
}
// -->
</script>

<form action="test.asp" method="post" name="form1">
<a href="javascript:setValues('open','<%= v_sBox2 %>','<%= v_sBox3 %>')">Box 1</a> <br>
<a href="javascript:setValues('<%= v_sBox1 %>','open','<%= v_sBox3 %>')">Box 1</a> <br>
<a href="javascript:setValues('<%= v_sBox1 %>','<%= v_sBox2 %>','open')">Box 1</a>
<input type="hidden" name="box1" value="">
<input type="hidden" name="box2" value="">
<input type="hidden" name="box3" value="">
</form>

Textbox: Populate

Keywords: javascript, textbox, populate

<script language="JavaScript">
<!--
function PopulateTextbox()
{
if ((document.form1.Server.options[0].selected) !== true)
{
var server = eval("document.form1.Server").value;
eval("document.form1.ServerName").value = server;
}
}
// -->
</script>

Timezone

Keywords: javascript, timezone, .getTimezoneOffset
<script language="JavaScript">
<!--
// from user's machine
alert(new Date().getTimezoneOffset() / 60)
// -->
</script>

Validate Form

Keywords: javascript, validate, validation, form
<script language="JavaScript">
<!--
function ValidateForm()
{
// check first name textbox
if (!(document.form1.fname.value))
{
alert("Please enter your first name.");
document.form1.fname.focus();
return false;
}
// check state dropdown
if ((document.form1.state.options[0].selected) == true)
{
alert("Please select a state.");
document.form1.state.focus();
return false;
}
// check gender radio button
if( !(document.form1.gender[0].checked) && !(document.form1.gender[1].checked))
{
alert("Please select your gender.");
return false;
}
// check agree checkbox
if(!(document.form1.agree.checked))
{
alert("Please check the "agree" checkbox.");
return false;
}
// if offer exists
if (document.form1.offer)
{
// check offer checkbox
if(!(document.form1.offer.checked))
{
alert("Please check the \"offer\" checkbox.");
return false;
}
}
// check users textbox > 50
if (document.form1.pangia_users.value > 50)
{
alert("Please enter a number of users that is less than 50.");
document.form1.pangia_users.focus();
return false;
}
        // no need for "@" in username
if ((inform.form1.value.indexOf("@") > -1))
{
  alert("Please only enter the portion of your email address before the '@' symbol.");
  document.form1.username.focus();
  return false;
}
return true;
}
// -->
</script>

<form name="form1" action="feedback.asp" method="post" onSubmit="return ValidateForm()">

Validation: Allowed Characters

Keywords: javascript, validation, allowed characters
<script language="JavaScript">
<!--
function validcharsinstring(instring,allowedchars)
{ 
	if (instring.length > 0)
	{
		for (x=0; x < instring.length; ++x)
		{
			if (allowedchars.indexOf(instring.charAt(x)) == -1)
			{
				return false;
			}
		}
		return true;
	}
	else
	{
		return false;
	}
}


function ValidateForm()
{

	// check users textbox invalid char
	if (!validcharsinstring(document.form1.pangia_users.value.toLowerCase(),'0123456789'))
	{
		alert("Please use only numbers for this entry");
		document.form1.pangia_users.focus();
		return false;
	}
	return true;
}
//-->
</script>
 
<form name="form1" action="feedback.asp" method="post" onSubmit="return ValidateForm()">

Validation: Date Range

Keywords: javascript, date range, validation, between dates

<script language="JavaScript">
<!--
function ValiDate(oTextBox)
{
var sd = "8/25/2003"
var ed = "10/25/2003"

//s,e and i are arrays ([2]=yyyy,[0]=mm,[1]=dd)
//s being the start date array
//e being the end date array
//i being the user input array
var s = sd.split('/')
var e = ed.split('/')
var i = oTextBox.value.split('/')

//create new date objects using constructed arrays
var startdate = new Date(s[2],s[0],s[1]);
var enddate = new Date(e[2],e[0],e[1]);
var inputdate = new Date(i[2],i[0],i[1]);

//run the comparison
if(inputdate.getTime() < startdate.getTime() || inputdate.getTime() > enddate.getTime())
{
alert("Both 'Assigned Date' and 'Due Date' must range between 8/25/2003 and 10/25/2003");
return false;
}
else
return true;
}
// -->
</script>

<form name="form1" action="#" method="post">
<input type="text" name="date" onChange="return ValiDate(this)"><br><br>
<input type="submit" value="Submit">
</form>

Validation: Required Character

Keywords: javascript, validation, required character, indexOf, valid domain

<script language="JavaScript">
<!--
if (document.form1.domainlist.value.indexOf('.') == -1)
{
alert("Please enter your domain(s) in the following format... \n\n domain1.com, domain2.com, domain3.com");
document.form1.domainlist.focus();
return false;
}
return true;
// -->
</script>

Validation: Required Field

Keywords: javascript, validation, required field, IsEmpty


<script language="JavaScript">
<!--
function IsEmpty(str)
{
 while (str.substring(0,1) == ' ') str = str.substring(1);
 
 if (str.length > 0)
   return false;
 else
  return true;
}

function ValidateForm()
{

 // name
 if (IsEmpty(document.form1.name.value))
 {
  alert("Please enter your name.");
  document.form1.name.focus();
  return false;
 }
 return true;
}
// -->
</script> 

 
HTML
<form name="form1" action="feedback.asp" method="post" onSubmit="return ValidateForm()">