My site:
- http://student.athabascau.ca/~coreyhe/homepage.html
- Vistor inputs location and date
- js file gets coordinates from geobytes.com and date from jquery-ui datepicker.
- js file passes decimal coordinates and date to PHP file.
- The PHP file queries tables and return activities and locations nearby
- In the "Dark Sky Preserves" output section I have coded a button in the PHP file that has the coordinates as content
The Button:
- When clicked I have the coordinates copied to computer clipboard, then a new tab opens to lightpollutionmap.info.
- Once that website has loaded, the user is to paste to the input field and the site zooms to coordinates.
- Purpose is to make it less tedious for user to find out light pollution in their area.
- I used the onclick=gotoLPM()for invoking.
- The gotoLPM()script is placed inside the<head>section.
- I have utilized the jquery.copy-to-clipboard.jsplug-in and it's in the<head>section as well.
The Problem:
- I would like to invoke this button via external jquery file - to keep js separate.
- Have tried putting the <script src=>in both<head>and<body>sections, but script would not execute.
- The script should execute after the PHP file has output/rendered the button into its designated <p>element with rest of MySQL queries.
- In short I would like to use a DOM event handler, not HTML like I have done.
- I am thinking I may have to dynamically append a button with jquery after PHP output
- Any suggestions?
- Note: lightpollutionmap.info may be slow....
The PHP code:
echo "<h1>Dark Sky Preserves</h1>";
if (mysqli_num_rows($resultDarksky) > 0) {
    // Output of MySQL rows 
    while($row = mysqli_fetch_assoc($resultDarksky)) {
        echo $row["name"]. "<br>";
        echo "GPS coordinates: " . $row["latitude"] . ", " . $row["longitude"] . "<br/>";
        echo "Hectacres: " . $row["area"] . str_repeat(' ', 5) . " Bortle scale: " . $row["bortle"] ."<br/>";
        echo "<a href='".$row["link"]."' target='_blank'>". $row["link"] ."</a><br/>";
        echo $row["comment"]. "<br/>";
        echo "<br/>";
    // BUTTON for lightpollutionmap.info
    }
        echo "<br/>";
        echo "Check out the light pollution at your location. Click on your coordinates below and ";
        echo "then paste into the top-left input field in lightpollutionmap.info<br/>";
        echo "<button id='lightPollutionMapButton' onclick='gotoLPM()'>" .$q. "," .$r. "</button>";
        echo "<br/>";
}   // if no records match query - print 0 results
    // BUTTON for lightpollutionmap.info
    else {
        echo "No designated dark sky preserve nearby, but click on your coordinates below and ";
        echo "then paste into the top-left input field in ";
        echo "lightpollutionmap.info website for dark skies near you:<br/>";
        echo "<button id='lightPollutionMapButton' onclick='gotoLPM()'>" .$q. "," .$r. "</button>";
}
The script file (inside HTML <head> section):
function gotoLPM(){
    $("#lightPollutionMapButton").CopyToClipboard();
    window.open("https://www.lightpollutionmap.info", "_blank");
  };
 
    