Recent Topics

Widget JavaScript and Css

Started by on Oct 17, 2016 – Contents updated: Oct 20, 2016

Oct 17, 2016 18:21    

I came across this issue several times for widget type plugins. When creating a new plugin that is a widget type which requires custom js or css for each instance becomes a coding nightmare. For example: when installing a slider widget, each instance requires custom JavaScript / css for unique element id's, functionality or styling to be included either before </head> or </body>.

Is there a proposed solution?

Oct 19, 2016 15:41

I don't understand why there is a problem here.

Oct 19, 2016 15:51

I won't call it a problem. It's just makes life a bit difficult.

If I create a widget type plugin for layersliders, then every instance where I add a new widget (layerslider) I must include new JavaScript for the new layerslider to work, because each new instance has a unique ID. The JavaScript identifies the slider by that I'd and adds functionality, skins ect...

Currently there is no way to add JavaScript for each new instance. JavaScript is only loaded once on the Plugin installation.

This means, currently, the only way I can new JavaScript for each widget instance is by specifying a JavaScript file in the installation, then every time a new instance of the widget is created, it must use regular expression to add new JS to the "default" file.

When the instance of the widget is removed, regular expression must look for the JavaScript in the "default" JS file for that widget, identify the right part, and remove it.

Currently I have it working like that, but it is a heavy runaround.

Wouldn't it be easier that I can add JS for each instance of a widget when it is created?
Does this make sense or do I need to explain myself better?

Oct 19, 2016 22:30

I don't understand this on so many levels...

  1. What is "layersliders" ?
    • Is it an existing piece of javascript you got from somewhere else that can only work for a specific ID in the DOM?
    • And then instead of modifying the script to work with multiple IDs you want us to modyfy b2evolution so you can easily duplicate the script?
  2. "Javascript loaded on installation"? I have no idea what you mean here. In my mind, if a plugin wants to use javascript, it must be inhjected into the <head> by using some event and each plugin can inject as many scripts as it wants.

And please make an annotated screenshot. it will really be worth a 1000 words.

Oct 19, 2016 23:52

Okay. I will get back to you on this with better explanation because currently you clearly not understanding what I am trying to convey. Should have a better (hopefully simpler) explanation by tomorrow or so.


Form is loading...

multiblog platform – This forum is powered by b2evolution CMS, a complete engine for your website.