Load individual CSS for specific web browsers

I am in need of assistance. My goal is to load a specific CSS file for each browser (Chrome, Safari, Mozilla, IE). I came across a solution that includes the Safari option, but unfortunately it is not working as expected.

<script type="text/javascript">
 var browser = navigator.userAgent.toLowerCase().indexOf('chrome') > -1 ? 'chrome' : 'other';
    if (BrowserDetect.browser.indexOf("chrome")>-1) {
document.write('<'+'link rel="stylesheet" href="/css/chrome.css" />');
} else if (BrowserDetect.browser.indexOf("mozilla")>-1) {
    document.write('<'+'link rel="stylesheet" href="css/mozilla.css" />');
} else if (BrowserDetect.browser.indexOf("explorer")>-1) {
    document.write('<'+'link rel="stylesheet" href="css/explorer.css" />');
} else if (BrowserDetect.browser.indexOf("safari")>-1) {
    document.write('<'+'link rel="stylesheet" href="css/safari.css" />');
}
</script> 

There is an HTML5 audio player on the page, and its color changes based on the browser. As a result, the CSS styles must be unique for each browser to maintain consistency with the player's appearance.

Answer №1

Check out Modernizr and Conditionizr for your JavaScript needs.

Why?

  1. They are reliable JavaScript libraries
  2. Avoid reinventing the wheel with the DRY principle
  3. They stay updated and adapt to browser changes, relieving you from tracking all browser evolutions

I understand this might not be a direct answer and could face criticism, but it is the most practical solution. What works for Safari today may not work tomorrow.

To see an example specific to your case in Conditionizr, check out:

https://github.com/conditionizr/conditionizr/blob/master/docs/DOCS.md#basic-config-setup

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

transition-duration is ineffective in a particular div

Whenever I hover over the purple text, I want it to increase in size using the zoom property. Here is an example: I am facing two issues: The time duration does not seem to work even when I try to apply it within the hover and non-hover classes. Wh ...

The responsiveness of Highcharts does not work properly when making an AJAX call

In my node.js application, I am utilizing the Highcharts API for visualizing charts. The divs containing the charts are initially hidden on page load. When the onclick function is triggered, the charts appear in the respective divs. Within the onclick func ...

Obtaining an OBJECT from a hashmap instead of a string

In my code, I am working with a hashmap and I want to extract all the values from this map without needing to reference a specific key. Here is a basic overview of the process: Create a hashmap using an external file with a structure of "id:value" Utili ...

"Enhancing the user experience: Triggering a window resize event in jQuery before page load on Magento

I am trying to trigger this function before the page finishes loading, but currently it only triggers after the page has loaded. Can anyone assist with this issue? $(window).on('load resize', function(){ var win = $(this); //this = window ...

The utilization of HTML in Laravel for generating PDF documents

I'm currently in the process of developing a website using Laravel, where I need to include a functionality that allows users to download a PDF containing details related to the specific page they are viewing (such as a training course). For this pur ...

Activating jquery functionality

Here is the code snippet I am working with: <script type="text/javascript> $(document).ready(function(){ $("Edit").click(function(){ $.get('fetchvalues.php'); }); }); </script> <input id="Edit" name="E ...

What is the process for incorporating an XML page with vike (ssr) into my website?

When it comes to my website's SEO, I use vike for ssr and would like to incorporate a sitemap in XML format. The issue arises as vike builds my pages in html format. Is there a way to integrate an XML file for SEO purposes? I attempted to access the ...

Is there a way to relocate the collapse button to the bottom after it has been clicked?

I have been using Bootstrap to design a expandable card that can be expanded when clicked. I am currently using the collapse feature, but I am facing an issue where the button remains in its original position, whereas I want it to move to the bottom of the ...

An AJAX event handling function returns a null value upon invocation

Recently, I've been working on a function named 'getAuthor' which includes an AJAX event. Here's the code snippet: function getAuthor(id){ $.get('http://www.connectnigeria.com/articles/wp-json/wp/v2/users/74',function(e){ ...

How can JSON and jQuery be used for sorting?

Using ajax to create HTML, the process looks like this: html += "<div class=\"cloumns rentprice\">" + data[i].car_price + "</div>"; I am trying to retrieve the JSON values from this dynamically generated HTML for indexing and sortin ...

Are there any compatibility issues when inserting an <iframe> containing HTML4 content into a HTML5 webpage?

Is there a way to gracefully add an <iframe> with HTML 4 source into a modern HTML 5 web page without causing any JavaScript or DOM conflicts? Can browsers handle the presence of different document source types without encountering errors in unique ...

Adding dynamically fetched JSON to an existing table

http://jsfiddle.net/mplungjan/LPGeV/ What could be improved in my approach to accessing the response data more elegantly? $.post('/echo/json/',{ "json": JSON.stringify({ "rows": [ { "cell1":"row 2 cell 1", "cel ...

Attempting to analyze a JSON string within the vicinity in order to execute a specific function

Currently, I am integrating a local JSON file to simulate an API connection in my project. The challenge I am facing is related to accessing the image key within the products of the JSON data. In my project setup, I have images that are imported using stat ...

The upcoming feature in Discord.js Commando is the need to have an async function for hasPermission() in order to wait for a database query

Currently, I am developing a discord bot with the help of discord.js and its commando framework. One of the important aspects is storing information in a mongodb database such as server prefixes or roles required for specific commands to execute. The com ...

Express application encountering an issue when trying to render a live chart with flot in the Jade client

Encountering an issue while trying to visualize real-time data using a flot chart. The client code displays the following error: Uncaught Invalid dimensions for plot, width = 1584, height = 0 I am perplexed by the fact that the height is showing as 0. ...

Input new information into a MySQL database with a simple click on a designated hyperlink

Is there a method to update information in a mysql database by clicking on a link? I am looking to implement a comment voting system on my website where when a user clicks on the "Vote for this comment" link, it should add +1 to the database... Any ideas ...

Exploring Beautiful Soup to extract child values based on their relationships with their siblings and parents

I'm having trouble extracting the text I-want-ya from: <div class="field"> <div class="labelx"><a class="clickme" href="#h_group123" rel="#h_group123" title="Group">* Group</a></div> <div class="input">I-want ...

Ordering dates by week in AngularJS 1

Currently, I have a list of objects: [{ name: one, date: 2017-09-18 }, { name: two, date: 2017-09-11 }, { name: three, date: 2017-09-13 }] I am looking to organize this list by week. Perhaps like the following structure: { 1week(or , m ...

leanmodel.js is failing to function properly following an AJAX request

Hello, I am new to this platform so please bear with me if I make any mistakes while posting a question. Currently, I am using leanmodel.js to open popups on my webpage. It works perfectly fine initially, but when I update the content of the page using an ...

Unable to detect JavaScript in Chrome Developer Tools

I am experiencing a strange issue where my JavaScript code is not showing in the sources window. When I include a debugger statement in my JS and then reload the page, it successfully breaks and I can view the JavaScript code. However, the tab is labeled ...