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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:03:07+00:00 2026-06-13T19:03:07+00:00

im trying to load a external js (json) file (PhoneGap app) whose structure is

  • 0

im trying to load a external js (json) file (PhoneGap app) whose structure is like

   var localString ={
  "tag1": "Username",
  "tag2": "Password",
  "submit": "Submit"
   }

and using the below code to load it at runtime, the newlocale variable holds the name of the file to be loaded for eg: if locale is english-USA then var resourcePath = en-US.js. The issue is the first time i run this code i get this error “ReferenceError: localstring is not defined” , but it loads the external strings the second time i load it. In between i am calling the external file using “select” tag in html5. Can someone provide some insights on where im going wrong or any pointers to overcome this issue.

var newlocale = window.DeviceCulture.get();
local(newlocale);

function local(lang) {
try {
    var resourcePath = lang + '.js';
    var scriptEl = document.createElement('script');
    scriptEl.type = 'text/javascript';
    scriptEl.src = resourcePath;
    alert(resourcePath);
    document.getElementsByTagName("head")[0].appendChild(scriptEl);
    //$('head').append(scriptEl);

    //var localString = window.localString;
    document.getElementById("07").value = localString['submit'];
 } catch (e) {
    errorEvent(e);
}
}
  • 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-13T19:03:09+00:00Added an answer on June 13, 2026 at 7:03 pm

    Okay I believe the root cause of your problem is that you are appending the tag for the .js file into the head after the page is already loaded. When you first load a page the script tages are downloaded and interpreted in order so b can depend on a. However, the way you are doing it is non-blocking so that the script you load is not fully loaded by the time you get to the next line in your code which tries to access “localString”.

    To solve this I’d restructure your code somewhat. First forget about making local files JavaScript. Just make them plain text .json files. For example:

    {
        "tag1": "Username",
        "tag2": "Password",
        "submit": "Submit"
    }
    

    Then I’d load that file using XHR instead of script tag insertion. Something like:

    var newlocale = window.DeviceCulture.get();
    var localString;
    local(newlocale);
    
    function local(lang) {
        try {
            var resourcePath = lang + '.json';
            var request = new XMLHttpRequest();
            request.open("GET", resourcePath, true);
            request.onreadystatechange = function(){
                if (request.readyState == 4) {
                    if (request.status == 200 || request.status == 0) {
                        localString = JSON.parse(request.responseText);
                        // at this point localString is loaded with the new language
                        document.getElementById("07").value = localString['submit'];
                    }
                }
            }
            request.send();
         } catch (e) {
            errorEvent(e);
        }
    }
    

    and that should take care of things.

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

Sidebar

Related Questions

I'm trying to load data from an external .js file, containing a JSON representation
I'm trying to load and parse json data from an external source into a
I'm trying to load an external XML file that contains references to images in
I am trying to load an external swf file. The swf width and height
I'm trying to load data from an external csv file that needs to be
I am trying to load an external site instead of the internal index.html file
I am using CodeIgniter. I am trying to load an external javascript file.The .js
I'm trying to dynamically load an external .js file (according to an input I
So there I am trying to load an external .js file which in return
I'm trying to load a javascript file into an external page. And want a

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.