자바스크립트 함수
자바스크립트 함수에 대한 설명과 예제 프로그램을 모아두고 있습니다.
[1] Form Validation - Checking for All Letters // If the element's string matches the regular expression it is all letters
function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
[2] Form Validation - Checking for All Numbers // If the element's string matches the regular expression it is all numbers
function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
[3] JavaScript Redirect <script type="text/javascript">
<!--
window.location = "http://www.google.com/"
//-->
</script>
[4] Javascript Print Script - window.print() <form>
<input type="button" value="Print This Page" onClick="window.print()" />
</form>
[5] Events in Javascript <html>
<head>
<script type="text/javascript">
<!--
function popup() {
alert("Hello World")
}
//-->
</script>
</head>
<body>

<input type="button" value="Click Me!" onclick="popup()"><br />
<a href="#" onmouseover="" onMouseout="popup()"">
Hover Me!</a>
</body>
</html>
[6] Example Function in Javascript <html>
<head>
<script type="text/javascript">
<!--
function popup() {
alert("Hello World")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="popup()" value="popup">
</body>
</html>
[7] JavaScript Variables <body>
<script type="text/javascript">
<!--
var linebreak = "<br />"
var my_var = "Hello World!"

document.write(my_var)
document.write(linebreak)

my_var = "I am learning javascript!"
document.write(my_var)
document.write(linebreak)

my_var = "Script is Finishing up..."
document.write(my_var)
//-->
</script>
</body> < .....
[8] Javascript Operators <body>
<script type="text/javascript">
<!--
var two = 2
var ten = 10
var linebreak = "<br />"

document.write("two plus ten = ")
result = two + ten
document.write(result)
document.write(linebreak)


document.write("ten * ten = ")
result = ten * ten
document.write(result)
document.write(linebreak)

document.write("ten / .....
[9] Javascript String Length <script type="text/javascript">
var myString = "123456";

var length = myString.length;

document.write("The string is this long: " + length);

// Same thing, but using the property inside the write function
document.write("<br />The string is this long: " + myString.length);

</script>
[10] Javascript String Split Function <script type="text/javascript">
var myString = "123456789";

var mySplitResult = myString.split("5");

document.write("The first element is " + mySplitResult[0]);
document.write("<br /> The second element is " + mySplitResult[1]);
</script>
[11] Javascript String Search <script type="text/javascript">
var myRegExp = /Alex/;
var string1 = "Today John went to the store and talked with Alex.";
var matchPos1 = string1.search(myRegExp);

if(matchPos1 != -1)
document.write("There was a match at position " + matchPos1);
else
document.write("There was no match in the first string");


</script>
[12] String indexOf <script type="text/javascript">
var aURL = "http://www.tizag.com/";
var aPosition = aURL.indexOf("www");

document.write("The position of www = " + aPosition);

</script>
[13] Comparing one String to another String <script type="text/javascript">
var username = "Agent006";
if(username == "Agent007")
document.write("Welcome special agent 007");
else
document.write("Access Denied!");
document.write("<br /><br />Would you like to try again?<br /><br />");

// User enters a different name
username = "Agent007";
if(username == "Agent007")
document.write(" .....
[14] Replace <script type="text/javascript">
var visitorName = "Happy";
var myOldString = "Hello username! I hope you enjoy your stay username.";
var myNewString = myOldString.replace("username", visitorName);

document.write("Old string = " + myOldString);
document.write("<br />New string = " + myNewString);

</script>
[15] Escape, Unescape Escape 와 Unescape : escape함수는 문자셋을 아스키(ASCII) 값으로 반환하고 unescape함수는 반대로 아스키 값을 문자셋으로 반환

<형식> escape(문자셋), unescape(문자열)

escape 함수의 반환값은 숫자앞에 '%' 기호가 붙어서 출력되고 unescape 함수의 [문자열]은 앞에 '%'를
붙인 다음 숫자값을 넣어 사용해야 한다.
    [1] [2]    
Javascript Source List :