search
HomeWeb Front-endJS TutorialImprovements in JavaScript DOM operations

Improvements in JavaScript DOM operations

Nov 25, 2016 am 11:15 AM
JavaScript

As a web front-end, dealing with and understanding browser differences is an important issue. Below I will introduce a summary of some of my notes at work. First, I will introduce the situation without using a js library.

 1. The setAttribute method sets the element class name: In jQuery, you can directly use the attr() method, which can be used in native JS

element.setAttribute(class, newClassName) //This is the W3C standard and is compatible with It is valid in W3C standard browsers. However, IE is the main theme for domestic users.
element.setAttribute(className, newClassName) //Such a setting is only valid in IE.
element.className = newClassName //All browsers are valid (as long as Support javascript)
Okay, that’s the beginning. The purpose of this article is to introduce the differences between browsers and let front-end friends understand how to use the most effective method to solve the problem. Let’s continue.

 2. FireFox does not have a window.event object, only an event object. IE only supports window.event, while other mainstream browsers support both, so it is generally written as:

function handle(e)
{
e = e || event;
...
}
 3. Principle of DOMContentLoaded event: The window.onload event is triggered after the page parsing/DOM tree establishment is completed and the downloading of all resources such as pictures, scripts, style sheets, etc. of. This is a bit too "late" for many practical applications, which affects the user experience. In order to solve this problem, a DOMContentLoaded method was added to FF. Compared with onload, this method is triggered earlier. It is triggered after the DOM content of the page is loaded without waiting for other resources to be loaded (this is also This is the implementation principle of jquery.ready() event).

//The following is the original analysis of jQuery 1.4.2 version

bindReady: function() {
 if ( readyBound ) {
  return;
 }
  readyBound = true;
  // Catch cases where $(document).ready () is called after the
   // browser event has already occurred.
  if ( document.readyState === "complete" ) {
   return jQuery.ready();
  }
  // Mozilla, Opera and webkit nightlies currently support this event
 if ( document.addEventListener ) {
  // Use the handy event callback
  document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
   // A fallback to window.onload, that will always work
  window.addEventListener ( "load", jQuery.ready, false );
   // If IE event model is used
  } else if ( document.attachEvent ) {
   // ensure firing before onload,
  // maybe late but safe also for iframes
document.attachEvent("onreadystatechange", DOMContentLoaded);
    // A fallback to window.onload, that will always work
   window.attachEvent( "onload", jQuery.ready );
   // If IE and not a frame
   / /continuously check to see if the document is ready
  var toplevel = false;
  try {
  toplevel = window.frameElement == null;
  } catch(e) {}
  if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
  }
  }
}
  Design idea - Treat Webkit and Firefox equally, and both directly register the DOMContentLoaded event. However, since Webkit was only introduced in version 525 or above, there are hidden dangers of compatibility. For IE, first register the onreadystatechange event of the document. After testing, this method is equivalent to window.onload. It will still wait until all resources are downloaded before triggering. After that, if it is determined that it is IE and the page is not in an iframe, the doScroll method of documentElement is continuously called through setTiemout until the call is successful and DOMContentLoaded is triggered. 1 The principle of jQuery's solution to IE is that under IE, some methods of DOM can only be called after the DOM parsing is completed. doScroll is such a method. In turn, when doScroll can be called, the DOM parsing is completed. Compared with document.write in prototype, this solution can solve the problem of failure when the page has an iframe. In addition, jQuery seems to be worried that this method will fail when the page is in an iframe, so it makes a judgment in the implementation code. If it is in an iframe, it is implemented through the onreadystatechange of the document, otherwise it is implemented through doScroll. However, after testing, doScroll still works even in an iframe.

 4. Learn to use IE’s conditional comments. Many front-ends are always complaining about the evil IE. Indeed, dealing with compatibility issues will indeed become more and more disgusting, but there is no way. Since there is no way to change, then please enjoy...






Version 6




  Let’s summarize it here for today. I will have time to post an advanced AJAX article when I get back from vacation next week. I hope it can be helpful to novices or people in need. Due to the limited writing style, please forgive me if the writing is not good. I have just started blogging and it is in the growth stage. Haha...


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),