Home Web Front-end JS Tutorial 7 JavaScript differences between FF and IE

7 JavaScript differences between FF and IE

May 16, 2016 pm 06:53 PM
javascript difference

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";
Copy after login


Firefox syntax:

The code is as follows:

document.getElementById("header").style.cssFloat = "left";
Copy after login


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;
Copy after login


Firefox syntax:

The code is as follows:

var myObject = document.getElementById("header"); 
var myComputedStyle = document.defaultView.getComputedStyle(myObject, null); 
var myStyle = myComputedStyle.backgroundColor;
Copy after login


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");
Copy after login


Firefox syntax:

The code is as follows:

var myObject = document.getElementById("header"); 
var myAttribute = myObject.getAttribute("class");
Copy after login


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");
Copy after login


Firefox syntax:

The code is as follows:

var myObject = document.getElementById("myLabel"); 
var myAttribute = myObject.getAttribute("for");
Copy after login


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;
Copy after login


Firefox syntax:

The code is as follows:

var myCursorPosition = [0, 0]; 
myCursorPosition[0] = event.pageX; 
myCursorPosition[1] = event.pageY;
Copy after login


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;
Copy after login


Firefox syntax:

The code is as follows:

var myBrowserSize = [0, 0]; 
myBrowserSize[0] = window.innerWidth; 
myBrowserSize[1] = window.innerHeight;
Copy after login


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); 
}
Copy after login


Firefox syntax:

The code is as follows:

#myElement { 
opacity: 0.5; 
}
Copy after login


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)";
Copy after login


Firefox syntax:

The code is as follows:

var myObject = document.getElementById("myElement"); 
myObject.style.opacity = "0.5";
Copy after login


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.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

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.

Comparison and difference analysis between SpringBoot and SpringMVC Comparison and difference analysis between SpringBoot and SpringMVC Dec 29, 2023 am 11:02 AM

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 implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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 implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

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.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

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-depth comparison: Analysis of the differences between Dimensity 8200 and Snapdragon In-depth comparison: Analysis of the differences between Dimensity 8200 and Snapdragon Mar 22, 2024 pm 12:48 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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 forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

See all articles