www.webdeveloper.com
Results 1 to 4 of 4

Thread: Calling function of external javascript directly

  1. #1
    Join Date
    Nov 2010
    Posts
    27

    Calling function of external javascript directly

    HI

    I have a requirement that my javascript function can be called directly from the other website. I know how to add the external javascript and then call respective function

    <input type="button" value="Testing direct function call of javascript" onclick="CALL THE EXTERNAL JAVASCRIPT(which will call main function in it" />

    how can I create such javascript

    Thanks
    mjask

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259
    I don't believe your request is possible.
    You cannot execute JS from a different domain (not your own).

  3. #3
    Join Date
    Nov 2010
    Posts
    27
    here is what I was looking for

    trial.js
    (function (){

    alert ("i am testing");
    }() );

    now whenever I will add this javascript in any html the alert will appear on its own:

    <script type="text/javascript" src="trial.js" ></script>

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    As long as the external javascript is included on the page via script tags, you could just call the function in it by name.

    Every Javascript on a page, whether embedded inline or loaded from an external .js file will have it's named functions exposed to the page and able to be called.

    Why don't you just do this:
    In trial.js
    Code:
    function myFunc() {
      alert('i am testing');
    }
    and then in your page
    HTML Code:
    <input type="button" onclick="myFunc();"/>
    <script type="text/javascript" src="trial.js"></script>
    Much less complex.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

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