简体   繁体   中英

what's the line meaning in javascript?

  1. if (document.all)
  2. document.body.style.behavior='url(#default#homepage)';
  3. if (window.sidebar)

What's those lines meaning in javascript? thank you.

document.all is used to check if the browser is IE

if (document.all) : used to check if the brower is IE, but note this is bad practice because it is no longer a good method of doing the test.

if (window.sidebar) : test if the browser is Firefox.

EDIT: document.body.style.behavior='url(#default#homepage)'; is most likely used to set the homepage when the browser is IE . However, it does not seem to work well with Firefox and the others .

Don't use document.all:

if (document.all) {
   element = document.all[id];
else {
   element = document.getElementById(id);
}
  1. document.all was introduced in Internet Explorer 4, because the W3C DOM hadn't yet standardised a way of grabbing references to elements using their ID.
    By the time IE 5 came out, document.getElementById() had been standardised and as a result, IE 5 included support for it. More info here. .

  2. document.body.style.behavior='url(#default#homepage)' is used to set current page as home page in the IE.

  3. if (window.sidebar) is a check for firefox

Statement 1 tries to detect if the browser is IE and statement 2 uses an IE-only API: behavior property .

However, document.all is not IE-only feature. It also exists on Chromium/Chrome and other WebKit based browsers.

Therefore, statement 1 get passed on IE & Chrome, but statement 2 only works on IE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM