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.
-
pass it when you call it? unclear what you really want.epascarello– epascarello2018年03月20日 14:02:07 +00:00Commented Mar 20, 2018 at 14:02
-
2I see two functions with no calls to either. Please edit your question to include a minimal reproducible examplej08691– j086912018年03月20日 14:02:40 +00:00Commented Mar 20, 2018 at 14:02
-
Possible duplicate of Javascript passing parameters to functionmesserbill– messerbill2018年03月20日 14:03:18 +00:00Commented 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?khollenbeck– khollenbeck2018年03月20日 14:08:38 +00:00Commented 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 functionOvertime– Overtime2018年03月20日 14:11:32 +00:00Commented Mar 20, 2018 at 14:11
1 Answer 1
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;
}
answered Mar 20, 2018 at 14:03
Ele
33.8k7 gold badges43 silver badges80 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Scott Sauyet
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.Overtime
@Ele there are other functions that may need to fill 'center' with other coordinates, such as a function named loadCardiff in the external file
Ele
Please add more code to understand the whole context.
lang-js