Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requirejs add arguments to the script element

Tags:

requirejs

is it possible to add arguments to the sctipt element

e.g.

<script 
    ...
    src="XX.js"
></script>

i want to add the argument hello="world" so this is added to the page:

<script 
    ... 
    src="XX.js"
    hallo="welt"
></script>

Reason: I have a js library (aloha-editor) which depends on a parameter for loading it's plugins (and the main functionallity is actuall in a plugin). However I only want to load the plugin when the user want's to edit and requirejs is the best choice since it is used at other parts in the application.

like image 633
Stefan Avatar asked Aug 01 '12 10:08

Stefan


1 Answers

If you are wrapping the aloha script in a define function you could pass arguments like this (link):

//in main appfile
require.config({
    'config': {
        'aloha': {
            src: "XX.js",
            hallo: "welt"
        }
     }
});

//and in the aloha file
define(['module'], function (module) {
    var src = module.config().src,
        hallo = module.config().hallo;

    ... // the aloha code
});

There was also an alternative "simplified" syntax in the docs.

Edit: Maybe I misunderstood the question, if you where asking if it's possible to add html attributes to a script tag already on the page: <script src='' data-src='xx.js' data-hallo='welt'></script> is the new html5 syntax for adding custom attributes to tags, everything preceded by data- is correct syntax.

like image 145
Trond Hatlen Avatar answered Oct 08 '22 08:10

Trond Hatlen