5 Replies Latest reply: May 17, 2012 11:39 PM by MichiHenning
rmdissa Level 1 Level 1 (0 points)

Hello,

 

I am trying to access JavaScript files outside the widget (but they are saved locally). When I embed the html/dashboard widget in ibook Author, the html widget on ibook doesn't run the JavaScript.

 

I did this at first using the html tag:

 

<script type="text/javascript" language="javascript" src="path of file that is saved outside the widget">....

 

And since ibook author doesn't support the key AllowFileAccessOutsideOfWidget, I would like to know if there is a way to link/access files (Javascript files) outside the widget?.

 

My JavaScript is the base functionality for many pages (mathematical material), I don't want to add the functionality everytime inside a widget, because it is huge in size >30MB. Is there any way to overcome this problem, while accessing the scripts should be done locally (so it works offline).

 

Many thanks

Rana

  • MichiHenning Level 4 Level 4 (1,350 points)

    rmdissa wrote:

     

    <script type="text/javascript" language="javascript" src="path of file that is saved outside the widget">....

     

    Hmmm… I have no problem with the following:

     

    <script type="text/javascript" src="main.js" charset="utf-8"></script>

     

    main.js lives in the root directory of the widget.

     

    Is the script file you are trying to run inside the widget bundle? If not, I'd try putting it there.

     

    I take it that you remembered to provide the start function for the script? Something like

     

    <body onload="load();">

    ...

    </body>

     

    Michi.

  • rmdissa Level 1 Level 1 (0 points)

    Hello Michi,

     

    No, I didn't add onload="load();" to the body of html. I am not using Dashcode to create my html/JavaScript code.

     

    You have mentioned that src="main.js" works for you, but how if you try to put the main.js outside the widget directory (or if you call it widget bundle) and link it using the html tag as follows:

     

    src="../code/web/main.js">

     

     

    What happens then? In my case, the JavaScript is not called. And that is my problem I would like to solve.

     

    Many thanks

    Rana

  • MichiHenning Level 4 Level 4 (1,350 points)

    rmdissa wrote:

     

    Hello Michi,

     

    No, I didn't add onload="load();" to the body of html. I am not using Dashcode to create my html/JavaScript code.

    You have to specify a start function, otherwise the browser run time has no idea which function in your Javascript to call. This is necessary whether you use Dashcode or not.

     

    You have mentioned that src="main.js" works for you, but how if you try to put the main.js outside the widget directory (or if you call it widget bundle) and link it using the html tag as follows:

     

    src="../code/web/main.js">

     

    What happens then? In my case, the JavaScript is not called. And that is my problem I would like to solve.

    I strongly suspect that the script you want to call must be inside the widget bundle. It is highly doubtful that the script would be allowed to access any other part of the iBooks bundle. (I haven't verified this, but I would expect that to fail.)

     

    I would first up make a very simple JavaScript that sits inside the widget bundle and displays something. Verify that this works. You will need to specify which function to call in response to the onload event.

     

    Now, I have absolutely no idea how you would put your Javascript elsewhere. One approach you could try is to unzip the .ibooks file, put the Javascript code somewhere inside it, and modify your HTML to try and use that script. Then see whether it runs.

     

    If not, I suspect you are out of luck. And I would expect the script be prevented from accessing any files outside its own widget bundle because, otherwise, the script could escape from its sandbox and do all sorts of malicious things.

     

    Michi.

  • rmdissa Level 1 Level 1 (0 points)

    Hello Michi,

     

    Thanks a lot for your reply. I am also now suspecting that what I want to do is possilbe.

     

    However, talking about the onload() method. I am able to make my widget work when I have the JavaScript inside the widget bundle. I don't have now problems with that, but we (the group) are trying to find a way to minimize the size of the widget and consequently the size of the ibook file.

     

    I also, thought of your suggestion of unzipping the file today. I will try it and will let you know.

     

    Many thanks for your thorough explanation

    Rana

  • MichiHenning Level 4 Level 4 (1,350 points)

    rmdissa wrote:

     

    However, talking about the onload() method. I am able to make my widget work when I have the JavaScript inside the widget bundle. I don't have now problems with that, but we (the group) are trying to find a way to minimize the size of the widget and consequently the size of the ibook file.

    Well, you can put Javascript inline between the <script> tags and call a function from there. In that case, the script will start running even before the page has completed loading. The approach that is generally taken is to specify a start function, such as load(), which is called in response to the onload event, which doesn't fire until after the page has finished loading.

     

    Please let us know what you learn!

     

    Michi.