7 JavaScript differences between FF and IE
Although JavaScript’s historical days of using long and annoying blocks of code to target specific browsers are over, the occasional use of some simple code blocks and object detection to ensure that some code works properly on the user’s machine is still Necessary.
In this article, I will briefly outline 7 aspects of JavaScript syntax between Internet Explorer and Firefox.
1. CSS “float” property
The basic syntax for obtaining a specific CSS property of a given object is the object.style property, and hyphenated properties use camel nomenclature to replace. For example, to get the background-color attribute of p with the ID "header", we need to use the following syntax:
document.getElementById("header").style.borderBottom= "1px solid #ccc";
But Since "float" is a reserved word in JavaScript, we cannot use object.style.float to obtain the "float" attribute. The following is the method we use in the two browsers:
IE syntax:
The code is as follows:
document.getElementById("header").style.styleFloat = "left";
Firefox syntax:
The code is as follows:
document.getElementById("header").style.cssFloat = "left";
2. Calculated style of element
By using the above object.style.property, JavaScript can easily obtain and modify the object's set CSS style. But the limitation of this syntax is that it can only get styles inline in HTML, or styles set directly using JavaScript. The style object cannot obtain styles set using external style sheets. To get the "calculated style" of an object, we use the following code:
IE syntax:
The code is as follows:
var myObject = document.getElementById("header"); var myStyle = myObject.currentStyle.backgroundColor;
Firefox syntax:
The code is as follows:
var myObject = document.getElementById("header"); var myComputedStyle = document.defaultView.getComputedStyle(myObject, null); var myStyle = myComputedStyle.backgroundColor;
3. Get the "class" attribute of the element
Similar to the case of the "float" attribute, the two browsers use different JavaScript methods to obtain this attribute.
IE syntax:
The code is as follows:
var myObject = document.getElementById("header"); var myAttribute = myObject.getAttribute("className");
Firefox syntax:
The code is as follows:
var myObject = document.getElementById("header"); var myAttribute = myObject.getAttribute("class");
4. Get the "for" attribute of the label
Same as 3, use JavaScript to get the label The "for" attribute also has different syntax.
IE syntax:
The code is as follows:
var myObject = document.getElementById("myLabel"); var myAttribute = myObject.getAttribute("htmlFor");
Firefox syntax:
The code is as follows:
var myObject = document.getElementById("myLabel"); var myAttribute = myObject.getAttribute("for");
The same syntax is also used for the setAtrribute method.
5. Get the cursor position
Getting the cursor position of an element is rare. If you need to do this, the syntax of IE and Firefox is also different. This example code is fairly basic and is typically used as part of many complex event handlers, and is only used here to describe the differences. Note that the results in IE are different than in Firefox, so there are some issues with this approach. Usually, this difference can be compensated by getting the "scroll position" - but that's a topic for another article.
IE syntax:
The code is as follows:
var myCursorPosition = [0, 0]; myCursorPosition[0] = event.clientX; myCursorPosition[1] = event.clientY;
Firefox syntax:
The code is as follows:
var myCursorPosition = [0, 0]; myCursorPosition[0] = event.pageX; myCursorPosition[1] = event.pageY;
6. Get the size of the window or browser window
Sometimes it is necessary to find out the size of the effective window space of the browser, which usually becomes "Windows".
IE syntax:
The code is as follows:
var myBrowserSize = [0, 0]; myBrowserSize[0] = document.documentElement.clientWidth; myBrowserSize[1] = document.documentElement.clientHeight;
Firefox syntax:
The code is as follows:
var myBrowserSize = [0, 0]; myBrowserSize[0] = window.innerWidth; myBrowserSize[1] = window.innerHeight;
7. Alpha transparency
Well, this is not actually a JavaScript syntax item - alpha transparency is through CSS to set. But when an object is set to fade via JavaScript, this needs to be accomplished by getting the CSS alpha setting, typically inside a loop. To change the CSS code through the following javaScript:
IE syntax:
The code is as follows:
#myElement { filter: alpha(opacity=50); }
Firefox syntax:
The code is as follows:
#myElement { opacity: 0.5; }
To use JavaScript to obtain these values, you need to use the style object:
IE syntax:
The code is as follows:
var myObject = document.getElementById("myElement"); myObject.style.filter = "alpha(opacity=80)";
Firefox syntax:
The code is as follows:
var myObject = document.getElementById("myElement"); myObject.style.opacity = "0.5";
Of course, as already mentioned, the opcity/alpha is usually changed in the middle of the loop to create animation effects, but this is a simple example just to clearly describe how the method is implemented.

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

AI Hentai Generator
Generate AI Hentai for free.

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

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

In the era of mobile Internet, the performance of mobile phones has always been one of the focuses of users. As the leaders in the mobile phone chip market, MediaTek and Qualcomm have also attracted the attention of consumers for their chips. Recently, MediaTek launched the Dimensity 8200 chip, while Qualcomm has its representative Snapdragon series chips. So, what are the differences between these two chips? This article will conduct an in-depth comparative analysis between Dimensity 8200 and Snapdragon. First of all, from the perspective of process technology, Dimensity 8200 uses the latest 6nm process technology, while some of Qualcomm Snapdragon’s

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

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We
