Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

javascript include in webview not working

I'm trying to include a js file (jquery-1.3.2.js copied from KitchenSink webview example) in an html page hosted in a webview:

var webview = Ti.UI.createWebView();
webview.url = 'test.html';

win1.add(webview);

and the html page code:

<html>
    <head>
        <script type="text/javascript" src="jquery-1.3.2.js"></script>
    </head>
<body style="height:0px;width:0px;">
    <script>
        Ti.API.info("Page is Loaded');
        try{
            $.ajax(...);
        }catch(e){
            Ti.API.error(e);
        }
    </script>
</body>

The page loads just fine in the webview (I can see the load message) BUT
I get an error messages saying that $ is undefined…

In KitchenSink it works perfectly (local_webview.html).

I even tried to copy the example "as is" (including all js, folder structure etc) to my project and it fails to load the js!

Tried running both in 1.2.0 AND 1.1.2 with same result.

Any clue?

Thanks,
Chen.

— asked May 4th 2010 by Chen Bekor
  • error
  • include
  • javascript
  • jquery
  • kitchensink
  • webview
0 Comments

16 Answers

  • A working solution for this is to give your js files a different extension. In my case I used .jsf.

    <script type="text/javascript" src="jquery-1.4.2.min.jsf"></script>
    

    Granted you'll lose syntax highlighting, so you'll likely have to rename back and forth until you're ready to sync with your device.

    — answered August 8th 2011 by Michael Reyes
    permalink
    6 Comments
    • Worked for me both in the simulator and on the device.

      — commented September 5th 2011 by Austin M
    • won't work, see my last comment in this thread please

      — commented May 13th 2012 by Patrice Ferlet
    • Doesn't work.

      — commented October 1st 2012 by Justin Toth
    • Confirmed it worked in titanium 3.00 GA ios 6

      1. if it doesn't try loading it in browser, if it fails to find the jsf file, meaning ur O/S is hiding the "actual extension", pls use some editor to double check the files "real extension" also.

      I use sublime and rename those as EJS, later i installed the EJS syntax highlighting. It allows me a peace in development for time being but, this problem has to be documented before more developed stumbled into this

      — commented January 17th 2013 by Alex T.
    • I voted for your solution because it worked, but seriously… now I'm feeling dirty for renaming my js files… But it worked!

      — commented February 2nd 2013 by Olivier Dupont-Therrien
    • Ok Michael but what about:

      For JavaScript files referenced in static local HTML files, these JavaScript files are omitted from processing and left intact, which means they can be correctly loaded  in the WebView.
      

      http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.WebView

      — commented March 19th 2014 by Andrey Tkachenko
  • Hi Chen, I am having the exact same problem. I can't work it out at all and I have been trying for two days now.

    The same code works fine in the iphone simulator but fails to fire in the ipad one. Normal JS is fine too. I can do alerts and messages. Just files won't load.

    — answered May 4th 2010 by Daniel Chapman
    permalink
    0 Comments
  • I'm having the problem too. Hello devs, please fix this.

    — answered May 9th 2010 by Sindre Sorhus
    permalink
    0 Comments
  • Same here - trying many things without any progress. js Files just want load.

    Any help from someone who solved this issue is appreciated,

    Thanks,

    Chen.

    — answered May 4th 2010 by Chen Bekor
    permalink
    0 Comments
  • I even tried using normal script tags and then dynamically loading the files, both by using a document.write, and appending to the dom manually, but neither worked.

    I am wondering if rather than our scripts not getting loaded, our scripts aren't getting included?

    I am not on my testing computer right now, but if you have a chance check with the file system api if you can detect the actual files at runtime.

    I just had a thought now, could we read the file contents, and then eval it as a string? Ugly workaround, but it might work.

    — answered May 4th 2010 by Daniel Chapman
    permalink
    0 Comments
  • ok, here is something for you. I copied the entire resources directory from KS over to my app. Webviews don't work anylonger in ks.

    it must be something outside the resources folder that is required

    — answered May 4th 2010 by Daniel Chapman
    permalink
    0 Comments
  • Any update on this issue ?

    I have the exact same problem, trying to use jQuery in a webview.

    Tried :

    <script type="text/javascript" src="../jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
    

    But the only working solution is to include the code from a remote location :

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    

    Problem is, this won't work offline.

    Any help would be GREATLY appreciated.

    — answered September 9th 2010 by HMax
    permalink
    1 Comment
    • I'm having the same problem. Did you ever get this to work so that you don't have to include it from a remote location?

      — commented May 16th 2012 by Chris Bill
  • Is there a reason why this is neccesary in some places, but not in others (such as when a web view is created in an event listener versus not)

    — answered March 9th 2012 by Zack Carlson
    permalink
    0 Comments
  • Very strange but I tried every solutions and my js file is never imported… A simple "alert" won't be executed and the debug console says that is a chromium error -6…
    The html file is in "screens" directory, my jsfile too..

    I tried src=:

    • jsfile.jsf
    • ./jsfile.jsf
    • screens/jsfile.js
    • /screens/jsfile.jsf
    • app://screens/jsfile.jsf

    And same with ".js" extension… If I put my code inside "script" tag in my webview, it works… So, what is the correct solution to import jsfile in webview

    — answered May 13th 2012 by Patrice Ferlet
    permalink
    3 Comments
    • I'm having the same problem. Did you have any luck with finding a solution?

      — commented May 16th 2012 by Chris Bill
    • No solution… I must write javascript inside script tags for now… I wanted to create a "template" class that import javascript inside html then set HTML in webview, but I've got problem reading files now… I will post a bug report…

      — commented May 17th 2012 by Patrice Ferlet
    • Ok, I found a "bad solution" that makes a trick. I created a "template" class that insert scripts. I will give my working code here… and an explanation for "how to use".

      My template class can be used to inject other datas inside html. It could be nice… I will create a github repository to allow some imporvements. That will be BSD licenced (to allow you to include code in commercial application)

      Just give me a few hours to prepare…

      — commented May 17th 2012 by Patrice Ferlet
  • Because I didn't found any solution… I now use a template system that I shared here:
    titanium-template on github

    If you want to help, fork project and please send pull requests

    Regards

    — answered May 17th 2012 by Patrice Ferlet
    permalink
    0 Comments
  • also you can try this:

    <script type="text/javascript">
    document.write("<sc"+"ript type=\"text/javascript\" src=\"jquery.txt\"></scri"+"pt>");
    </script>
    

    it worked for me.

    — answered December 20th 2012 by Hasan ZORLU
    permalink
    0 Comments
  • 1- for android application you have to put
    <property name="ti.android.compilejs" type="bool">false</property>
    to avoid processes all javascripts files on recources folder. so you can you relative paths as web development

    2- also keep in mind that don't use minify files

    — answered April 29th 2014 by Mohmmad Khanfar
    permalink
    0 Comments
  • 1- for android application you have to put
    <property name="ti.android.compilejs" type="bool">false</property>
    to avoid processes all javascripts files on recources folder. so you can you relative paths as web development

    2- also keep in mind that don't use minify files

    — answered April 29th 2014 by Mohmmad Khanfar
    permalink
    0 Comments
  • Michael Reyes and Leslie Rohde

    See Michael Reyes solution above. I confirm that this works as well. Leslie Rohde has a similar solution. Awesome job Michael and Leslie!!! Saved me a lot of trouble.

    Thanks to You Both

    — answered November 2nd 2011 by Jason Dielman
    permalink
    0 Comments
  • Michael Reyes solution works fine for me. Also make sure to have a type="text/javascript" in your script tag to tell the browser (web view) how to handle the contents of your file. Finaly I added the .jsf file extension to content types list to get back code highlighting and validaton

    — answered January 22nd 2014 by N K
    permalink
    0 Comments
  • The ultimate cause of this problem is likely buried deep in the Titanium tool chain somewhere, but the symptom I observed in my own successful resolution of this is that .js files in the Resources directory do not make it into your app as is – they are probably compiled to ObjC!

    What I am doing is this:

    var f0 = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'jquery.txt');
    var webkitScript0 = f0a.read().text;
    webview.evalJS(webkitScript0);
    

    Because I need to poke jquery into a remote page, but by extension I would think you could simply rename the jquery file as I have and make your html script tag load it under the changed name.

    — answered May 14th 2010 by Leslie Rohde
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.