BOM

来源:互联网 发布:点卡充值软件 编辑:程序博客网 时间:2024/06/15 20:49

将窗口移动到屏幕左上角:window.moveTo(0,0) 

将窗口移动到(100,50):window.moveTo(100,50)

将窗口向下移动100像素:window.moveBy(0,100)

将窗口向上移动100像素:window.moveBy(0,-100)

将窗口向左移动100像素:window.moveBy(-100,0)

将窗口向右移动100像素:window.moveTBy100,0)

opera和只能对最外层的window对象使用

浏览器窗口新宽度和高度:window.resizeTo(10,100);

新窗口和原窗口宽度和高度之差:window.resizeBy(10,100);



导航和打开窗口

var w=window.open("http://www.baidu.com","topframe")

调整大小:w.resizeTo(100,100);

移动位置:w.moveTo(100,100);

关闭新打开的窗口w.close();

alert(w.closed;)  //true

alert(w.opener==window);  //true


确定弹出窗口是否被浏览器内置的屏蔽程序阻止

var a=window.open("http://www.baidu.com","_blank");

if(a==null){

alert("the popup was blocked");

}


如果是浏览器扩展或其他程序阻止的弹出窗口,那么window.open()通常会抛出一个错误。因此,要想准确的检测出弹出窗口是否被屏蔽,必须在检测返回值的同时,将对window.open()的调用封装在一个try-catch块中。

var  blocked=false;

try{

var a=window.open("http://www.baidu.com","_blank");

if(a==null){

blocked=true;

}catch (ex){

blocked=true;

}

}

if(blocked){

alert("the popup was blocked");

}

超时调用

setTimeout();两个参数,一个要执行的代码,另一个执行代码前需要等待多少毫秒。

//不建议传递字符串

setTimeout("alert('hello')",1000);

//推荐的调用方式

var timeOutId=setTimeout(function(){

alert('hello'),1000);

},1000);

//取消尚未超时的调用

clearTimeout(timeOutId);

间歇调用

setInterval(function(){

alert('hello')

},1000);


取消间歇调用的重要性远远高于超时调用,因为间歇调用不加干涉的情况下,间歇调用会一直执行到页面卸载

var num=0;
var max=10;
var interval=null;
function a(){num++;if(num==max){clearInterval(interval);alert("done")}};
interval=setInterval(a,1000);


很少真正使用间歇调用,原因是后一个间歇调用可能会在前一个间歇调用结束之前启动,一般都使用超时调用模拟间歇调用

var num=0;

var max=10;

function z(){

num++;

if(num<max){

setTimeout(a,500)

}else{

alert("done");

}

}

setTimeout(z,500);



系统对话框

alert() 、confirm()、prompt()

confirm()对话框包括OK和cancel

if(confirm("Are you sure?")){

alert("yes");

}else{

alert("no");

}

yue文本框

var result=prompt("What's your name?","yue")

if(result!==null){

alert("welcome"+result);

}

//显示打印对话框

window.print();

//显示查找对话框

window.find();


location对象

hash  "#content" 返回URL中的hash(#号后跟0或多个字符)

host  “www.wrox.com.80”  返回服务器名称和端口号 

hostname  "www.wrox.com"  返回服务器名称

href   "http://www.wrox.com"  饭hi当前加载页面的完整url

pathname  "/opj/"   返回url中的目录、文件名

port  “8080”  返回url中指定的端口号,如果url不包含端口号,则这个属性返回空字符串

protocol  "http"   返回页面使用的协议。通常是http:或https

search  "?q=javascript"   返回URL的查询字符串。这个字符串以问号开头


每个查询字符串参数都成了返回对象的属性

<script>
function a(){
var str=location.search.length?location.search.substring(1):[];
var arg={};
var items=str.length?str.split("&"):[];
for(i=0;i<items.length;i++){
var item=items[i].length?items[i].split("="):[];
var name=decodeURIComponent(item[0]);
var value=decodeURIComponent(item[1]);
arg["name"]=value;


}
return arg;


}

</script>
?q=dsd&b=30

alert(arg["q"])  //dsd

alert(arg["b"])  //30

除hash外,每次修改location的属性,页面都会以新url重新加载,且生成一条历史记录

将URL   http://www.wrox.com/wileyCDA  修改为 http://www.wrox.com/wileyCDA/#section1

location.hash="#section1";

将URL   http://www.wrox.com/wileyCDA  修改为 http://www.wrox.com/wileyCDA/?q=section1

location.search="?q=section1";

将URL   http://www.wrox.com/wileyCDA  修改为 http://www.yahoo.com/wileyCDA

location.hostname=http://www.yahoo.com/

将URL   http://www.wrox.com/wileyCDA  修改为 http://www.yahoo.com:8080/wileyCDA

location.port=8080;


导致浏览器位置改变,但不会生成历史记录,在调用replace()后,用户不能回到前一个页面。

<body>
<p>dwedw</p>
</body>
<scripttype="text/javascript">
setTimeout(function(){
location.search="http://www.wrox.com";
},1000);
</script>


reload()重新加载当前显示的页面 ,调用之后的代码可能会也可能不会执行代码,这取决于网络延迟或系统资源,所以,最好将reload()放在最后一行

loacation.reload();  //重新加载(有可能从缓存中加载)
location.reload(true);  //重新加载(从服务器中加载)

检测插件

plugins();

<scripttype="text/javascript">
function chajian(name){
name=name.toLowerCase();
for(i=0;i<navigator.plugins.length;i++){
if(navigator.plugins[i].name.toLowerCase.indexOf(name)>-1){
return true;
}}

return false;

}
</script>

检测IE中的插件ActiveXobject(),IE是使用COM对象实现插件的,而COM对象使用唯一标识符来标识,因此,想要检查特定插件,就必须知道其COM标识符

function chajianIE(name){

try{

new ActiveXobject(name);

return true;

}catch (ex){

return false;

}

}


检测flash

alert(chajianIE("ShockwaveFlash.ShockwaveFlash"));

检测QuickTime

alert(chajianIE("QuickTime.QuickTime"));


典型的做法是针对每个插件分别创建检测函数,而不是使用上面介绍的通用检测方法。


检测所有浏览器中的Flash

function hasFlash(){

var result=chajian("Flsah" );

if(!result){

result=chajianIE("ShockwaveFlash.ShockwaveFlash");

}

return result;

}

alert(hasFLash());

检测所有浏览器中的QuickTime

function hasQuickTime(){

var result=chajian("QuickTime" );

if(!result){

result=chajianIE("QuickTime.QuickTime");

}

return result;

}

alert(hasQuickTime());





原创粉丝点击