Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-28-2012, 12:39 PM   PM User | #1
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
Javascript API's

Hi,

I am trying to add a google api onto my site. The problem I am having is that google provide the code to add the api inline, however, being the organised man I am I want to run all scripts like css from an external .js file. I have tried to do this with a number of scripts and failed each time. Could anyone suggest how I break this down?

Thanks so much.


<script type="text/javascript">
google.load('visualization', '1', {packages: ['geomap']});

function drawVisualization() {
var data = new google.visualization.DataTable();
data.addRows(6);
data.addColumn('string', 'Country');
data.addColumn('number', 'Popularity');
data.setValue(0, 0, 'Germany');
data.setValue(0, 1, 200);
data.setValue(1, 0, 'United States');
data.setValue(1, 1, 300);
data.setValue(2, 0, 'Brazil');
data.setValue(2, 1, 400);
data.setValue(3, 0, 'Canada');
data.setValue(3, 1, 500);
data.setValue(4, 0, 'France');
data.setValue(4, 1, 600);
data.setValue(5, 0, 'RU');
data.setValue(5, 1, 700);

var geomap = new google.visualization.GeoMap(
document.getElementById('visualization'));
geomap.draw(data, null);
}


google.setOnLoadCallback(drawVisualization);
</script>
vision is offline   Reply With Quote
Old 02-28-2012, 03:47 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You must not include any HTML tags <script type = "text/javascript"> </script> in your external Javascript file. Is that the problem?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-28-2012, 04:38 PM   PM User | #3
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
The code above is the exact inline code from google but when I add just the javascript code below into a .js file nothing happens? I have added a basic html code below to show how I am using everything. I have tried with lots of different API's but I am breaking the page every time I add it into an external page.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Google Visualization</title>
<script src="_js/script.js"></script>
</head>
<body >
<div id="visualization" style="width: 800px; height: 400px;"></div>
</body>
</html>

Javascript Code added exactly as below.

google.load('visualization', '1', {packages: ['geomap']});

function drawVisualization() {
var data = new google.visualization.DataTable();
data.addRows(6);
data.addColumn('string', 'Country');
data.addColumn('number', 'Popularity');
data.setValue(0, 0, 'Germany');
data.setValue(0, 1, 200);
data.setValue(1, 0, 'United States');
data.setValue(1, 1, 300);
data.setValue(2, 0, 'Brazil');
data.setValue(2, 1, 400);
data.setValue(3, 0, 'Canada');
data.setValue(3, 1, 500);
data.setValue(4, 0, 'France');
data.setValue(4, 1, 600);
data.setValue(5, 0, 'RU');
data.setValue(5, 1, 700);

var geomap = new google.visualization.GeoMap(
document.getElementById('visualization'));
geomap.draw(data, null);
}


google.setOnLoadCallback(drawVisualization);
vision is offline   Reply With Quote
Old 02-28-2012, 05:03 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,554
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
is your script tag at the bottom of your <body> tag? (it should be)
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is online now   Reply With Quote
Old 02-28-2012, 06:23 PM   PM User | #5
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
I thought all tags needed to be in the header even when linking to external sheets?

The code in my last post shows my HTML page and my Script page.
vision is offline   Reply With Quote
Old 02-28-2012, 08:13 PM   PM User | #6
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,554
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by vision View Post
I thought all tags needed to be in the header even when linking to external sheets?

The code in my last post shows my HTML page and my Script page.
no, <script> tags can go anywhere, and it's better UX to have external scripts at the bottom so that they don't prevent page rendering while the script is fetched.

there's not enough code shown to make this work. it seems you are missing some code, probably a single file to be included on every page that uses the google map stuff.

google.load() is not a standard JavaScript command, so you need to define it using the aforementioned script.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is online now   Reply With Quote
Old 02-28-2012, 09:16 PM   PM User | #7
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
I see, that makes sense.

Okay, got that bit but I am still have no idea how I take this script and put it externally? If someone could explain then I can see what has to be in place and then apply it to any inline code.

I understand that API's work using external websites for their data and libraries but I can't see how you link to an external script webpage and then to an external site for the data?

Here is the whole code from google:

<script type="text/javascript">
google.load('visualization', '1', {packages: ['geomap']});

function drawVisualization() {
var data = new google.visualization.DataTable();
data.addRows(6);
data.addColumn('string', 'Country');
data.addColumn('number', 'Popularity');
data.setValue(0, 0, 'Germany');
data.setValue(0, 1, 200);
data.setValue(1, 0, 'United States');
data.setValue(1, 1, 300);
data.setValue(2, 0, 'Brazil');
data.setValue(2, 1, 400);
data.setValue(3, 0, 'Canada');
data.setValue(3, 1, 500);
data.setValue(4, 0, 'France');
data.setValue(4, 1, 600);
data.setValue(5, 0, 'RU');
data.setValue(5, 1, 700);

var geomap = new google.visualization.GeoMap(
document.getElementById('visualization'));
geomap.draw(data, null);
}


google.setOnLoadCallback(drawVisualization);
</script>
vision is offline   Reply With Quote
Old 02-29-2012, 02:34 AM   PM User | #8
blaze4218
Regular Coder

 
Join Date: Apr 2005
Location: Texas
Posts: 448
Thanks: 24
Thanked 63 Times in 63 Posts
blaze4218 is an unknown quantity at this point
Quote:
Okay, got that bit but I am still have no idea how I take this script and put it externally? If someone could explain then I can see what has to be in place and then apply it to any inline code.

I understand that API's work using external websites for their data and libraries but I can't see how you link to an external script webpage and then to an external site for the data?
I think that was what rnd me was telling you just that. You must include additional javascript that enables the google.load() method. I found <script type="text/javascript" src="https://www.google.com/jsapi"></script>
at https://developers.google.com/loader/
along with detailed documentation on how to use it. I would assume you have the following in your HTML document
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Google Visualization</title>
</head>
<body >
<div id="visualization" style="width: 800px; height: 400px;"></div>
</body>
 <script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <script src="_js/script.js"></script> 
</html>
and then you would create the file "script.js" in the "_js" directory. Open the file "script.js" and past the code
Code:
 google.load('visualization', '1', {packages: ['geomap']});

function drawVisualization() {
var data = new google.visualization.DataTable();
data.addRows(6);
data.addColumn('string', 'Country');
data.addColumn('number', 'Popularity');
data.setValue(0, 0, 'Germany');
data.setValue(0, 1, 200);
data.setValue(1, 0, 'United States');
data.setValue(1, 1, 300);
data.setValue(2, 0, 'Brazil');
data.setValue(2, 1, 400);
data.setValue(3, 0, 'Canada');
data.setValue(3, 1, 500);
data.setValue(4, 0, 'France');
data.setValue(4, 1, 600);
data.setValue(5, 0, 'RU');
data.setValue(5, 1, 700);

var geomap = new google.visualization.GeoMap(
document.getElementById('visualization'));
geomap.draw(data, null);
}


google.setOnLoadCallback(drawVisualization);
Save everything and load your HTML page...

when you paste <script type="text/javascript" src="https://www.google.com/jsapi"></script> in your page you are loading all the extra javascript that makes the api work into your page. now the api is on your website, so all you have to do is call the available methods like so google.load('visualization', '1', {packages: ['geomap']});
__________________
Allwisend bin ich nicht, doch viel ist mir bewursst
-Goethe
blaze4218 is offline   Reply With Quote
Users who have thanked blaze4218 for this post:
vision (03-02-2012)
Old 02-29-2012, 11:22 AM   PM User | #9
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
I have tried that but nothing happens. I assume that the functions aren't being called using that HTML above? I have tried adding onload="visualisation()" in the body tag to call the function at the top of the script but again nothing is happening.
vision is offline   Reply With Quote
Old 03-02-2012, 11:09 AM   PM User | #10
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
Thanks for all of the suggestions so far but I am yet to resolve this and it would be a great help if we could. thanks.
vision is offline   Reply With Quote
Old 03-02-2012, 11:26 AM   PM User | #11
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
As soon as you don't give us a test URL ... you should start debugging. There are some neat tools out there (including some built-in developer tools for IE, Opera or Chrome) that will certainly help you in detecting issues with your page.
devnull69 is offline   Reply With Quote
Old 03-02-2012, 01:32 PM   PM User | #12
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
what do you mean by a test URL?
vision is offline   Reply With Quote
Old 03-02-2012, 01:41 PM   PM User | #13
vision
New to the CF scene

 
Join Date: Feb 2012
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
vision is an unknown quantity at this point
I have been trying a million combinations and I have finally got this to work. Thanks for all of the help given.
vision is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:32 AM.


Advertisement
Log in to turn off these ads.