首页 > 编程技术 > html

window.parent和window.opener区别

发布时间:2016-9-20 18:58

下面一段代码是关于window.parent和window.opener区别 来讲的,我们如果要用到iframe的值传到另一框架就要用到window.opener.document.getElementById(name).value = uvalue;这种形式哦。

window.parent能获取一个框架的父窗口或父框架。顶层窗口的parent引用的是它本身。

可以用这一点特性来判断这个窗口是否是顶层窗口。如:

Code
function IsTopWindow( win )
{
    if( win.parent == win ) return true;
    else return false;
}
window.opener引用的是window.open打开的页面的父页面。


opener即谁打开我的,比如A页面利用window.open弹出了B页面窗口,那么A页面所在窗口就是B页面的opener,在B页面通过opener对象可以访问A页面。
parent表示父窗口,比如一个A页面利用iframe或frame调用B页面,那么A页面所在窗口就是B页面的parent。
在JS中,window.opener只是对弹出窗口的母窗口的一个引用。比如:

a.html中,通过点击按钮等方式window.open出一个新的窗口b.html。那么在b.html中,就可以通过window.opener(省略写为opener)来引用a.html,包括a.html的document等对象,操作a.html的内容。假如这个引用失败,那么将返回null。所以在调用opener的对象前,要先判断对象是否为null,否则会出现“对象为空或者不存在”的JS错误。

window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击了a.htm上的一个链接而打开了b.htm,然后我们打算在b.htm上输入一个值然后赋予a.htm上的一个id为“name”的textbox中,就可以写为:
window.opener.document.getElementById("name").value = "输入的数据";

下面这种xmlhttp创建方法是一种完全的创建方法哦,下面来看看吧xmlhttp的功能多么的强大哦。

function CreateHTTPObject(){
    var xmlhttp;
   
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            xmlhttp = false;
        }
    }
   
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        }
        catch (e) {
            xmlhttp=false;
        }
    }
   
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        }
        catch (e) {
            xmlhttp=false;
        }
    }
   
    return xmlhttp;
}

下面二种检测浏览器类型的代码程序哦,希望这段代码给你帮助哦。

js检测浏览器类型代码,

方法1

var ua = navigator.userAgent.toLowerCase();
var binfo =
{
    ve : ua.match(/.+(?:rv|it|ra|ie|me)[/: ]([d.]+)/)[1],
    ie : /msie/.test(ua) && !/opera/.test(ua),
    op : /opera/.test(ua),
    sa : /version.*safari/.test(ua),
    ch : /chrome/.test(ua),
    ff : /gecko/.test(ua) && !/webkit/.test(ua)
};

方法2

var ua = navigator.userAgent.toLowerCase(), na = '';
var binfo =
{
    ie : (na = ua.match(/msie ([d.]+)/)) ? na[1] : 0,
    ff : (na = ua.match(/firefox/([d.]+)/)) ? na[1] : 0,
    ch : (na = ua.match(/chrome/([d.]+)/)) ? na[1] : 0,
    op : (na = ua.match(/opera.([d.]+)/)) ? na[1] : 0,
    sa : (na = ua.match(/version/([d.]+).*safari/)) ? na[1] : 0
}

 

下面一段代码是使用js来使我们上传的图片按照比例缩放哦, 好了费话不说多了我们来看看js 图片按比例缩放代码哦。

<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
 var image=new Image();
 var iwidth = 150;  //定义允许图片宽度
 var iheight = 150;  //定义允许图片高度
 image.src=ImgD.src;
 if(image.width>0 && image.height>0){
 flag=true;
 if(image.width/image.height>= iwidth/iheight){
  if(image.width>iwidth){  
  ImgD.width=iwidth;
  ImgD.height=(image.height*iwidth)/image.width;
  }else{
  ImgD.width=image.width;  
  ImgD.height=image.height;
  }
  ImgD.alt=image.width+"×"+image.height;
  }
 else{
  if(image.height>iheight){  
  ImgD.height=iheight;
  ImgD.width=(image.width*iheight)/image.height;  
  }else{
  ImgD.width=image.width;  
  ImgD.height=image.height;
  }
  ImgD.alt=image.width+"×"+image.height;
  }
 }

//-->
</script>

点击实例文本编辑功能,哦,我们来看看双击并可以编辑内容的js代码吧。

点击实例文本编辑功能,哦,我们来看看双击并可以编辑内容的js代码吧

<input class="a" id="test" readonly value=www.111cn.net? type="text" /><input type="submit" value="修改" style="display:none;" id="submit" />
<script type="text/javascript">
var test = document.getElementById("test");
var mysubmit = document.getElementById("submit");
test.ondblclick = function()
{
this.readOnly = false;
this.className = "b";
mysubmit.style.display = "";
}
mysubmit.onclick = function()
{
test.readOnly = true;
test.className = "a";
this.style.display = "none";
}
</script>

标签:[!--infotagslink--]

您可能感兴趣的文章: