www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript Require File To Run

  1. #1
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210

    Javascript Require File To Run

    Hi,

    Is it possible to make a .js file only run if another .js file is located.

    so lets say I have:

    Code:
                                    Index.html
                                           |
                                           |
                         -----------------
                        |
                        |
                    Scripts.js
                         |
                  ------
                 |
            Required.js
    in the Index it will call to Scripts.js but then scripts.js will only run if Required.js is in the same directory.

    Simon

  2. #2
    Join Date
    Dec 2005
    Posts
    2,984
    Well, there are no include or require statements in JS, so the only way you can do it is to put both script tags in index.html.

    However, you have an ability to check then. In scripts.js you would use this:
    Code:
    if(typeof yourFunctionName == 'function') { 
       //the rest of your code
    }
    Replace yourFunctionName in the above statement with a function name that is found in Required.js. If Required.js has been loaded, then typeof will return 'function' - voila - a way to 'require' javascript.

  3. #3
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    Nice!! thanks aj will give that one a go

  4. #4
    Join Date
    Sep 2009
    Location
    oxfordshire, england
    Posts
    210
    Ok got it now, Solved

  5. #5
    Join Date
    Dec 2009
    Posts
    19
    Quote Originally Posted by aj_nsc View Post
    Well, there are no include or require statements in JS, so the only way you can do it is to put both script tags in index.html.

    However, you have an ability to check then. In scripts.js you would use this:
    Code:
    if(typeof yourFunctionName == 'function') { 
       //the rest of your code
    }
    Replace yourFunctionName in the above statement with a function name that is found in Required.js. If Required.js has been loaded, then typeof will return 'function' - voila - a way to 'require' javascript.
    Not Natively, however there is a site made for converting PHP functions into javascript functions. http://phpjs.org/functions/include:433 , the function uses an ajax call to retrieve a files contents.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles