jQuery load()一个包含JavaScript的html文件 [英] Jquery load() a html file which contains JavaScript

查看:90
本文介绍了jQuery load()一个包含JavaScript的html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大难题.我想加载一个.html文件,其中包含javascript(google maps)代码以在其中呈现div.

I have a big dilema. I want to load a .html file which contains javascript(google maps) code to render the div inside it.

maps.html看起来像这样:

maps.html look like this :

    <script type="text/javascript">
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}
</script>

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
    //<![CDATA[




var hash = getUrlVars();

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(hash['lat'],hash['lng']),
        zoom: 14,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;


      downloadUrl("xmlout_carol.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length+1; i++) {
          var name = markers[i].getAttribute("nume");
          var address = markers[i].getAttribute("adresa");
          var type = markers[i].getAttribute("id");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<font face='Tahoma' style='font-size:12px;'><div style='min-width:230px;'><b>" + name + "</b> <br/>" + address +"<a target='_top'  href='../statii.php?id=" + type + "'><img style='float:right; border:0px; margin-left: 40px;'  src='go.png' /></a><div/></font>";
          var tip = markers[i].getAttribute("tip");
          var icon = customIcons[tip] || {};

          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
           // shadow: 'shaddow.png'
          //shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'mouseover', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
      google.maps.event.addListener(map, 'click', function() {
        infoWindow.close(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

    //]]>

  </script>

  <body onload="load()">
    <div id="map" style="width: 900px; height: 500px"></div>
  </body>

此脚本将地图呈现到div.map

我想做的就是将此.html加载到另一个.php文件中包含的div中,如下所示:

What i want to do is to load this .html into a div that is contained in another .php file like this :

$("div#insert_here").load("maps.html?lat=xxx&long=yyy");

它输出maps.html中包含的div,但没有map no java.

It output the div contained in maps.html but with no map no java.

所以问题是...如果.html文件已经包含将数据输出到.html文件中的div的javascript,如何在另一个.php文件中使用jquery加载.html文件? ??

So the question is... How do I load a .html file using jquery in another .php file if the .html file already contains javascripts to output data to the div in .html file ???

非常感谢!

推荐答案

正如其他人所说,特别地加载JS或执行eval()函数;).

As the others said, load JS particularly, or do the eval() function ;).

这将解析JS并使其可以在最初执行.

That parses the JS and makes it possible to be executed initially.

这篇关于jQuery load()一个包含JavaScript的html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆