History   |   Planning   |   HTML/CSS   |   JavaScript   |   ASP   |   .Net 1.0   |   .Net 2.0   |   .Net 3.0   |   C#   |   Java   |   SQL   |   XML   |   GIS   |   Software   |   Glossary
JavaScript
    - Alert

    - Allowed Chars

    - Banner

    - Browser

    - Buttons

    - Checkbox

    - Count

    - Confirm

    - Date

    - Dropdown

    - Enter Key

    - Eval

    - Find Char

    - Focus

    - Format

    - Is Empty

    - OnBeforeUnload

    - Populate

    - Popup

    - Preload

    - Print

    - Prompt

    - Radio

    - Redirect

    - Set Values

    - Timezone

    - Validate

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

<script language="javascript">
<!--
alert("Hello there!");window.location="DataEntry.asp"
//-->
</script>
Using 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>"
%>
Jump to Top
JavaScript : Allowed Chars
JS
<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>
HTML
<form name="form1" action="feedback.asp" method="post" onSubmit="return ValidateForm()">
Jump to Top
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>
Jump to Top
JavaScript : Browser
<script language="JavaScript">
<!--
function whatbrowser
{
        if(document.layers)
        {
            thisbrowser="NN4";
        }
        if(document.all)
        {
             thisbrowser="IE";
        }
        if(!document.all && document.getElementById)
        {
             thisbrowser="NN6";
        }
}
// -->
</script>			
Jump to Top
JavaScript : Buttons
2 Submit Buttons
<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">	
Background Color
<input type="button" value="Cancel" style="background:#E6E6E6">	
Cancel
<%
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);'>	
Disable
<input type="button" disabled value="Change all">
Jump to Top
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)">	
Jump to Top
JavaScript : 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)">			
Jump to Top
JavaScript : Count
<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>			
Jump to Top
JavaScript : Date
Set
<script language="JavaScript">
<!--
function SetDate()
{
	var today = new Date();
	document.form1.date.value = today;
}
// -->
</script>			
Validate if date entered is between 2 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>		
Jump to Top
JavaScript : Dropdown
Navigate
<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>
Jump to Top
JavaScript : Enter Key
Submit form when enter key pressed
<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)">	
Jump to Top
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>
Jump to Top
JavaScript : Find Character
Validate form field has a cretain required character
<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>		
Jump to Top
JavaScript : Focus
<body onload="document.Form1.txtUserName.focus()">	
Jump to Top
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>	
Jump to Top
JavaScript : IsEmpty
JS
<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()">
Jump to Top
JavaScript : OnBeforeUnload
Run a server side process (Web Service) after browser close button click
<body id="mybody"  onbeforeunload="releaseLocks();">

<form id="Form2"  runat="server"> 
	<asp:ScriptManager ID="ScriptManager1" AsyncPostBackTimeout="360000" runat="server">
		<Services>
			<asp:ServiceReference Path="~/WebServices/CertLocking.asmx" />
		</Services>
	</asp:ScriptManager>

<script language="JavaScript">
<!--
var certIsLocked=false;

function releaseLocks(){
	if(document.activeElement.protocol!="http:" ){
		//do nothing - this is a javascript call or the active element 
		//does not have a protocol property
	}
	else if(document.activeElement.protocol=="undefined"){
		//some other tag type?
	}
	else {
		if (certIsLocked==true){
			try{
				CertLocking.UnlockCerts();
			}
			catch(err){
			}                  
		}
                 
	}           
}
// -->
</script>		
Jump to Top
JavaScript : 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>			
Select value from 2 textboxes and populate a dropdown (check if exists already first).
<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 information into some form fields and populate a textbox with the info.
View File
Jump to Top
JavaScript : Popup
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>		
Jump to Top
JavaScript : 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>	
Jump to Top
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>					
<script language="JavaScript">
<!--
function printthis()
{
	print();
}
// -->
</script>

<a href='#' onclick='javascript:printthis();'><img src="print.gif"></a>					
Jump to Top
JavaScript : Prompt
<script language="javascript">
<!--
var the_name = prompt("What's your name?", "Enter your name here");

document.write(the_name);
//-->
</script>		
Jump to Top
JavaScript : Radio
Determine which radio button has been selected (credit card type) and pass that value to a new window.
<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>		
Jump to Top
JavaScript : Redirect
<script language="JavaScript">
<!--
setTimeout('Redirect()', 5000);

function Redirect()
{
	location.href = 'http://www.mysite.com';
}
// -->
</script>			
Jump to Top
JavaScript : Set Values
<%
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>
Jump to Top
JavaScript : Timezone
<script language="JavaScript">
<!--
// from user's machine
alert(new Date().getTimezoneOffset() / 60)
// -->
</script>			
Jump to Top
JavaScript : Validate
<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()">			
Jump to Top
 
Copyright © 2010 12 Bravo