0

I have a JavaScript Function in an HTML Page's . It will return a value. The returning value must be caught from another JavaScript Function. This 2nd JavaScript Function is in an external JavaScript File. This external JS File is linked to the HTML file.

So how am I suppose to do this.

Could someone help me on this matter?

Thanks and regards, Chiranthaka

Update

HTML File.(Dashboard.html)

<head>
      <script type="text/javascript" src="js/script.js"></script>
        <script type="text/javascript">
          function setJsonSer() {
                    formData = {
                    'Email': '[email protected]',
                    'Password': 'russell1234',
                    'URL': getVaria()
                };
                    $.ajax({
                    url: "/APIWebService.asmx/AnalyticsDataShowWithPost",
                    type: 'POST',
                    data: formData,
                    complete: function(data) {
                        alert("Set JSON In  "+JSON.stringify(data));
                    }
                 });

            $.ajax({
                url: "/APIWebService.asmx/AnalyticsDataShowWithPost",
                type: 'GET',
                data: formData,
                complete: function(data) {
                    alert("Get JSON Out  "+JSON.stringify(data));
                }
            });
      }

   </script>
</head>

External JavaScript File - Script.js

function retStartDate(){

 var strStartDate = document.getElementById("from_date").value;

 alert("Start date " + strStartDate);

 return strStartDate;

}

function retEndDate(){

 var strEndDate = document.getElementById("to_date").value;

 alert("End date " + strEndDate);

 return strEndDate;

}

function getVaria(){

 alert(retEndDate());

 var strWsUrl =
   'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+
   'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-  
   date='+retStartDate()+'&       
   end-date='+retEndDate()+'&max-results=10';

 alert("Query String "+strWsUrl);

 return strWsUrl;

 } 

--End of update ----

5
  • 1
    This question does not have enough detail. You need to show how the external JS file is declaring its function. There are some ways of declaring functions that prevent it from appearing in the global scope. Without seeing code, we have no way of knowing if this is the case Commented Sep 10, 2014 at 6:21
  • There is quite a lot of code there. What function are you having trouble calling? Commented Sep 10, 2014 at 6:40
  • function setJasonSer(){ } Commented Sep 10, 2014 at 6:44
  • ?? but you're not calling setJasonSer from your external file Commented Sep 10, 2014 at 6:48
  • I want to call that setJsonSer() from the external JavaScript File. Commented Sep 10, 2014 at 7:28

1 Answer 1

3

In your HTML page you need to define a function which is globally accessible.

For example the following function foo will be accessible from your external file:

<script type="text/javascript">
    function foo (){
        // do something
    }
</script>

This simple trick trick should do the trick.

Sign up to request clarification or add additional context in comments.

4 Comments

When your answer considered setJasonSer() function in the HTML page within the <head></head> can be caught from the external JavaScript File?
Yes you can access setJasonSer() within head tag from external file.
Then how can I do that pal?
I have uploaded an example with explanation: dropbox.com/s/rxo833dzch9gnqc/ExternalAccess.zip?dl=0 Please have a look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.