js技巧汇总

================== 屏蔽页面的js错误================
<script language='javascript'> 
function resumeerror() {return true;} 
window.οnerrοr=resumeerror; 
</script> 

================= 屏蔽页面右键、文本选择、复制等功能===============
<script type="text/javascript">
document.body.οncοntextmenu=function(){return false;};
document.body.οndragstart=function(){return false;};
document.body.onselectstart=function(){return false;};
document.body.onbeforecopy=function(){return false;};
document.body.οnselect=function(){document.selection.empty();};
document.body.οncοpy=function(){document.selection.empty();};
</script>

================= iframe自适应子页面的高度 ===============
<iframe name="mainFrame" width=100 scrolling=no src="main.html" onload="this.height=mainFrame.document.body.scrollHeight"></iframe>
或者在子页面中添加
window.parent.document.getElementByIdx_x_x(" mainFrame").style.height = document.body.scrollHeight+420;
420是 父页面比子页面高的高度

================= 

window.parent与window.opener的区别

 ===============

window.parent & window.opener

window.parent针对frame

父页面:

<html> 
<head><title>父页面</title></head> 
<body> 
<form name="form1" id="form1">

<input type="text" name="username" id="username"/>

</form> 
<iframe src="b1.html" width=100%></iframe> 
</body> 
</html>

子页面:

<script type="text/javascript"> 
function change(){
var _parentWin = window.parent ; 
_parentWin.form1.username.value = "来自子窗口 的参数" ; }
</script> 
<input type ="button" οnclick='change()'>click</input>

 

 

window.opener针对的是打开子窗口 的父窗口

注释:只有表示顶层窗口的 Window 对象的 operner 属性才有效,表示框架的 Window 对象的 operner 属性无效。

父页面:

<script type="text/javascript"> 
function openSubWin() 

var _width = 300 ; 
var _height = 200 ; 
var _left = (screen.width - _width) / 2 ; 
var _top = (screen.height - _height) / 2 ; 
window.open("b2.html",null, 
"height=" + _height + ",width=" + _width + ",status=no,toolbar=no,menubar=no,location=no,resizable=yes,left=" + _left + ",top=" + _top); 

</script> 
<form name="form1">
<input type="text" name="username" id="username"/> 
<input type="button" value="弹出子页面" onClick="openSubWin();"> 
</form>

 

子页面:

<script type="text/javascript"> 
function UpdateParent() 

var _parentWin = window.opener ; 
_parentWin.form1.username.value = "来自子窗口 的参数" ; 

</script> 
<input type="button" name="button" id="button" value="更新主页面的UserName内容" onClick="UpdateParent();">

 

 

ps:以下刷新父窗口并关闭当前窗口

<script language="JavaScript" type="text/javascript">  function refreshParent() {     window.opener.location.href = window.opener.location.href;     if (window.opener.progressWindow)            window.opener.progressWindow.close();        window.close(); } </script>  <a href="javascript:void(0)" οnclick="refreshParent()">刷新父窗口并关闭当前窗口</a>

 

 

javascript:history.go()和History.back()

 

<input   type=button   value=刷新   οnclick="window.location.reload()">  
  <input   type=button   value=前进   οnclick="window.history.go(1)">  
  <input   type=button   value=后退   οnclick="window.history.go(-1)">  
  <input   type=button   value=前进   οnclick="window.history.forward()">  
  <input   type=button   value=后退   οnclick="window.history.back()">
 
后退+刷新
<input   type=button   value=后退   οnclick="window.history.go(-1);window.location.reload()">

 

history.back()是回上一页
i=1
history.go(i)去指定的某页
如果是history.go(0)那就是刷新


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值