This article is given by my friend kathir. Thanks for it
Math Function
Method Function What it Does
abs(x) Returns the absolute value of the variable x.
cos(x) Returns the cosine of the variable x.
log(x) Returns the natural log of the variable x.
max(x,z) Returns the larger of the two variables x and z.
min(x,z) Returns the smaller of the two variables x and z.
pow(x,z) Returns the value of the variable x to the zth power.
random() Returns a random number between 0 and 1.
round(x) Returns the variable x rounded to the nearest integer.
sin(x) Returns the sine of the variable x.
sqrt(x) Returns the square root of the variable x.
tan(x) Returns the tangent of the variable x.
JavaScript For SplitMethod
function divide_string()
{
var where_is_mytool="home/mytool/mytool.cgi";
var mytool_array=where_is_mytool.split("/");
alert(mytool_array[0]+" "+mytool_array[1]+" "+mytool_array[2]);
}
Yes - No Dialog Box (confirm)
function Conform()
{
var x=window.confirm("Are you sure you are ok?")
if (x)
window.alert("Good!")
else
window.alert("Too bad")
}
CharAt()
function Test()
{
var my_car="Ferrari";
var the_char=my_car.charAt(1);
alert('The 1st character is '+the_char+'.');
}
indexOf()
function Test()
{
var my_car="Ferrari";
var where_is_a=my_car.indexOf('a');
alert('The a is at position '+where_is_a+'.');
}
To Generate Random Number between (0-4)
function Test()
{
var ran_number=Math.floor(Math.random()*5);
alert(ran_number);
}
Array in JavaScript
var quote= new Array(5)quote[0]="I like JavaScript.";
quote[1]="I used to like Java.";
quote[2]="JavaScript rules.";
quote[3]="Help! JavaScript Error!";
quote[4]="Just Kidding.";
The SetTimeout Function
function Test()
{
document.getElementById("txtSubMenuURL").value="Hey! I have changed!";
setTimeout("moretext()",1000);
}
function moretext()
{
alert("dsd"); document.getElementById("txtSubMenuURL").value="I just change with the time!";
}
Opening And Closing a New Window
To open a new window, you will need to use yet another ready-made JavaScript function. Here is what it looks like:window.open('url to open','window name','attribute1,attribute2')
This is the function that allows you to open a new browser window for the viewer to use. Note that all the names and attributes are separated with a comma rather than spaces. Here is what all the stuff inside is:
- 'url to open'
This is the web address of the page you wish to appear in the new window. - 'window name'
You can name your window whatever you like, in case you need to make a reference to the window later. - 'attribute1,attribute2'
As with alot of other things, you have a choice of attributes you can adjust.
Below is a list of the attributes you can use:
- width=300
Use this to define the width of the new window. - height=200
Use this to define the height of the new window. - resizable=yes or no
Use this to control whether or not you want the user to be able to resize the window. - scrollbars=yes or no
This lets you decide whether or not to have scrollbars on the window. - toolbar=yes or no
Whether or not the new window should have the browser navigation bar at the top (The back, foward, stop buttons..etc.). - location=yes or no
Whether or not you wish to show the location box with the current url (The place to type http://address). - directories=yes or no
Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...). - status=yes or no
Whether or not to show the window status bar at the bottom of the window. - menubar=yes or no
Whether or not to show the menus at the top of the window (File, Edit, etc...). - copyhistory=yes or no
Whether or not to copy the old browser window's history list to the new window.
<FORM>
<INPUT type="button" value="New Window!" onClick="window.open('http://www.pageresource.com/jscript/jex5.htm','mywindow','width=400,height=200')">
</FORM>
Test it out below:
Yes, you got a 400 by 200 window with some writing in it!
Some Important Rules
Before we move on, we need to make note of some things so you won't go insane like I did trying to get this to work right!
- When you get to the INPUT tag, keep everything in that tag on one single line in your text editor, including the javascript commands. (The text goes to the next line on this page so you can print it out easily).
- Once you come to the onClick=" ", don't leave any spaces between anything. Just use the commas and the quote marks. Any white space will keep it from working correctly in Netscape.
- Don't put quote marks around the yes, no, or numbers for the attributes. You only use single quotes around the entire set of attributes.
- In some browsers, you may need to substitute the number 1 for yes, and the number zero for no in the attributes section. The yes or no should work fine, though.
Okay, enough rules. Let's look at the code that makes a completely new browser! Basically, you just use yes for all of the attributes. Here is the code:
<FORM>
<INPUT type="button" value="New Window!" onClick="window.open('http://www.pageresource.com/jscript/jex5.htm','mywindow','width=400,height=200,toolbar=yes,
location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,
resizable=yes')">
</FORM>
Give it a try, this window has all the features!
Remember, keep everything on one line....one really, really long line! I just put the sample code on new lines so you wouldn't have to scroll forever to read everything........and your printer won't go crazy now either!
Closing a New Window
Hmm.....what's with the "Close Window" button you saw in the new window? How does one do do that? To use that trick, use the window.close() function in the HTML of the new window. Just put this code wherever you want the close button to show up in the new window:
<FORM>
<INPUT type="button" value="Close Window" onClick="window.close()">
</FORM>
Of course, the window can be closed with the "x" symbol on the top-right of the window as well.
Set the Window Position
There is another set of options you can use to set the position of the new window on the viewers, but it only works with NS4+ and IE4+:
- screenX=number in pixels
Sets the position of the window in pixels from the left of the screen in Netscape 4+. - screenY=number in pixels
Sets the position of the window in pixels from the top of the screen in Netscape 4+. - left=number in pixels
Sets the position of the window in pixels from the left of the screen in IE 4+. - top=number in pixels
Sets the position of the window in pixels from the top of the screen in IE 4+.
<FORM>
<INPUT type="button" value="New Window!" onClick="window.open('jex5.htm','mywindow','width=400,height=200,left=0,top=100,screenX=0,screenY=100')">
</FORM>
Now, that is a lot of work- but you can now customize a new window for your viewers!
JavaScript Browser Detection
Function Test()
{
var browserName=navigator.appName;
if (browserName=="Netscape")
{
alert("Hi Netscape User!");
}
else
{
if (browserName=="Microsoft Internet Explorer")
{
alert("Hi, Explorer User!");
}
else
{
alert("What ARE you browsing with here?");
}
}
}
You can do the same thing with the navigator.appVersion, except you will most likely want to grab just the integer from the version information (2,3,4, etc.). To do this, we use the parseInt() function: var browserVer=parseInt(navigator.appVersion);
Dynamically Changing Time
<HEAD>
<SCRIPT language="JavaScript">
<!--
function startclock()
{
var thetime=new Date();
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getYear();
var AorP=" ";
if (nhours>=12)
AorP="P.M.";
else
AorP="A.M.";
if (nhours>=13)
nhours-=12;
if (nhours==0)
nhours=12;
if (nsecn<10)
nsecn="0"+nsecn;
if (nmins<10)
nmins="0"+nmins;
if (nday==0)
nday="Sunday";
if (nday==1)
nday="Monday";
if (nday==2)
nday="Tuesday";
if (nday==3)
nday="Wednesday";
if (nday==4)
nday="Thursday";
if (nday==5)
nday="Friday";
if (nday==6)
nday="Saturday";
nmonth+=1;
if (nyear<=99)
nyear= "19"+nyear;
if ((nyear>99) && (nyear<2000))
nyear+=1900;
document.clockform.clockspot.value=nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear;
setTimeout('startclock()',1000);
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<FORM name="clockform">
Current Time: <INPUT TYPE="text" name="clockspot" size="40">
</FORM>
<SCRIPT language="JavaScript">
<!--
startclock();
//-->
</SCRIPT>
</BODY>
Drop Down Box
<FORM name="guideform">
<SELECT name="guidelinks" onChange="window.location=document.guideform.guidelinks.options[document.guideform.guidelinks.selectedIndex].value"> <OPTION SELECTED value="jdrop2.htm">--Choose--
<OPTION value="jex15.htm">Page 1
<OPTION value="jex16.htm">My Cool Page
</SELECT>
</FORM>
String Function
JavaScript anchor Function
The anchor function can be used to wrap the string into a link anchor.<html>
<head>
<script type="text/javascript">
var strLinkAnchor = new String("top")
strLinkAnchor = strLinkAnchor.anchor("top")
document.write(strLinkAnchor)
</script>
</head>
<body></body>
</html>
Output
topJavaScript big Function
The big function can be used to make to string to appear in <big> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.big("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
topJavaScript blink Function
The blink method can be used to make the string blink. This function only works in netscape. IE doesn’t support it yet.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.blink("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
topJavaScript bold Function
This function is used to wrap the string into the <bold> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.bold("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
topJavaScript fixed Function
This function is used to wrap the string in the <TT> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.fixed("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
topJavaScript italics Function
This function wraps the string in the <i> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.italics("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
topJavaScript small Function
This function is used to wrap the string into the <small> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.small("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
topJavaScript strike Function
This function is used to wrap the string into the <strike> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.strike("top")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
JavaScript sub Function
This function is used to wrap the string in the <sub> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.sub("top")
document.write(str)
</script>
</head>
<body>Normal Text</body>
</html>
Output
top Normal TextJavaScript sup Function
This function can be used to wrap the string in the <sup> tag.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.sup("top")
document.write(str)
</script>
</head>
<body>Normal Text</body>
</html>
Output
top Normal TextJavaScript charAt Function
This function can be used to retrieve the character at the given index.<html>
<head>
<script type="text/javascript">
var str = new String("top")
str = str.charAt(1)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
oJavaScript charCodeAt Function
This function can be used to convert the char at the given index to the ASCII value.<html>
<head>
<script type="text/javascript">
var str = new String("ABC")
str = str.charCodeAt(0)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
65JavaScript indexOf Function
This function can be used to return the index of the given character.<html>
<head>
<script type="text/javascript">
var str = new String("ABC")
str = str.indexOf("B")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
1JavaScript lastIndexOf Function
This function can be used to return the last index of a given character.<html>
<head>
<script type="text/javascript">
var str = new String("ABCB")
str = str.lastIndexOf("B")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
3JavaScript link Function
The link method can be used to create a link with the specified string as the URL.<html>
<head>
<script type="text/javascript">
var str = new String("TopXML")
str = str.link("http://www.topxml.com")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
TopXMLJavaScript concat Function
The concat function can be used to concat a string to another one.<html>
<head>
<script type="text/javascript">
var str = new String("Hello")
str = str.concat(" World")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
Hello WorldJavaScript fromCharCode Function
This function can be used to return a string from a number of Unicode character values. This function cannot be called with a string object, instead use String.formCharCode.<html>
<head>
<script type="text/javascript">
str = String.fromCharCode(65, 66, 67)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
ABCJavaScript split Function
The split function can be used to split a string into an array of sub strings.<html>
<head>
<script type="text/javascript">
str = new String("0,1,2,3")
str = str.split(",")
document.write(str[0]+"<br/>")
document.write(str[1]+"<br/>")
document.write(str[2]+"<br/>")
document.write(str[3]+"<br/>")
</script>
</head>
<body></body>
</html>
Output
01
2
3
JavaScript slice Function
The slice function can be used to slice a specific section and return a new string.<html>
<head>
<script type="text/javascript">
str = new String("Hello World")
str = str.slice(5,11)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
WorldJavaScript substring Function
The function substring can be used to get a specific section from a string. It basically works the same as the slice function.<html>
<head>
<script type="text/javascript">
str = new String("Hello World")
str = str.substring(5,11)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
WorldJavaScript substr Function
The function substr can be used to get a specific section from a string. It works the same as slice and substring.<html>
<head>
<script type="text/javascript">
str = new String("Hello World")
str = str.substr(5,11)
document.write(str)
</script>
</head>
<body></body>
</html>
Output
WorldJavaScript match Function
The match function can be used to match a regular expression against a string. It returns an array of the matches.<html>
<head>
<script type="text/javascript">
str = new String("Hello World")
str = str.match("H")
for(i=0;i<str.length;i++)
{
document.write(str[i]+"<br/>")
}
</script>
</head>
<body></body>
</html>
Output
HJavaScript replace Function
The function replace can be used to replace a string with another string. A regular expression can be used to find the string.<html>
<head>
<script type="text/javascript">
var str = new String("Hello World")
document.write(str+"<br/>")
var regEx = new RegExp ('World', 'gi') ;
str = str.replace(regEx, 'User')
document.write(str)
</script>
</head>
<body></body>
</html>
Output
Hello WorldHello User
JavaScript search Function
This function can be used to in conjunction with a regular expression to search for a specific format.<html>
<head>
<script type="text/javascript">
strWrongEmail = new String("sonu_sonu.com")
emailRegEx = /^[^@]+@[^@]+.[a-z]{2,}$/i
if(strWrongEmail.search(emailRegEx) == -1){
document.write("Email is not valid")
}
else
document.write("Email is valid")
</script>
</head>
<body></body>
</html>
Output
Email is not validJavaScript toLowerCase Function
This function can be used to convert the string to lower case.<html>
<head>
<script type="text/javascript">
str = new String("HELLO WORLD")
str = str.toLowerCase()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
hello worldJavaScript toUpperCase Function
This function is used to convert the string to upper case.<html>
<head>
<script type="text/javascript">
str = new String("hello world")
str = str.toUpperCase()
document.write(str)
</script>
</head>
<body></body>
</html>
Output
HELLO WORLDJavaScript fontcolor Function
The fontcolor function can be used to wrap the string in the <font> tag, which changes the color of the string.<html>
<head>
<script type="text/javascript">
str = new String("hello world")
str = str.fontcolor("red")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
hello worldJavaScript fontsize Function
The fontsize function can be used to change the font size of the string.<html>
<head>
<script type="text/javascript">
str = new String("hello world")
str = str.fontsize("12")
document.write(str)
</script>
</head>
<body></body>
</html>
Output
hello world
02:53 |
Category:
Javascript
|
0
comments
Comments (0)