Titanium Community Questions & Answer Archive

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

Include js file using absolute path

Pretty basic question, but it's been giving me issues for a while - how do you include a javascript file using an absolute path? The following doesn't seem to work:

Ti.include("/folder/file.js");

If I use a relative path from the current file, it's fine, but that will get messy pretty quick. Is there some other way to reference the root directory?

— asked May 18th 2010 by Mike Dosey
  • absolute
  • file
  • include
  • iphone
  • mobile
  • path
  • relative
3 Comments
  • It does work on Android, only not on iphone ;)

    — commented August 10th 2010 by Friedrich Seydel
  • so much for cross-platform :/

    — commented May 18th 2011 by Markus Balsam
  • I've got it working on iPhone, not Android.

    — commented July 9th 2012 by Ryan Pelham

14 Answers

  • Not as easy as I'd like it to be, but this works fine:

    Ti.include(Titanium.Filesystem.resourcesDirectory+'file.js');
    
    — answered September 16th 2010 by Mike Griffiths
    permalink
    2 Comments
    • This doesn't work. Ti.include() only works with relative paths. It will generate a url to the directory of the current file (like this: file://path/to/your/current/directory) and then append the contents of the include statement. By using Titanium.Filesystem.resourcesDirectory you're going to end up with a URL that looks like file://path/to/your/current/directory/plus/path/generated/by/Titanium.Filesystem.resourcesDirectory/file.js. Which will fail.

      — commented September 20th 2010 by Brandon Jackson
    • Had to change to this:

      Titanium.Filesystem.resourcesDirectory+'functions/included_file.js'
      

      from:

      'functions/included_file.js'
      

      When I switched from Ti SDK 1.6.2 - 1.7.1 to make the app find the included JS.

      — commented July 22nd 2011 by david brewer
  • Here's a small, contrived example:

    1. Resources/app.js
    2. Resources/include/someFile.js
    3. Resources/database/DataAccess.js

    If you want to run it, put a Content.db database file in "Resources/database/" (doesn't matter what's in the database).

    So basically, DataAccess.js just defines a db communication object which installs Content.db in the constructor. Inside app.js, I can create an instance of DataAccess and everything works fine, because the install path is relative to the Resources folder. However, if I try to create an instance of DataAccess inside someFile.js, which is created as a new window by app.js, the application will crash because the database install path is no longer correct. (Titanium is looking relative to the "Resources/include" folder)

    Now it's not so bad to work around the problem in a small example like this with just a couple files. But I'm trying to build a fairly deep directory structure, where many files would be including a base DataAccess.js file.

    — answered July 31st 2010 by Mike Dosey
    permalink
    0 Comments
  • can we create visualizer in titanium

    — answered February 8th 2012 by Khurram Sheikh
    permalink
    0 Comments
  • I have this problem too. I have a single JS file that includes all of the other JS files I need. But that doesn't work in any other path. How?

    — answered June 8th 2010 by Scott Penrose
    permalink
    0 Comments
  • This is still an issue for me. I have several files that all include a base Database.js file, which contains a Database.install(…) command. But the path to the database changes for the different files that include Database.js. I just need some way to start from the Resources folder, rather than always having to provide a relative path.

    — answered July 30th 2010 by Mike Dosey
    permalink
    0 Comments
  • You can use Ti.includeAbsolute to include starting from the Resources directory.

    It is an undocumented API, but it works a lot better. (on every platform)

    — answered March 30th 2011 by Christian Brousseau
    permalink
    2 Comments
    • [2364] <Notice>: [ERROR] application received error: Result of expression 'Ti.includeAbsolute' [undefined] is not a function. at app.js (line 1)

      on my iphone… maybe it only works with an higher titanium sdk verison.

      currentlly i'm using:
      [INFO] Titanium SDK version: 1.6.1
      [INFO] iPhone Device family: iphone
      [INFO] iPhone SDK version: 4.3

      and in simulator it works but not on device… so beware of using undocumented methods :)

      any solution?

      — commented April 8th 2011 by Florian Bergmann
    • Doesn't work.

      — commented August 2nd 2011 by Justin Toth
  • 'Ti.include(path/file);' works fine for what is supposed to do. HOWEVER it does not work with absolutes paths. So consider the file structure:

                 include.js
            dir1
    

    Resources

            dir2
                 fileToIncludeIn.js
    

    In fileToIncludeIn.js one cannot include the include.js file.

    A general solution would be to have all your files in one directory. If someone can solve this problem please post.

    P.S. speaking from iPhone perspective.

    — answered July 1st 2011 by BORA Catalin-Andrei
    permalink
    0 Comments
  • What version of Titanium are you using? this works fine for me here,

    Ti.include(&#39;path&#x2F;file.js&#39;);

    will include file at resources/path/file.js

    i can then do includes in file.js

    Ti.include(&#39;anotherpath&#x2F;file.js&#39;);

    will include resources/anotherpath/file.js

    or have i misunderstood the question?

    — answered July 31st 2010 by Richie Mortimer
    permalink
    1 Comment
    • This does not work for me. Is the file you're running "Ti.include" from in the root directory? If so, then it does work, because you're already at the root and this is a relative path. That's not what the question is asking.

      The poster is asking how to reference the root directory from an arbitrarily nested subfolder (an absolute path).

      — commented October 24th 2010 by Tom Lianza
  • I gave up trying to deal with this issue. But I notice that the KS has the same issue. Notice that all of there examples are in a single Dir.
    What I have done is never go more than 1 level of dirs down. So I have dirs for TAB01, TAB02, TAB03, TAB05… TAB02-procA, TAB02-procB… PROC01, PROC02… RESOURCES/INCLUDE/InFile01.js
    This way, all of the related code dirs tend to cluster together when listing for organization.
    I can always use Ti.include('../INCLUDE/fileToInclude.js'); from any other file. I just never put JS files at root other than the app.js.
    And this works really well for me.

    — answered November 18th 2010 by vincent youmans
    permalink
    0 Comments
  • Anyone?

    — answered May 24th 2010 by Mike Dosey
    permalink
    0 Comments
  • This works for me,
    may have to change the amount of folder escapes for your situation

    var fh = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'images/temp.js');
    
    Ti.include('../../../../../../../../../../'+fh);
    
    — answered June 8th 2010 by Robert Greenock
    permalink
    1 Comment
    • I use prefixes too, but how is what you wrote any different than this?

      Ti.include(&#39;..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;..&#x2F;&#39;+images&#x2F;temp.js);

      — commented September 20th 2010 by Brandon Jackson
  • I gave up trying to deal with this issue. But I notice that the KS has the same issue. Notice that all of there examples are in a single Dir.
    What I have done is never go more than 1 level of dirs down. So I have dirs for TAB01, TAB02, TAB03, TAB05… TAB02-procA, TAB02-procB… PROC01, PROC02… RESOURCES/INCLUDE/InFile01.js
    This way, all of the related code dirs tend to cluster together when listing for organization.
    I can always use Ti.include('../INCLUDE/fileToInclude.js'); from any other file. I just never put JS files at root other than the app.js.
    And this works really well for me.

    — answered November 18th 2010 by vincent youmans
    permalink
    0 Comments
  • Posted this in the other threads: https://appcelerator.lighthouseapp.com/projects/32238/tickets/2585-include-paths-for-iphone-and-android-are-not-based-on-the-same-starting-point

    This issue has been resolved with 1.7:
    http://builds.appcelerator.com.s3.amazonaws.com/index.html

    — answered March 30th 2011 by John Welch
    permalink
    0 Comments
  • I gave up trying to deal with this issue. But I notice that the KS has the same issue. Notice that all of there examples are in a single Dir.
    What I have done is never go more than 1 level of dirs down. So I have dirs for TAB01, TAB02, TAB03, TAB05… TAB02-procA, TAB02-procB… PROC01, PROC02… RESOURCES/INCLUDE/InFile01.js
    This way, all of the related code dirs tend to cluster together when listing for organization.
    I can always use Ti.include('../INCLUDE/fileToInclude.js'); from any other file. I just never put JS files at root other than the app.js.
    And this works really well for me.

    — answered November 18th 2010 by vincent youmans
    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.