12 methods to solve common browser compatibility problems using JavaScript

Source: Internet
Author: User
Tags jquery library

If you do not know the reason, do not worry too much. Please study CSS rules and view this article Article : Use CSS to fix everything: 20 + Common Errors and fixes.
If these are invalid, you can fix them using the 12 JavaScript solutions listed below, so that your web page looks like it can span all browsers!
In this article, we will unveil the Javascript solution for the 12 most common CSS problems that you may encounter when developing web applications.
1. Auto-Match Height

Since we abandoned the table-based page layout, creating visual effects for columns or content boxes of the same height is already a challenge.

1.1Use jquery to set the matching height

This jquery plug-in "balances" the height of the box in the same container and creates an introduction mesh-using a simple JavaScript alternative from the availability and performance perspective:Equalheights () functionMeasure the height of all elements of the same level in a container and set the lowest height of each element to the height of the highest element.

How to work

Equalheights () cyclically tests the child nodes at the highest level of a specified element, and then sets their minimum height to the height of the highest element.

Click here to preview the effect

1.2Match the column height with jquery

Another plug-in of jquery that can make the height of the box equal

$ ("# Col1, # col2"). equalizecols ();

Match the height as you think

$ ("# Col1, # col2"). equalizecols ("P, P ");

Match the two volumes and add a blank space after the P tag in # col1 or # col2 (short one.
2. Transparent support for IE6 PNG

Versions earlier than IE6 do not support PNG transparency. Hack, IE 5.5 and 6 are supported, but hack is not ideal and hard to use. Let's take a look at what we can do to support IE6 users and bring the best transparency to most visitors on the website.

2.1Force IE6 to support transparency

IE7 is a javascript library created by Dean Edwards to force MSIE (IE6, ie5) to behave like a compatible standard browser. It fixes many CSS problems and enables transparent PNG to work normally in IE6 and ie5, and also allows advanced CSS selectors.

Click to view preview results
Click to download the source file

2.2.Improved ifixpng

Fixed the issue of PNG images in IE6 and below. IMG labels and CSS background images can both be used. This plug-in is an improvement for the original ifixpng plug-in. Features include: images or labels with background images. Currently, background-position is supported, including the absolute positioning correction of IE browser. (Bottom:-1px | bottom: 0px)

Click to view preview results
Click here to download the source file

3. Use JavaScript to change the class

This is a convenient JavaScript function. You can change the class of any element in the current file from oldclass to newclass. This is especially useful to quickly use CSS instead of encoding to change the style.

Function changeclass (oldclass, newclass ){
VaR elements = Document. getelementsbytagname ("*");
For (I = 0; I <elements. length; I ++ ){
If (elements [I]. classname = oldclass) elements [I]. classname = newclass;

Click to view preview results
Click to download the source file

4. CSS browser Selector

If you only need to type a special selector, here you can write some JavaScript and set a class label based on the name of the current browser. What will happen?

4.1CSS Browser

This is a very small JavaScript with only one line and less than 1 kb. It allows CSS selectors. It allows you to write specific CSS for each operating system and each BrowserCode. You can write some JavaScript code and set the class name. That is to say, the content is based on the current browser.

Click to view preview results
Click to download the source file

Jquery browser Selector

Here is another very simple jquery-based method for processing browser selectors. All you need to do is load the jquery library file and add the following code.

$ (Document). Ready (function (){
$ ('Html '). addclass ($. browser );

Now you can prepare your style, such as. MSIE,. Mozilla,. Opera,. Safari, or other target browsers.

Click to view preview results

5. minimum/maximum height/width support

For CSS Min-width, Min-height, Max-width, Max-height, border-*-width, margin, and padding attributes, there are some good jquery corrections.


This is a jquery plug-in that supports Min-width, Max-width, Min-height, and Max-height without the original sound.

Click to view preview results
Click to download the source file

Jsizes 5.2

This small jquery plugin supports CSS Min-width, Min-height, Max-width, Max-height, border-*-width, margin, and padding attributes. In particular, he provides a way to determine where an element is visible. Since the methods of all models return numerical values, these can also be safely used in strict DOM elements.

Jquery (function ($ ){
VaR mydiv =$ ('# mydiv ');

// Set margin-top to 100px and margin-bottom to 10em
Mydiv. margin ({top: 100, bottom: '10em '});

// Displays the size of the top border in pixels
Alert (mydiv. Border (). Top );

// Displays true if the element is visible, false otherwise
Alert (mydiv. isvisible ());

// Set padding-Right to 10px and margin-left to 15px using chaining
Mydiv. Padding ({right: 10}). margin ({left: 15 });

Click to view preview results
Click to download the source file

6. Vertical/horizontal center of Elements

You may have encountered this problem before: to center an element horizontally or vertically. Vertical center is quite troublesome in CSS, especially if you want to support all mainstream browsers.

6.1Center element plugin

This plug-in can center all elements in the page, vertical and horizontal center using CSS negative margin method.

$ ("Element"). Center (); // vertical and horizontal
$ ("Element"). Center ({
Horizontal: false // only vertical

Click to view preview results
Click to download the source file

6.2How do I vertically center an element?

In this video tutorial, Jeffrey Jordan way will show you how to align an image vertically in your browser with the power of jquery.

7. Use the Q tag in IE

Q tags are expected to be used instead of BLOCKQUOTE tags to display quotation marks. However, ie/win does not support Q tags, because most website authors choose not to use Q tags.


When you add this script in the header of your file to automatically scan web page Q tags in IE browser, and correctly display them (including nested references ). If the IE browser supports the Q tag in the future, this plug-in will add the browser version check.

Click to download the source file

8. increase the size of the click target and obtain more response transformations.

Put all your content in a clickable tag to use the monotonous "Read more ..." Let's talk about the link.

Click to download the source file

9. Lazy Loader

Lazy loader is a jquery. It delays the loading of images on the page. It will not be loaded until the user browses an image outside the horizon (visible in the page. This is the opposite of image preloading.

Click to view preview results
Click here to download the source file

10. bgiframe

It is easy to solve the Z-index problem in IE.

Click to view preview results
Click here to download the source file

11. iefixbuttons

Iefixbuttons is a jquery plug-in that fixes the <button> label bug of IE6 and 7.

Click to view preview results
Click here to download the source file

12. Overflow correction

Corrected horizontal overflow in IE. IE displays a scroll bar in the overflow element, especially if there is only one row in the element, the scroll bar will cover this line of content. This plugin fixes this issue by modifying padding.

Click to view preview results
Click here to download the source file

Original English:Using JavaScript to fix 12 Common Browser headaches

Original Chinese Translation:Corrected 12 Common Browser problems with JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.