Forum Discussion

nibo's avatar
nibo
Mentor
11 years ago

Add external JS file with liaAddScript

Hi guys,

 

is it possible to pass some kind of parameter to <@liaAddScript> where I can define an url to an external JS file?

 

In my current use case the file is the jquery-cookie plugin and is located in the assets library.

 

The challenge is that jquery plugins require the jquery library to be loaded first and I can ensure this only by using <@liaAddScript>.

 

Thanks for your help in advance.

Nikolay

  • Hi Nikolay,

     

    I don't think you can do this in the <@LiaAddScript> , but maybe what you can do is to use LiaAddScript to dynamically load  the external script and then evalutate it, or use something like jQuery.getScript

     

    So your code can be then written when you are sure that the external library has actually been loaded. Of course there are security implications when doing this but in this case it should be okay as you are loading a script you actually control.

     

    Hope this helps,

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)

    Hi Nikolay,

     

    I don't think you can do this in the <@LiaAddScript> , but maybe what you can do is to use LiaAddScript to dynamically load  the external script and then evalutate it, or use something like jQuery.getScript

     

    So your code can be then written when you are sure that the external library has actually been loaded. Of course there are security implications when doing this but in this case it should be okay as you are loading a script you actually control.

     

    Hope this helps,

    • nibo's avatar
      nibo
      Mentor

      Hi Paolo,

       

      great idea, thanks.

       

      I don't know why I didn't think of that, since I'm doing something similar somewhere else :)

       

      Regards,

      Nikolay