摆脱document.write?

时间:2010-12-04 22:11:44

标签: javascript jquery

我有一个功能:

  function getScript(src) {
    document.write('<' + 'script src="' + src + '"' +
                   ' type="text/javascript"><' + '/script>');
  }

它被召唤一次:

  getScript("http://maps.gstatic.com/intl/sv_se/mapfiles/api-3/3/1b/main.js");
})();

我需要摆脱document.write,因为它在firefox中的xhtml中不受支持。我该怎么做呢? Jquery很好:))

完整代码:

window.google = window.google || {};
google.maps = google.maps || {};
(function() {

  function getScript(src) {
    document.write('<' + 'script src="' + src + '"' +
                   ' type="text/javascript"><' + '/script>');
  }

  var modules = google.maps.modules = {};
  google.maps.__gjsload__ = function(name, text) {
    modules[name] = text;
  };

  google.maps.Load = function(apiLoad) {
    delete google.maps.Load;
    apiLoad([null,[[["http://mt0.google.com/vt?lyrs=m@139\u0026src=api\u0026hl=sv-SE\u0026","http://mt1.google.com/vt?lyrs=m@139\u0026src=api\u0026hl=sv-SE\u0026"]],[["http://khm0.google.com/kh?v=74\u0026hl=sv-SE\u0026","http://khm1.google.com/kh?v=74\u0026hl=sv-SE\u0026"],null,null,null,1],[["http://mt0.google.com/vt?lyrs=h@139\u0026src=api\u0026hl=sv-SE\u0026","http://mt1.google.com/vt?lyrs=h@139\u0026src=api\u0026hl=sv-SE\u0026"],null,null,"imgtp=png32\u0026"],[["http://mt0.google.com/vt?lyrs=t@126,r@139\u0026src=api\u0026hl=sv-SE\u0026","http://mt1.google.com/vt?lyrs=t@126,r@139\u0026src=api\u0026hl=sv-SE\u0026"]],null,[[null,0,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1.12\u0026hl=sv-SE\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1.12\u0026hl=sv-SE\u0026"]],[null,0,8,9,[[[330000000,1246050000],[386200000,1279600000]],[[345000000,1279600000],[386200000,1286700000]],[[348900000,1286700000],[386200000,1293600000]],[[354690000,1293600000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1.12\u0026hl=sv-SE\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1.12\u0026hl=sv-SE\u0026"]],[null,0,10,19,[[[329890840,1246055600],[386930130,1284960940]],[[344646740,1284960940],[386930130,1288476560]],[[350277470,1288476560],[386930130,1310531620]],[[370277730,1310531620],[386930130,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1.12\u0026hl=sv-SE\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1.12\u0026hl=sv-SE\u0026"]],[null,3,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=sv-SE\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=sv-SE\u0026"]],[null,3,8,9,[[[330000000,1246050000],[386200000,1279600000]],[[345000000,1279600000],[386200000,1286700000]],[[348900000,1286700000],[386200000,1293600000]],[[354690000,1293600000],[386200000,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=sv-SE\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=sv-SE\u0026"]],[null,3,10,null,[[[329890840,1246055600],[386930130,1284960940]],[[344646740,1284960940],[386930130,1288476560]],[[350277470,1288476560],[386930130,1310531620]],[[370277730,1310531620],[386930130,1320034790]]],["http://mt0.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=sv-SE\u0026","http://mt1.gmaptiles.co.kr/mt?v=kr1p.12\u0026hl=sv-SE\u0026"]]],[["http://cbk0.google.com/cbk?","http://cbk1.google.com/cbk?"]],[["http://khmdb0.google.com/kh?v=33\u0026hl=sv-SE\u0026","http://khmdb1.google.com/kh?v=33\u0026hl=sv-SE\u0026"]],[["http://mt0.google.com/mapslt?hl=sv-SE\u0026","http://mt1.google.com/mapslt?hl=sv-SE\u0026"]],[["http://mt0.google.com/mapslt/ft?hl=sv-SE\u0026","http://mt1.google.com/mapslt/ft?hl=sv-SE\u0026"]]],["sv-SE","US",null,0,null,"http://maps.google.com","http://maps.gstatic.com/intl/sv_se/mapfiles/","http://gg.google.com","https://maps.googleapis.com","http://maps.googleapis.com"],["http://maps.gstatic.com/intl/sv_se/mapfiles/api-3/3/1b","3.3.1b"],[4241463462],1,null,null,null,null,0,""], loadScriptTime);
  };
  var loadScriptTime = (new Date).getTime();
  getScript("http://maps.gstatic.com/intl/sv_se/mapfiles/api-3/3/1b/main.js");
})();

4 个答案:

答案 0 :(得分:8)

jQuery核心中已存在$.getScript()

$.getScript("http://maps.gstatic.com/intl/sv_se/mapfiles/api-3/3/1b/main.js");

它的行为方式类似,创建<script>元素并将其附加到<head> ...但不包含document.write()

答案 1 :(得分:3)

你为什么不把它放在文件里?

<script src="http://maps.gstatic.com/intl/sv_se/mapfiles/api-3/3/1b/main.js">
</script>

如果要求您在完成其余操作后加载脚本,请将两个脚本标记放在正文的末尾(googlemaps脚本作为第二个)

答案 2 :(得分:1)

你可以说:

var tag = $('<script src="' + src + '" type="text/javascript"></script>');
$("head").append(tag);

答案 3 :(得分:1)

document.documentElement.appendChild(document.createElement("script")).src = "http://maps.gstatic.com/intl/sv_se/mapfiles/api-3/3/1b/main.js";