简体   繁体   中英

Load Javascript on page inside Div using jQuery load()

I have this page that uses Google Maps Javascript API (extPage.php):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

<script>


function initialize() {

    var cord = new google.maps.LatLng(28.545078,-81.377196);

    var mapOptions = {
        zoom: 15,
        center: cord,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);

}

google.maps.event.addDomListener(window, 'load', initialize);


</script>

</head>

<body>

<div id="map-canvas"style="width:600px; height:500px"></div>

</body>

</html>

That page loads fine as is but I am trying to load into a div on another page. I am doing it like this (test.php):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

<script type="text/javascript">


    $(document).ready(function() {

        $("#LoadBut").click(function() {

            $("#extDiv").load('extPage.php');

        });

    });


</script>

</head>


<body>

<input type="button" id="LoadBut" value="Load">

<div id="extDiv"></div>


</body>

</html>

When I run it just like this I get an error ReferenceError: google is not defined . I've tried moving the <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> to the test.php page. The error message goes away but the map does not load.

I've never been to sure about the best way to handle Javascript on a page inside of a div like this.

Any help with this would be great.

Thanks!

From what I understand of your requirements, you can use the Google Static Maps API , whcih does not require any JavaScript.

You just place an img tag with a reference to a static map url into your page with the appropriate parameters. For your example:

<div id="extDiv">
    <img src="https://maps.googleapis.com/maps/api/staticmap?center=28.545078,-81.377196&zoom=15&size=600x500&maptype=roadmap&sensor=false" />
</div>

See the note at the top of the documentation to see if the inclusion of API keys are applicable to your project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM