Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:12:05+00:00 2026-06-08T04:12:05+00:00

Case 1: I have passed a variable to a external js file like this

  • 0

Case 1: I have passed a variable to a external js file like this

<script type="text/javascript">

var data1, data2, data3, data4;

function plotGraph() {
    var oHead1 = document.getElementsByTagName('HEAD').item(0);
    var paramScript = document.createElement("script");
    paramScript.type = "text/javascript";
    paramScript.setAttribute('data1', data1);
    paramScript.setAttribute('data2', data2);
    paramScript.setAttribute('data3', data3);
    paramScript.setAttribute('data4', data4);
    oHead1.appendChild(paramScript);
    var oHead = document.getElementsByTagName('HEAD').item(0);
    var oScript = document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src = "js/graph.js";
    oHead.appendChild(oScript);
}
</script>

Case 2: I even tried passing it like this using jquery

<script type="text/javascript">   

    function plotGraph() {
        var data1, data2, data3, data4;
        $.getScript("js/graph.js");
    }
</script>

In the first case which is working but i had to create global variables… I dont want this…

In the second case which is not working has local variables which are not recognized in the js file..

How shall i do it? Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:12:06+00:00Added an answer on June 8, 2026 at 4:12 am

    Variables can only be shared between separate scripts if they are:

    1. Global variables
    2. If they are somehow in the same context (hard with multiple external scripts)
    3. If you provide a function in one module that returns access to the variables.
    4. If you call the other script and pass the variables or a reference to the variables to the other script.

    Edit: Now that the OP has explained what they’re really trying to do (communicate data from an ajax call to functions in an external script), the real answer to this issue is this:

    You should call a global function in your external script FROM the success handler of the ajax call and pass these data elements to that function. The function in the external script can then either act on the data immediately or save the data in it’s own variables for later use.


    FYI: $.getScript() loads a script at the global level. That’s why your case 2 doesn’t work.

    One way to share access to a group of variables is to put them all as properties of an object and then either make that object be global or provide a global function that retrieves access to that object. For example, you could declare a single global object with a number of properties.

    var myConfigObject = {
        data1: value1,
        data2: value2,
        data3: value3,
        data4: value4
    };
    

    To call a function in your external script file, you would do the following:

    1. Define a globally accessible function in your external script file. Let’s call it doIt(a, b, c, d) – a function that four arguments.
    2. Then, in your ajax call where you have your data values available, you just call doIt() and pass it the desired data values doIt(3, "foo", data4, whatever).
    3. Then, in the implementation of doIt(a, b, c, d), you have available those four data values that were passed to it.

    So, in your external file, you’d have this:

    function doIt(a, b, c, d) {
        // do whatever doIt wants to do
        // use the arguments passed to this function
    }
    

    In your main index.html file, you’d have an ajax call:

    $.ajax(..., function(data) {
        // process the returned data from the ajax call
        // call doIt
        doIt(3, "foo", data4, whatever);
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following case: All boats have a boat type like shark ,
I have a really interesting case: I have a site where this is already
I have a regular .NET application. In this case I have a part that's
I'm new to codeigniter this is my case: I have a homepage with a
I have a case where I check if a variable is undefined. in some
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have an external log file which name changes each session, with the format
I am trying to simply set a variable and have it passed back to
I have this function, and I want to pass a variable (z) to the
I have a string passed into a function, I would like to compare the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.