Jump to content

Topic on Project:Support desk

How to add "script with data attributes" to Common.js?

3
Stefahn (talkcontribs)

I have a small script like this: <script data-leadbox="foo1" data-url="foo2" data-config="foo3" type="text/javascript" src="https://foo4.com/script.js"></script>

I know in Common.js I can load a script via mw.loader.load('https://foo4.com/script.js");.

But how do I add the data attributes to my code?

I tried things like

data-leadbox="foo1";
data-url="foo2";
data-config="foo3";
mw.loader.load('https://foo4.com/script.js");

but this doesn't work.

Wargo (talkcontribs)

It is not possible. If you can change that script, prepare it to use variables, so you can use the second way.

TheDJ (talkcontribs)

This is an inline script element and can not be used this way from Common.js. You either need variables or a callback (ready) hook or something for that to work (most libraries provide an alternative setup routine like that). Video.js for instance provides both an HTML setup process, as well as a dynamic loader process.

As a last ditch effort, it is of course always possible to directly modify the PHP codebase of the skin or something, to output it regardless, but then you will have to maintain that yourself.

Reply to "How to add "script with data attributes" to Common.js?"