Add javascript library equivalent to script tag in head?

Just starting out? Need help? Post your questions and find answers here.
Stefan Schnell
Posts: 46
Joined: Tue Dec 01, 2015 8:17 am
Contact:

Add javascript library equivalent to script tag in head?

Post by Stefan Schnell »

Hello community,

at first a happy new year to all.

Now my question:

I want to use the mobile detect library from here: http://hgoebl.github.io/mobile-detect.js/ with the following code:

Code: Select all

; Begin-----------------------------------------------------------------

  ; Sub LoadScript------------------------------------------------------
    Procedure LoadScript(script.s, *func)
      !$.getScript(v_script, p_func);
    EndProcedure 

  ; Sub Main------------------------------------------------------------
    Procedure Main()
      !var md = new MobileDetect(window.navigator.userAgent);
    EndProcedure

  ; Main----------------------------------------------------------------
    LoadScript("mobile-detect.js", @Main())

; End-------------------------------------------------------------------
All I get is a reference error (Uncaught reference error: MobileDetect is not defined). What is my error?

Thanks for hints and tips.

Cheers
Stefan
Last edited by Stefan Schnell on Sat Jan 02, 2016 6:30 am, edited 1 time in total.
Stefan Schnell
Posts: 46
Joined: Tue Dec 01, 2015 8:17 am
Contact:

Re: Add javascript library equivalent to script tag in head?

Post by Stefan Schnell »

Hello community,

I tried a little bit more and find out that the manually adding of

Code: Select all

<script type="text/javascript" src="mobile-detect.js"></script> 
in the SpiderBasic_Compilation0.html works fine. On this case I can use the library without any errors. So I changed the program to add this code automatically, but I doesn't work also.

Code: Select all

; Begin-----------------------------------------------------------------

  ; Sub LoadScript------------------------------------------------------
    Procedure LoadScript(script.s, *func)
      !$.getScript(v_script, p_func);
    EndProcedure 

  ; Sub Main------------------------------------------------------------
    Procedure Main()

      !var v_script = document.createElement("script");
      !v_script.setAttribute("type", "text/javascript");
      !v_script.setAttribute("src", "mobile-detect.js");
      !document.getElementsByTagName('head')[0].appendChild(v_script);

      !var md = new MobileDetect(window.navigator.userAgent); 

    EndProcedure

  ; Main----------------------------------------------------------------
    LoadScript("mobile-detect.js", @Main())

; End-------------------------------------------------------------------

Code: Select all

; Begin-----------------------------------------------------------------

  ; Sub LoadScript------------------------------------------------------
    Procedure LoadScript(script.s, *func)
      !$.getScript(v_script, p_func);
    EndProcedure 

  ; Sub Main------------------------------------------------------------
    Procedure Main()

      ; Variables-------------------------------------------------------
        Protected md.i

      !v_md = new MobileDetect(window.navigator.userAgent);

    EndProcedure

  ; Main----------------------------------------------------------------
    !var v_script = document.createElement("script");
    !v_script.setAttribute("type", "text/javascript");
    !v_script.setAttribute("src", "mobile-detect.js");
    !var v_head = document.getElementsByTagName("head")[0]
    !v_head.insertBefore(v_script, v_head.firstChild);
    ;!v_head.appendChild(v_script);

    LoadScript("mobile-detect.js", @Main())

; End-------------------------------------------------------------------
What is the correct way to add a JavaScript library in SpiderBasic equivalent to the script tag in the html header?

Thanks for tips and hints.

Cheers
Stefan
Last edited by Stefan Schnell on Sat Jan 02, 2016 4:18 pm, edited 1 time in total.
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Add javascript library equivalent to script tag in head?

Post by Peter »

Hello Stefan,

normally both methods ($.getScript() or adding the script to the header) should work.
It may be that it is a dojo/AMD-thing. Perhaps Fred knows more?

Greetings ... Peter
Stefan Schnell
Posts: 46
Joined: Tue Dec 01, 2015 8:17 am
Contact:

Re: Add javascript library equivalent to script tag in head?

Post by Stefan Schnell »

Hallo Peter,

thank you very much for your reply.
:)

Cheers
Stefan
Post Reply