JavaScript函数用于将变量传递给URL。 [英] JavaScript Function Use To Pass Variables to a URL.
问题描述
我有这个JavaScript代码段。我使用的网络服务是GoogleAPI。
I have this JavaScript code snippet. The webservice I am using is a GoogleAPI.
<script type="text/javascript" >
var googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date='+strStartDate+'&end-date='+strEndDate+'&max-results=10';
//The following JS function will pass the values from the form to the URL.
function passVariable() {
var strStartDate = document.getElementById("from_date").value;
var strEndDate = document.getElementById("to_date").value;
alert(googleURL);
}
由于评论提到函数passVariable(){}用于传递strStartDate& strEndDate作为googleURLs的strStartDate& strEndDate。
我使用
As the comment mentions function passVariable() {} used to pass the strStartDate & strEndDate as the googleURLs' strStartDate & strEndDate.
I use the
alert(googleURL);
显示网址,输出结果为未定义 。我无法理解我该怎么做。有人可以帮我解决这个问题吗?
谢谢&问候,
Chiranthaka
to display the URL and the output was "undefined". I cannot understand what should I do. Could someone please help me to solve this matter?
Thanks & regards,
Chiranthaka
推荐答案
<script>
var strStartDate;
var strEndDate;
function SetVariable() {
strStartDate = document.getElementById("from_date").value;
strEndDate = document.getElementById("to_date").value;
}
SetVariable();
var googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date='+strStartDate
+'&end-date='+strEndDate+'&max-results=10';
alert( googleURL);
</script>
试试这个:
Try this:
<script>
function passVariable() {
var strStartDate = document.getElementById("from_date").value;
var strEndDate = document.getElementById("to_date").value;
var googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date='+strStartDate+'&end-date='+strEndDate+'&max-results=10';
alert(googleURL);
}
</script>
阅读本文以了解 JavaScript中的范围 [ ^ ]。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +
你输入了错误的名字,应该是strStartDate而不是startDate,试试这个:
Read this to understand the Scope in JavaScript[^].
++++++++[Round 2]+++++++++
You have typed the wrrong names, should be strStartDate not startDate, Try this:
<script>
var strStartDate;
var strEndDate;
//This is the function that alert the GoogleURL.
function testVal(){
var googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date='+strStartDate+'&end-date='+strEndDate+'&max-results=10';
alert(googleURL);
}
//The following JS function will pass the values from the form to the URL.
function passVariable() {
strStartDate = document.getElementById("from_date").value;
strEndDate = document.getElementById("to_date").value;
alert(strStartDate);
alert(strEndDate);
testVal();
}
</script>
这篇关于JavaScript函数用于将变量传递给URL。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!