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 9218419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:51:36+00:00 2026-06-18T02:51:36+00:00

I have two separate .js files and I want to assign the value of

  • 0

I have two separate .js files and I want to assign the value of a global variable called citId inside an ajax call in my first .js file and then use that variable in my second .js file.

The code is too long to post, so I’ve put an example of what I’m trying to do.

My first .js file is main.js:

var citId = "";
$(document).ready(function($) {
var url = some url;

$.ajax({
    type: 'GET',
    url: url,
    jsonpCallback: 'getSBJSON',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
                  //Does some stuff

                   urlId = product of some stuff;

        $.ajax({
            type: 'GET',
            url: url + urlId,
            jsonpCallback: 'getSBJSON',
            contentType: "application/json",
            dataType: 'jsonp',
            success: function(json) {

                if (json.length > 0) {
                    citId = urlId;
                    window.open('Citations.html', '_self');
                } else {
                var page = linkBase + urlId;
                window.open(page);
                }  
            },
            error: function(e) {
                console.log(e.message);
            }   
         });  

    });   
    },
    error: function(e) {
        console.log(e.message);
    }
});
});

and my second .js file is citations.js:

$(document).ready(function($) {

    var itemLnkId = citId;
    $.ajax({
        type: 'GET',
        url: url + citId,
        jsonpCallback: 'getSBJSON',
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(json)
        // more stuff

    });
});

I’ve read a lot of answers that either show how to assign the global variable inside the ajax call or how to use a global variable across .js files but nothing that shows how to do both combined.

When I’ve simply used a global variable, it works for the original value but does not show the value being changed inside the ajax call. Is there some way to do both assigning a value and then using it in a different .js file?

  • 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-18T02:51:37+00:00Added an answer on June 18, 2026 at 2:51 am

    Instead of using a global variable why don’t you pass the variable that you need using invokedata of the ajax call?

    to use it in the success call.

    Example http://weboutofthebox.com/en-GB/28/Article/Ajaxinvokedataparameter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays that I want to print to separate files. Here's my
I have two factories ( post_factory.rb , comment_factory.rb ) in separate files. I'd like
I have two separate files that one is server-side JS. The other one is
I have two separate Excel files containing an old and a new electrical plant
I have been looking all over for this. I have two seperate prpt files
I have two text files with various columns, each column is separated by a
We have two separate web-apps, say 'retailUI' and 'bulkUI'. These two are basically two
I have two separate applications (both part of the same system) that share a
I have two separate caches running in a JVM (one controlled by a third
I have two separate sets of tables in the same database that model 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.