1

I have a button on my website that when clicked generates a word. This word is then used in a url call to download a specific file.

 <button class="button1" onclick="myFunction()">cafe</button>
 var cafe = [];
 function myFunction() {
 cafe.push('cafenew');

The 2nd piece of code below takes the word 'cafenew' that was pushed into the var cafe (when the button was clicked) and inserts it into the url address:

 name=cafe

 var src = "https://www.dropbox.com/s/"+name+".kml?dl=1";

But for some reason this does not work. The file being loaded is a layer onto a web map. I have checked the url address, everything is correct. I tested this by using:

 var cafe = "cafenew" 

and the layer loaded correctly, but I obviously loose the function of the button if I fix what the variable equals.

Thanks in advance for any help!

5
  • name is not defined, and why are you pushing to an array? Seems like you want a simple string. Commented Apr 16, 2016 at 14:48
  • You should provide the code where you give name its value based on cafe. Commented Apr 16, 2016 at 14:48
  • why are you reposting this question? Commented Apr 16, 2016 at 14:49
  • I have cut the previous question down and reshaped it, my previous question was too confusing Commented Apr 16, 2016 at 14:53
  • Sorry, name = cafe I have edited the code Commented Apr 16, 2016 at 14:55

1 Answer 1

2

Try this it's working fine :

Html :

<button class="button1" onclick="myFunction();">cafe</button>

Script :

 var cafe = [];
 window.myFunction = function() {
 cafe.push('cafenew');
 console.log(cafe);
 var name = cafe[0];
 var src = "https://www.dropbox.com/s/"+name+".kml?dl=1";
 console.log(src);
 }

Working fiddle : https://jsfiddle.net/8etxu8pr/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, the console log shows this is now creating the correct url. However for some reason, the file is not loaded. The url shown on the console log is correct and when clicked the file begins to download, but this should automatically be opened on the website. Any ideas? What does the 0 do in var name = cafe[0]; ?
'0' is index .. as you are pushing text 'cafenew' into the array 'cafe' . So for access the text from the array I used 'cafe[0]'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.