Home Web Front-end JS Tutorial Web front-end solutions to browser compatibility issues

Web front-end solutions to browser compatibility issues

Mar 14, 2018 pm 01:36 PM
web compatibility

This time I will bring you the web front-end solution to the browser compatibility problem. What are the precautions for the web front-end to solve the browser compatibility problem. The following is a practical case. Let’s take a look. .

The so-called browser compatibility problem refers to the situation where different browsers have different parsing of the same piece of code, resulting in inconsistent page display effects. In most cases, our requirement is that no matter what browser the user uses to view our website or log in to our system, there should be a unified display effect. Therefore, browser compatibility issues are issues that front-end developers often encounter and must solve:

1. css3Media queryCompatibility solution: Respond.js

GitHub address: https://github.com/scottjehl/Respond
(from the Internet)
IE8 does not support CSS media queries, which is greatly detrimental to responsive design. Respond.js can help IE6-8 be compatible with "min/max-width" media query conditions.

Usage: Reference Respond.js after the reference location of all css files in the page. And the earlier Respond.js is referenced, the less chance the user will see the page flickering.

2. Custom attribute problem

Problem description: Under IE, you can use the method of obtaining regular attributes to obtain custom attributes, or you can use getAttribute() to obtain custom attributes; under Firefox , custom attributes can only be obtained using getAttribute().
Solution: Uniformly obtain custom attributes through getAttribute().

3. The problem that the variable name is the same as the ID in HTML

Problem description: Under IE, the ID of the HTML object can be used directly as the variable name of the subordinate object of the document, but not under Firefox; Firefox Under IE, you can use the same variable name as the HTML object ID, but not under IE.
Solution: Use document.getElementById("idName") instead of document.idName. It is best not to use variable names with the same HTML object ID to reduce errors; when declaring variables, always add the var keyword to avoid ambiguity.

4. Const problem

Problem description: Under Firefox, you can use the const keyword or the var keyword to define constants; under IE, you can only use the var keyword to define constants.
Solution: Use the var keyword uniformly to define constants. Regarding const, which is a method of defining variables after let in ES6, one thing to note is that
must be assigned a value when declaring a variable, otherwise an error will be reported.

5. Window.event problem

Problem description: window.event can only be run under IE, but not under Firefox. This is because Firefox's event can only be run when the event occurs. For on-site use.
Solution: Add the event parameter to the function where the event occurs, and use var myEvent = evt?evt:(window.event?window.event:null) in the function body (assuming the formal parameter is evt)
Example :

<input type="button" onclick="doSomething(event)"/> 
<script language="javascript"> 
function doSomething(evt) { 
    var myEvent = evt?evt:(window.event?window.event:null) 
    ...} 123456
Copy after login

6. Problems with event.x and event.y

7. Get the position of the mouse on the page
Under IE, the event object has x, y attributes, but not pageX, pageY attributes;
Under Firefox, the event object has pageX, pageY attributes, but no x, y attributes.

Solution:

Use mX (mX = event.x? event.x : event.pageX;) to replace event.x under IE or event.pageX under Firefox.

7. Regarding the frame problem

Take the following frame as an example:
1. Access the frame object
IE: Use window.frameId or window.frameName to access the frame object;
Firefox: Use window.frameName to access the frame object;
Solution: Use window uniformly .document.getElementById("frameId") to access this frame object;
2. Switch frame content
You can use window.document.getElementById("frameId").src = "webjx. com.html" or window.frameName.location = "webjx.com.html" to switch the content of the frame;
If you need to pass the parameters in the frame back to the parent window, you can use the parent keyword in the frame To access the parent window.

8. Body loading problem

Problem description: Firefox's body object exists before the body tag is fully read by the browser; while IE's body object must be before the body tag is read. It does not exist until the browser has completely read it.
[Note] This issue has not been actually verified and will be modified after verification.
[Note] It has been verified that the above problem does not exist in IE6, Opera9 and FireFox2. A simple JS script can access all objects and elements that have been loaded before the script, even if the element has not been loaded yet.

9. Event delegation method

Event对象提供了一个属性叫target,可以返回事件的目标节点,我们成为事件源,也就是说,target就可以表示为当前的事件操作的dom,但是不是真正操作dom,当然,这个是有兼容性的,标准浏览器用ev.target,IE浏览器用event.srcElement,此时只是获取了当前节点的位置,并不知道是什么节点名称,这里我们用nodeName来获取具体是什么标签名,这个返回的是一个大写的,我们需要转成小写再做比较(习惯问题):

window.onload = function(){
     var oUl = document.getElementById("ul1");
     oUl.onclick = function(ev){
        var ev = ev || window.event;
            var target = ev.target || ev.srcElement;
           if(target.nodeName.toLowerCase() == &#39;li&#39;){
                alert(123);
         alert(target.innerHTML);
        }
     }
}1234567891011
Copy after login

十、访问的父元素的区别

问题说明:在IE下,使用 obj.parentElement 或 obj.parentNode 访问obj的父结点;在firefox下,使用 obj.parentNode 访问obj的父结点。
解决方法:因为firefox与IE都支持DOM,因此统一使用obj.parentNode 来访问obj的父结点。

十一、innerText的问题.

问题说明:innerText在IE中能正常工作,但是innerText在FireFox中却不行。
解决方法:在非IE浏览器中使用textContent代替innerText。

if(navigator.appName.indexOf("Explorer") >-1){ 
    document.getElementById(&#39;element&#39;).innerText = "my text"; 
} 
else{ 
    document.getElementById(&#39;element&#39;).textContent = ";my text"; 
}123456
Copy after login

十二、用setAttribute设置事件

var obj = document.getElementById(&#39;objId&#39;); 
obj.setAttribute(&#39;onclick&#39;,&#39;funcitonname();&#39;);12
Copy after login

FIREFOX支持,IE不支持
解决办法:
IE中必须用点记法来引用所需的事件处理程序,并且要用赋予匿名函数

var obj = document.getElementById(&#39;objId&#39;); 
obj.onclick=function(){fucntionname();};12
Copy after login

十三、设置类名

setAttribute(‘class’,’styleClass’)
FIREFOX支持,IE不支持(指定属性名为class,IE不会设置元素的class属性,相反只使用setAttribute时IE自动识CLASSNAME属性)
解决办法如下:

setAttribute(&#39;class&#39;,&#39;styleClass&#39;) 
setAttribute(&#39;className&#39;,&#39;styleClass&#39;) 
//或者直接
 object.className=&#39;styleClass&#39;;123456
Copy after login

十四、绑定事件

在IE下我们通常使用attachEvent方法

var btn1Obj = document.getElementById("btn1"); //object.attachEvent(event,function); btn1Obj.attachEvent("onclick",method1); btn1Obj.attachEvent("onclick",method2); btn1Obj.attachEvent("onclick",method3); 12345
Copy after login

可惜这个微软的私人方法,火狐和其他浏览器都不支持,幸运的是他们都支持W3C标准的addEventListener方法

var btn1Obj = document.getElementById("btn1"); 
//element.addEventListener(type,listener,useCapture); btn1Obj.addEventListener("click",method1,false); 
btn1Obj.addEventListener("click",method2,false); 
btn1Obj.addEventListener("click",method3,false); 12345
Copy after login

顺变说一下这两个的使用方式:

addEventListener的使用方式

target.addEventListener(type,listener,useCapture);
Copy after login

target: 文档节点、document、window 或 XMLHttpRequest。
type: 字符串,不含“on”如“click”、“mouseover”、“keydown”等。
listener :实现了 EventListener 接口或者是 JavaScript 中的函数。
useCapture :是否使用捕捉,一般用 false 。
例如:

document.getElementById("testText").addEventListener("keydown", function (event) { alert(event.keyCode); }, false); 1
Copy after login

2.对于attachEvent

target.attachEvent(type, listener);
Copy after login

target: 文档节点、document、window 或 XMLHttpRequest。
type: 字符串,事件名称,含“on”,比如“onclick”、“onmouseover”、“onkeydown”等。
listener :实现了 EventListener 接口或者是 JavaScript 中的函数。

例如:

document.getElementById("txt").attachEvent("onclick",function(event){alert(event.keyCode);}); 1
Copy after login

但是他们都给出了事件的移除方法

removeEventListener(event,function,capture/bubble);
Copy after login

十五、ajax请求

对于ajax请求只要出现兼容性的方面就是创建对象时候的区别我们要考虑IE6的情况,下面给出代码

   //设置IE6的情况,注意,在判断XMLHttpRequest是否存在时将其
    //设置为window.XMLHttpRequest,这样将其设置为属性,在检测时就不是未定义
    //而是undefine
    //1.创建Ajax对象
    if(window.XMLHttpRequest){        var oAjax=new XMLHttpRequest();
    }    else{        var oAjax=new ActiveXObject("Microsoft.XMLHTTP");
    }
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

前端页面测试的方法

javascript中call与apply的应用

The above is the detailed content of Web front-end solutions to browser compatibility issues. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Comparison and difference analysis of Bluetooth 5.3 and 5.2 versions Comparison and difference analysis of Bluetooth 5.3 and 5.2 versions Dec 28, 2023 pm 06:08 PM

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.

Solution to i7-7700 unable to upgrade to Windows 11 Solution to i7-7700 unable to upgrade to Windows 11 Dec 26, 2023 pm 06:52 PM

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

How compatible is the Go language on Linux systems? How compatible is the Go language on Linux systems? Mar 22, 2024 am 10:36 AM

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:

Can I use Bluetooth headphones in airplane mode? Can I use Bluetooth headphones in airplane mode? Feb 19, 2024 pm 10:56 PM

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

Detailed explanation of win11 compatibility issues with win10 software Detailed explanation of win11 compatibility issues with win10 software Jan 05, 2024 am 11:18 AM

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

WIN10 compatibility lost, steps to recover it WIN10 compatibility lost, steps to recover it Mar 27, 2024 am 11:36 AM

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

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Best practices for resolving PHP function compatibility issues Best practices for resolving PHP function compatibility issues May 01, 2024 pm 02:42 PM

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{...})

See all articles