Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Working with website content using javascript

    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):

    Code:
    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,

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    1.
    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:

    Code:
    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.

    2.
    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):

    Code:
    var scriptMount = function(url){
      var js = document.createElement("script");
      js.setAttribute("src",url);
      document.getElementsByTagName("head").item(0).appendChild(js);
    };
    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:

    www.whateverdomain.com/whateverpath?jsonp=parseResponse


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

    3.
    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.
    Last edited by ironboy; 09-27-2011 at 10:22 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •