A very simple js to determine the browser's kernel

  • 2020-03-30 03:42:29
  • OfStack

You remember how to write inline styles in JavaScript, right? I guess I'm being ridiculous!

In the front-end development process, sometimes we need to determine the browser's kernel prefix and do different things to different browsers, so we can do this.

Alert (element. Style. WebkitTransition); This is to get the transition value prefixed with webkit. But if the browser is not prefixed with webkit, it returns undefined. Instead, we can enumerate all the kernel prefixes and get a value for one of their CSS. The code is as follows:


function getVendorPrefix() {
  //The body is used to avoid having to pass in an element
  var body = document.body || document.documentElement,
    style = body.style,
    vendor = ['webkit', 'khtml', 'moz', 'ms', 'o'],
    i = 0;

  while (i < vendor.length) {
    //This is where you determine if there is a corresponding kernel prefix
    if (typeof style[vendor[i] + 'Transition'] === 'string') {
      return vendor[i];
    }
    i++;
  }
}


Related articles: