js怎么调用计步器 目前我们是wap调用的

JavaScript031

js怎么调用计步器 目前我们是wap调用的,第1张

第一种方式:直接JS脚本

<script type="text/javascript">

try {

var urlhash = window.location.hash

if (!urlhash.match("fromapp"))

{

if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)))

{

window.location="http://www.laozuo.org(更换成自己的WAP网站)"

}

}

}

catch(err)

{

}</script>

第二种方式,稍微长一些,引用

function uaredirect(murl){

try {

if(document.getElementById("bdmark") != null){

return

}

var urlhash = window.location.hash

if (!urlhash.match("fromapp")){

if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {

location.replace(murl)

}

}

} catch(err){}

}

上述的脚本保持到mobile.js文件,然后在需要调用的页面引用下面2个JS调用文件。

js的函数调用会免费奉送两个而外的参数就是 this 和 arguments 。arguments是参数组,他并不是一个真实的数组,但是可以使用.length方法获得长度。

书上有说4中调用方式:

方法调用模式

函数调用模式

构造器调用模式

apply调用模式

下面我们来看看一些实例更好理解。

1:方法调用模式。

请注意this此时指向myobject。

/*方法调用模式*/

var myobject={

value:0,

inc:function(){

alert(this.value)

}

}

myobject.inc()

2:函数调用模式

请注意this此时指向window

/*函数调用模式*/

var add=function(a,b){

alert(this)//this被绑顶到window

return a+b

}

var sum=add(3,4)

alert(sum)

3:构造器调用模式

javascript语言精粹一书建议摒弃这中方式。因为有更好的方式。这里先不介绍。下次发表博文的时候贴出来。

会在这里加一个连接。

/*构造器调用模式 摒弃*/

var quo=function(string){

this.status=string

}

quo.prototype.get_status=function(){

return this.status

}

var qq=new quo("aaa")

alert(qq.get_status())

4:apply调用模式

==我们可以来看一个更有用的apply实例。看最下面的代码。

/*apply*/

//注意使用了上面的sum函数

//与myobject

//这中调用方式的优点在于可以指向this指向的对象。

//apply的第一个参数就是this指针要指向的对象

var arr=[10,20]

var sum=add.apply(myobject,arr)

alert(sum)