使用JavaScript中的变量创建通过按钮的网址 [英] Javascript using variables to create a url through buttons
问题描述
我在我的网站上点击该按钮时产生一个字。这个词,然后在一个URL呼叫用于下载一个具体的文件。
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');
第二张code以下需要被推入VAR网吧词cafenew(当按钮被点击)并将其插入到URL地址:
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";
但出于某种原因,这是行不通的。加载该文件是一个层到网络地图。我检查了URL地址,一切是正确的。我用这个测试:
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!
推荐答案
试试这个它的正常工作:
HTML:
<button class="button1" onclick="myFunction();">cafe</button>
脚本:
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);
}
工作小提琴: https://jsfiddle.net/8etxu8pr/
这篇关于使用JavaScript中的变量创建通过按钮的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!