javascript中内置函数

一、基本函数库

  split():用于把一个字符串分割成字符串数组

  toUpperCase():

  substr():

  长度

  length()

  拼接(两种)

  +

  concat():合并多个字符串,并返回合并的结果

  查找

  indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置 

  lastIndexOf():可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索

  删除/添加

  shift() :用于把数组的第一个元素从其中删除,并返回第一个元素的值

  pop():用于删除并返回数组的最后一个元素

  unshift() : 可向数组的开头添加一个或更多元素,并返回新的长度

  push():可向数组的末尾添加一个或多个元素,并返回新的长度

  截取

  substr(start,end):在字符串中抽取从 start 下标开始的指定数目的字符

 

  简单例子:

<script type="text/javascript">
var str="i like php";
//拆分,把每个单词得到
var arr=str.split(" ");
//遍历数组,得到第一个字母,大写,放回原处
var word="";
var nstr="";
for(var k in arr){
	word=arr[k][0].toUpperCase()+arr[k].substr(1);
	nstr+=word+" ";
}
document.write(nstr);
</script>

 

 

 

检测浏览器是什么:

<script type="text/javascript">
if(navigator.userAgent.toLowerCase().indexOf("msie")>=0){
	document.write("ie");
}else if(navigator.userAgent.toLowerCase().indexOf('firefox')>=0){
	document.write("huohu");
}else if(navigator.userAgent.toLowerCase().indexOf('chrome')>=0){
	document.write('google');
}
</script>

定时器的小例子(复习一些基础的函数):

  Date()

  setInterval()

  getInterval()

  toLocaleString()

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
<script type="text/javascript">
var i=0;
function fun1(){
	D=new Date();
	document.getElementById("div1").innerHTML=D.toLocaleString();
}
function fun2(){
	window.clearInterval(t);
}
</script>
</head>
<body>
<div id="div1" style="border:1px solid red;"></div>
<input type="button" value="stop" onclick="fun2();"/>
</body>
</html>
<script type="text/javascript">
fun1();
var t=window.setInterval("fun1()",1000);
</script>

得到URL地址:

分析下面的URL: 

            http://www.xxx:8080/test.php?user=admin&pwd=admin#login

    1. location.href:得到整个如上的完整url
    2. location.protocol:得到传输协议http:
    3. location.host:得到主机名连同端口www.xxx.cn:8080
    4. location.hostname:得到主机名www.xxx.cn
    5. location.pathname:得到主机后部分不包括问号?后部分的/test.php
    6. location.search:得到url中问号?之后井号#之前的部分?user=admin&pwd=admin
    7. location.hash:得到#之前的部分#login
<script type="text/javascript">
document.write("href:"+location.href+"<br/>");
//设置当前的主机名和端口号 document.write("host:"+location.host+"<br/>"); document.write("pathname:"+location.pathname+"<br/>"); document.write("search:"+location.search+"<br/>"); document.write("hash:"+location.hash+"<br/>"); location.replace("http://www.baidu.com"); </script>

 js面向对象:http://www.dreamdu.com/javascript/define_object/

posted @ 2015-09-01 09:45  tiandi2050  阅读(328)  评论(0编辑  收藏  举报