Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have this JavaScript code snippet. The webservice I am using is a GoogleAPI.

JavaScript
<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);
        
	}		


As the comment mentions function passVariable() {} used to pass the strStartDate & strEndDate as the googleURLs' strStartDate & strEndDate.

I use the
JavaScript
alert(googleURL);
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
Posted

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>

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>
 
Share this answer
 
v5
Comments
Chiranthaka Sampath 16-Jul-14 2:20am    
Both these code snippets are not working. I cannot understand why & also I have the same thing as at the below. But it given me the same result. What is the problem with this?

var googleURL = 'https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions='+'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-date='+StartDate+'&end-date='+EndDate+'&max-results=10';

var strStartDate;
var strEndDate;

//This is the function that alert the GoogleURL.

function testVal(){

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();

}
Peter Leow 16-Jul-14 2:35am    
You have typed the wrong variable names, e.g. Should be strStartDate not StartDate, in the googleURL.
Chiranthaka Sampath 16-Jul-14 2:51am    
Ok now the problem is solved. But the dates are not passing to the URL as it should be. The strStartDate & strEndDate at the googleURL variable hasn't been replaced with the var strStartDate & var strEndDate values. So what do you think about that?
Chiranthaka Sampath 16-Jul-14 6:17am    
The problem is now solved & the answer is residing in the http://www.codeproject.com/Questions/797352/Replacing-Certain-Text-Parts-Using-JavaScript?arn=0 questions last solution. Thank you very much Ashok.
XML
<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>
 
Share this answer
 
v3
Comments
Chiranthaka Sampath 16-Jul-14 0:54am    
No pal it doesn't work that way. So what else I have to do?
ArunRajendra 16-Jul-14 1:37am    
Try the modified solution.
Chiranthaka Sampath 16-Jul-14 3:07am    
Ok now the problem is solved. But the dates are not passing to the URL as it should be. The strStartDate & strEndDate at the googleURL variable hasn't been replaced with the var strStartDate & var strEndDate values. So what do you think about that?
ArunRajendra 16-Jul-14 5:00am    
Let assume this done on button click then add this lines into event.

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';

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900