View Full Version : Working with website content using javascript

09-27-2011, 10:01 PM
I'm sorry I don't have a website to go with my question...it's on a dev server. But it's fairly simple I think.

I have a php script running server side that I'm looping through (using different numbers at the end of the php page to get new content each iteration), and I want to grab the content of the php page to display, inside a google maps infowindow ideally.

The problem is, right now the infowindow displays just the link. I want the content of what the link is (which is just a few words in any case).

Here's the general code where I place the page inside a variable and then try to display the contents of the page in html variable (the link I show here won't work but it gives you the idea):

var hazard_links = "https://ch.noaa.gov/m/googleapi/rec/extracthazards.php?lat="+lat+"&lon="+lon;

var html = '<span style="font-size: 10pt; color: red;"><hr><a href="'+hazard_links+'" target = "_blank">' + hazard_links + '</a>' + '<span style="font-size: 10pt; color: red;">';

Thanks for any help,

09-27-2011, 10:17 PM
As long as the url is on the same server as the page that wants to load its contents it's easy, use some good old AJAX or SJAX:

var getFileContent = function (url){
var r = window.XMLHttpRequest && new XMLHttpRequest();
!r && (r = new ActiveXObject("Microsoft.XMLHTTP"));
r && r.open("GET", url, false)
r && r.send(null);
!r && (r = {responseText:false});
var r = r.responseText;
return r

Otherwise it's more difficult, because AJAX (i.e. XMLHttpRequests) are not cross-domain.

If the website on the other domain provides what is sometimes called JSONP or you are on speaking terms with the webmaster you could load the content as Javascript (since this is allowed cross-domain):

var scriptMount = function(url){
var js = document.createElement("script");

The JSONP standard actual states that you should send the name of the function (has to be a global) that you want called upon response in the url in the parameter jsonp, like:


So in this case the server response would be parseResponse('some data as a string or javascript object')

From a domain that does not and will not support jsonp:
You have to build your own server-side proxy (in PHP use curl) and then you can use AJAX/SJAX as usual against that proxy.