Summary of some compatibility and error-prone issues in js
We have also encountered a lot of js compatibility issues. This article will share with you an article summarizing some compatibility and error-prone issues in js. It has a good reference value. I hope it will be helpful to everyone. I hope it can help. to everyone.
1. Attribute related
We usually refer to attributes and properties as attributes, but they are indeed different concepts.
Characteristics It will appear in the HTML text, and the modification of the characteristics will definitely appear in the outerHTML of the element, and the characteristics only exist in the element node;
Attributes (property) modify the JS object, except for the built-in browser Except for some features, other attribute operations will not affect HTML text.
1. IE6/7 does not distinguish between attributes and characteristics
Other browsers will distinguish between attributes and characteristics, but IE67 does not distinguish between them. Under IE67 we can only use the attribute name to delete Characteristics, although these two names are often the same, there are always differences.
2. IE6/7 cannot use getAttribute/setAttribute to operate features whose values are not strings
In modern browsers, getAttribute will definitely return the corresponding string in HTML, while IE67 returns The results are unpredictable, so under IE67, we have to use AttributeNode to operate attributes.
3. IE6/7/8 cannot obtain CSS text through the style attribute
This problem should be the sequelae of IE6/7 not distinguishing between attributes and characteristics. When obtaining the style attribute, Just use elem.style.cssText.
4. IE6/7 will parse relative URLs into absolute URLs
This problem even causes the empty src attribute under IE6/7 to generate repeated requests. You can use getAttribute('href/ src', 4).
5. The default behavior of element characteristics is different
There will be many bugs of this type. For example, in some old webkit browsers, the default value of checkbox/radio is "", while Not on. In some older webkit browsers the first element of the select will not be selected.
2. Style operations
Normally, styles include getting and setting inline styles and getting the calculated styles of elements
1. IE supports background-position-x /y and other browsers do not support
background-position-x/y can be used to conveniently gradient the position of an image. If it is not supported, we can consider using the parsing background-position attribute to handle it.
2. IE6/7 does not support the opacity attribute
You can achieve the same effect through the alpha filter, but remember to trigger the haslayout of the element.
3. IE6/7/8 will mistakenly let the nodes generated by clone inherit some attributes
For example, background, if you modify one or two, they will be changed at the same time.
4. Different ways to get the calculated style
IE6/7/8 uses elem.currentStyle while other browsers use the window.getComputedStyle function.
5. Different pixelation methods
Pixelization refers to converting distances whose units are not pixels into pixels to facilitate calculations. Strictly speaking this is not a compatibility issue, but it may be used across the board. In IE6/7/8, we can use elem.runtimeStyle with pixelLeft for processing.
Modern browsers can use the width attribute.
6. Some BUG behaviors when obtaining CSS
In the Webkti core browser, margin-right often goes wrong.
3. Query operation
Query passing refers to finding a set of elements through some characteristic strings, or judging whether the elements satisfy the string.
1. IE6/7 does not distinguish between id and name
When using getElementById and getElementsByName under IE6/7, elements with the same id or name as the given value will be returned at the same time. Since the name is usually agreed upon by the backend, when we write JS, we should ensure that the id does not duplicate the name.
2. IE6/7 does not support getElementsByClassName and querySelectorAll
These two functions have been supported since IE8, so under IE6/7, we can actually only use getElementByTagName.
3. IE6/7 does not support getElementsByTagName('*') and will return non-element nodes
Either do not use *, or write a function to filter it yourself.
4. querySelectorAll under IE8 is not friendly to attribute selectors
Almost all browser predefined attributes have problems, try to use custom attributes or not use attribute selectors.
5. querySelectorAll does not support pseudo-classes under IE8
Sometimes pseudo-classes are very useful, but IE8 does not support them. jquery provides: first, :last, :even, :odd, :eq, :nth, :lt, :gt are not pseudo-classes, and we should not use them at any time.
6. The matches function of IE9 cannot handle elements that are not on the DOM tree
As long as the element is not on the DOM tree, it will definitely return false. It is really not feasible to throw the element in the body and then delete it after matching. Drop it, of course we can also write our own matching function to avoid reflow.
4. Event operation
Usually when everyone asks about the compatibility of JS, the first reaction will be attachEvent and addEventListener, but there are still many details about the difference between these two functions.
1. Event action objects are different
addEventListener is the current object to which the event bubbles up, while attachEvent is window.
. Event parameter objects are different
Be sure to note that the function bound to attachEvent has parameter e. Do not write e = e || event anymore, but the parameter attributes of both There are many differences, such as button or witch, whether pageY is supported or not, etc.
2. The evil scroll incident
The support for wheel events is a mess, and the rules are as follows:
IE6-11 chrome mousewheel wheelDetla next-120 upper 120
firefox DOMMouseScroll detail next 3 upper-3
firefox wheel detlaY Next 3 Upper-3
IE9-11 wheel deltaY Lower 40 Upper-40
chrome wheel deltaY Lower 100 Upper-100
3. Three major non-bubbling events
The focus/blur events of all browsers do not bubble. Fortunately, most browsers support focusin/focusout events, but the damn firefox does not even support this.
The submit event does not bubble up under IE678.
Under IE678, the change event will not be triggered until blur.
We have no effective means to solve these three problems and can only deal with them through simulation triggering.
4. a.click()
This method is not safe in many browsers. Once executed, even if the default behavior is blocked, the page will still jump.
5. Node operation
Node operation usually refers to copying, generating a node or moving the location of a node.
1.. innerHTML
When IE6/7/8 uses innerHTML, there must be a text node in front, otherwise many tags will be lost. In addition, in many cases, using innerHTML will cause errors in the defaultValue attribute.
2. Useless tbody
IE6/7/8 will automatically add an empty tbody to an empty table
3. The attachEvent event will be copied when cloneNode
And unless we record the events we bound, we can't unbind them.
Related recommendations:
Common browser compatibility issues in html and css
Detailed analysis of compatibility performance in JavaScript
Detailed explanation of JavaScript string operation methods and browser compatibility examples
The above is the detailed content of Summary of some compatibility and error-prone issues in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Nowadays, many mobile phones claim to support Bluetooth 5.3 version, so what is the difference between Bluetooth 5.3 and 5.2? In fact, they are essentially subsequent updated versions of Bluetooth 5, and there is not much difference in most performance and functions. The difference between Bluetooth 5.3 and 5.2: 1. Data rate 1 and 5.3 can support higher data rates up to 2Mbps. 2. While 5.2 can only reach a maximum of 1Mbps, it means that 5.3 can transmit data faster and more stably. 2. Encryption control enhancement 2. Bluetooth 5.3 improves encryption key length control options, improves security, and can better connect to access control and other devices. 3. At the same time, because the administrator control is simpler, the connection can be more convenient and faster, which is not possible in 5.2.

The performance of i77700 is completely sufficient to run win11, but users find that their i77700 cannot be upgraded to win11. This is mainly due to restrictions imposed by Microsoft, so they can install it as long as they skip this restriction. i77700 cannot be upgraded to win11: 1. Because Microsoft limits the CPU version. 2. Only the eighth generation and above versions of Intel can directly upgrade to win11. 3. As the 7th generation, i77700 cannot meet the upgrade needs of win11. 4. However, i77700 is completely capable of using win11 smoothly in terms of performance. 5. So you can use the win11 direct installation system of this site. 6. After the download is complete, right-click the file and "load" it. 7. Double-click to run the "One-click

The Go language has very good compatibility on Linux systems. It can run seamlessly on various Linux distributions and supports processors of different architectures. This article will introduce the compatibility of Go language on Linux systems and demonstrate its powerful applicability through specific code examples. 1. Install the Go language environment. Installing the Go language environment on a Linux system is very simple. You only need to download the corresponding Go binary package and set the relevant environment variables. Following are the steps to install Go language on Ubuntu system:

With the continuous development of modern technology, wireless Bluetooth headsets have become an indispensable part of people's daily lives. The emergence of wireless headphones frees our hands, allowing us to enjoy music, calls and other entertainment activities more freely. However, when we fly, we are often asked to put our phones in airplane mode. So the question is, can I use Bluetooth headphones in airplane mode? In this article, we will explore this question. First, let’s understand what airplane mode does and means. Airplane mode is a special mode for mobile phones

The software in the win10 system has been perfectly optimized, but for the latest win11 users, everyone must be curious about whether this system can be supported, so the following is a detailed introduction to the win11 software that does not support win10. Come and find out together. Does win11 support win10 software: 1. Win10 system software and even Win7 system applications are well compatible. 2. According to feedback from experts who use the Win11 system, there are currently no application incompatibility issues. 3. So you can upgrade boldly with confidence, but ordinary users are advised to wait until the official version of Win11 is released before upgrading. 4. Win11 not only has good compatibility, but also has Windo

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

1. Right-click the program and find that the [Compatibility] tab is not found in the properties window that opens. 2. On the Win10 desktop, right-click the Start button in the lower left corner of the desktop and select the [Run] menu item in the pop-up menu. 3. The Win10 run window will open, enter gpedit.msc in the window, and then click the OK button. 4. The Local Group Policy Editor window will open. In the window, click the [Computer Configuration/Administrative Templates/Windows Components] menu item. 5. In the opened Windows component menu, find the [Application Compatibility] menu item, and then find the [Remove Program Compatibility Property Page] setting item in the right window. 6. Right-click the setting item, and in the pop-up menu

Best practices to solve PHP function compatibility issues: Use versioned function names (for example: array_map_recursive()) Leverage function aliases (for example: functionarray_map($callback,$array){...}) to check function availability (for example: if (function_exists('array_map_recursive')){...}) use namespace (for example: namespaceMyNamespace{...})
