Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a JavaScript Function in an external JavaScript page. The function is at the below.

Script.js
JavaScript
function getVaria(){

strStartDate = document.getElementById("from_date").value;
strEndDate = document.getElementById("to_date").value;

//Following variable will be the query to the 1st chart.
strWsUrl = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=ga%3Asource&metrics=ga%3Ausers&filters=ga%3Asource!%3D(direct)&sort=-ga%3Ausers&start-date=' + strStartDate + '&end-date=' + strEndDate + '&max-results=10";

alert(strStartDate);
alert(strEndDate);
alert(strWsUrl);

return strWsUrl;

}


This function shaould 'return strWsUrl'. To grab that value I have created another function outside of that perticular external JavaScript page. It is at below.

Dashboard.html

HTML
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript">
	//Date Picker
	$(function() {
		$("#to_date").datepicker({  maxDate: new Date() });
		$("#from_date").datepicker({  maxDate: new Date() });
		$("div.ui-datepicker").css( { "font-size": "10px" } );
	});
</script>

<script type="text/javascript">

	function myTest(){
	
		getVaria();
		alert("This is UI Page " + strWsUrl);
}


However the result is This is UI Page Undefined. I think this is because the strWsUrl value does not pass to the HTML page. So What have I done wrong?

Could someone enlight me to solve the matter?
Chiranthaka
Posted

1 solution

Your code is full of the worst part of JavaScript.
1. All your variables are globals - as no var used to declare them. A huge mistake!!!
2. If you write a function to return a value then use that value!!!
---
JavaScript
function myTest(){
        alert("This is UI Page " + getVaria());
}

..and...
JavaScript
function getVaria(){
  var strStartDate = document.getElementById("from_date").value;
  var strEndDate = document.getElementById("to_date").value;

  //Following variable will be the query to the 1st chart.
  var strWsUrl = "https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A76546294&dimensions=ga%3Asource&metrics=ga%3Ausers&filters=ga%3Asource!%3D(direct)&sort=-ga%3Ausers&start-date=' + strStartDate + '&end-date=' + strEndDate + '&max-results=10";

  alert(strStartDate);
  alert(strEndDate);
  alert(strWsUrl);

  return strWsUrl;
}

---
Do yourself a favor! Stop for a week and read some decent book about programming with JavaScript. Your development can go so wrong with JavaScript (it's a language permitting any kind of idiotic things) if you do not understand its base...
 
Share this answer
 
v2
Comments
Chiranthaka Sampath 21-Jul-14 23:18pm    
Ok pal this works like a charm. Thanks a lot!
Kornfeld Eliyahu Peter 22-Jul-14 1:45am    
I know. But you have to understand - you HAVE to learn if you ever want to write code JavaScript or not!!!
Chiranthaka Sampath 22-Jul-14 1:52am    
OK I will Thanx for the advice pal.

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