JQuery plugins and css

I am using jquery to write plugins in my application. Each plugin needs a different css file. What is the best way to download jquery and css file?

I use plugins on different pages in different places. How to find the absolute path to these files?

EDIT: Does anyone have an answer?

+4
source share
1 answer

I think a fairly standard way is to simply require that the corresponding CSS file is also used whenever the plugin is used.

You may have your jQuery plugin though:

$('head').append('<link rel="stylesheet" href="/css/myPlugin.css" />'); 

Or even:

 if (!$('head>link[href$="myPlugin.css"]').size()) // if the stylesheet isn't already included $('head').append('<link rel="stylesheet" href="/css/myPlugin.css" />'); 

Better yet, put this in your own plugin:

 $.extend({addStyleSheet : function (nameOfPlugin) { if (!$('head>link[href$="' + nameOfPlugin + '.css"]').size()) // if the stylesheet isn't already included $('head').append('<link rel="stylesheet" href="/css/' + nameOfPlugin + '.css" />'); }); 

Then in your plugin just name it like this:

 $.addStyleSheet('myPlugin'); 
+2
source

Source: https://habr.com/ru/post/1333197/


All Articles