关于document.documentElement和document.body 的一些疑问

今天在工作中,调试网页代码时, 偶然遇到下面一段代码 ,这是电脑管家的网页广告过滤的js代码
/*
电脑管家chrome 广告过滤
*/


var GJAD_CS =
{
    elemhideElt : null,
    setElemhideCSSRules: function (selectors)
    {
      if (GJAD_CS.elemhideElt && GJAD_CS.elemhideElt.parentNode)
        GJAD_CS.elemhideElt.parentNode.removeChild(GJAD_CS.elemhideElt);


      if (!selectors)
        return;


      GJAD_CS.elemhideElt = document.createElement("style");
      GJAD_CS.elemhideElt.setAttribute("type", "text/css");


      // Try to insert the style into the <head> tag, inserting directly under the
      // document root breaks dev tools functionality:
      // http://code.google.com/p/chromium/issues/detail?id=178109
      (document.head || document.documentElement).appendChild(GJAD_CS.elemhideElt);


      var elt = GJAD_CS.elemhideElt;  // Use a local variable to avoid racing conditions
      function setRules()
      {
        if (!elt.sheet)
        {
          // Stylesheet didn't initialize yet, wait a little longer
          window.setTimeout(setRules, 0);
          return;
        }


        elt.innerText = selectors;
      }
      setRules();
    },
    
    QueryCss: function()
    {
        var query_param={
        "url":  window.location.href
        };


        chrome.extension.sendRequest({cmd: "query_css", json: JSON.stringify(query_param)}, function(response)
        {
          if (response && response.selectors != "")
          {
            GJAD_CS.setElemhideCSSRules(response.selectors);
          }
        });
    },
};

GJAD_CS.QueryCss();


其中document.head || document.documentElement这一块不是很理解  网上查了下 

如果网页是<html>开头  使用 document.head,
类似于<!DOCTYPE><html></html> ,使用 document.documentElement

在 IE 和 Firefox 中均是如此。

为了兼容,不管有没有 DTD,可以使用上面的代码:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值