loading javascript library, local vs cdn

Hi there,

I’m struggling with loading a javascript library.

If I use the library from a CDN server it works, when loaded from a local file it doesn’t.
The library is loaded in the HTMLHeader shared method (public).

' in HTMLHeader shared method - loading from CDN - OK
Dim html As string
html = "<script src=""//cdn.domain.com/editor/editor.js""></script>"
return html

Now I’d like to store the javascript locally and tried using a webfile.
I tried opening the local editor.js as a webfile, stored in a session property “localjs”, it’s not working:

' in HTMLHeader shared method - loading from local file - NOT OK
Dim html As string
html = "<script src=""" + localjs.url + """></script>"
return html

When debugging I can see the webfile exists and is found.
Looking at the browser debugger I can see the editor.js is there…

I’m far from skilled in js, can someone put me in the right direction to use local javascript libraries ?
I don’t understand why it’s different when loading from a CDN or from a local file.

Any help appreciated !

Thanks

[quote=222529:@Olivier Colard] in HTMLHeader shared method - loading from CDN - OK
Dim html As string
html = “<script src=”"//cdn.domain.com/editor/editor.js"">"
return html[/quote]

I am not aware of such a shared method. Are you referring to the HTMLHeader Property of the WebApplication that is available in the Inspector ?

You may want to use a WebPageSource and use it’s EditSource event to supply the URL
http://documentation.xojo.com/index.php/WebPageSource.EditSource

Something like this should do it :

Source = "<script src=""" + localjs.url + """></script>"

Hi Michel,

Thanks for answering. I forgot to mention that I was creating a WebSDK class (WebControlWrapper).
I’m using a public HTMLHeader shared method (see WebSDK docs p.17).
This method is adding the html string to the section.

When I use the CDN, editor.js is loaded, when using locals.url the library is found but not loaded…

[quote=222562:@Olivier Colard]Hi Michel,

Thanks for answering. I forgot to mention that I was creating a WebSDK class (WebControlWrapper).
I’m using a public HTMLHeader shared method (see WebSDK docs p.17).
This method is adding the html string to the section.

When I use the CDN, editor.js is loaded, when using locals.url the library is found but not loaded…[/quote]

Have you looked at the code in the browser developer tools ?

Yes I’m using browser developer tools (firebug with firefox and web inspector with safari).
I see my <script src=""" + localjs.url + “”"> entry in the section.
I can click the full path / name and open the editor.js file so I presume the path/filename are correct but that’s all…

I’m getting a javascript error “undefined is not an object (evaluating ‘a.getEditor’)”
a.getEditor is in editor.js file

This is not right. You should see the actual URL instead of the Xojo code.

Something like :

<script src="/35BB12DFDC01ADAFC7C82049CC32E48E/files/7850-5147-7267-5782-1718/editor.js"></script>

Michel,
I didn’t copy the exact string in my post but, here is it:

<script src="/49ACE21568693265AD586BBD0E3C34ED/files/9533-2530-9842-8604-1247/ckeditor.js" type="text/javascript">

[quote=222572:@Olivier Colard]Michel,
I didn’t copy the exact string in my post but, here is it:

<script src="/49ACE21568693265AD586BBD0E3C34ED/files/9533-2530-9842-8604-1247/ckeditor.js" type="text/javascript">

OK. So the method works as expected.

The issue could be using a relative path URL as opposed to absolute. You may want to try this :

"<script src=""http://" +Session.header("host")+ localjs.url + """></script>"

I tried, it adds the IP:port , as expected

<script src="127.0.0.1:8081/312A4CAD8EA504B514B879E770D02FFB/files/8800-5673-5022-1816-9562/ckeditor.js" type="text/javascript">

but I still get the same js error

I tried opening the webfile with inMemory=False to access it on disk but it doesn’t solve it.

Let’s get you back on the right track…

The code you’re using at the top is perfectly fine, but HTMLHeader fires before any Session information is created. If this library is the same for every session, create a shared property on the WebControlWrapper subclass and set its content right in the top of the HTMLHeader event… Specifically,

if localjs=nil then ...create the Webfile here End if

See if that works for you.

Thanks Greg,

That’s already what I’m doing.
localjs is a shared property of the class and the webfile created in the open event:

[code]
’ class open event
dim wf as new WebFile
wf = WebFile.Open(File, false) ’ already tried with false and true
localjs = wf 'localjs is a class shared property

    [/code]

The open event is too late. You need to do it in HTMLHeader.

The reason for this is that all of the WebControlWrapper subclasses are called just as the session starts so their header info can be included in the initial session setup. The Open event doesn’t fire until the control is actually instantiated.

Put it back into HTMLHeader but the problem is the same.
Even if open event was too late, I can see that the script is added to the section, I can open the editor.js file from the browser inspector clicking on the editor.js file.

I’m creating the webfile, storing into a class shared property then building the html header as follow:

Source = "<script src=""" + localjs.url + """></script>"

Here is the section from firebug, when the webfile is created in the HTMLHeader method.

[code]

Untitled [/code]

It seems that the js error is now different, the error seems to happen later in the editor.js.
I’ll investigate this further before you spend more time helping.

I have to compare the local and CDN libraries to make sure they are the same.

Thanks for helping to this point, I’m a step further, but not arrived yet !

Thanks.

Ah, make sure you set the session property of the Webfile to Nil. Otherwise it will only be delivered to one particular session.