-2

I have to functions, one is within an external js file named "london.js" and one is within a HTML script.

I wish to take a value from the external file and insert it into my HTML script function.

   function initMap(cityLatLng) {
      map = new google.maps.Map(document.getElementById('map'), {
      zoom: 12,
      center: ldn,
}

  function loadLondon() {
      var ldn = {lat: 51.5, lng: -0.1}
}

I am not sure how to pass values through functions, I have tried putting the variable outside the function but it didn't work. I wish to make use of the var ldn in initMap.

Thanks in advance.

6
  • pass it when you call it? unclear what you really want. Commented Mar 20, 2018 at 14:02
  • 2
    I see two functions with no calls to either. Please edit your question to include a minimal reproducible example Commented Mar 20, 2018 at 14:02
  • Possible duplicate of Javascript passing parameters to function Commented Mar 20, 2018 at 14:03
  • Which one is your external? And which one is your embedded script? Do you have control over the external script? Are you able to make changes to it? Commented Mar 20, 2018 at 14:08
  • @KrisHollenbeck initMap is internal and loadLondon is external, I am able to make changes to both, I just wish to make use of that variable within the other function Commented Mar 20, 2018 at 14:11

1 Answer 1

2

An alternative is to return that variable ldn and assign it to the property center:

function initMap(cityLatLng) {
  map = new google.maps.Map(document.getElementById('map'), {
  zoom: 12,
  center: loadLondon(),
}

function loadLondon() {
  var ldn = {lat: 51.5, lng: -0.1};

  // your logic with ldn;

  return ldn;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Although this sounds good in theory, the word load in the function makes me wonder if it's being loaded asynchronously, in which case, there is a lot more work to do.
@Ele there are other functions that may need to fill 'center' with other coordinates, such as a function named loadCardiff in the external file
Please add more code to understand the whole context.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.