最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting rid of document.write? - Stack Overflow

programmeradmin4浏览0评论

I have a function:

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

And it's called once:

  getScript(".js");
})();

I need to get rid of the document.write, because it's not supported in xhtml in firefox. How do I do this? Jquery is fine :)

Full code:

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,[[["@139\u0026src=api\u0026hl=sv-SE\u0026","@139\u0026src=api\u0026hl=sv-SE\u0026"]],[["\u0026hl=sv-SE\u0026","\u0026hl=sv-SE\u0026"],null,null,null,1],[["@139\u0026src=api\u0026hl=sv-SE\u0026","@139\u0026src=api\u0026hl=sv-SE\u0026"],null,null,"imgtp=png32\u0026"],[["@126,r@139\u0026src=api\u0026hl=sv-SE\u0026","@126,r@139\u0026src=api\u0026hl=sv-SE\u0026"]],null,[[null,0,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1320034790]]],[".12\u0026hl=sv-SE\u0026",".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]]],[".12\u0026hl=sv-SE\u0026",".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]]],[".12\u0026hl=sv-SE\u0026",".12\u0026hl=sv-SE\u0026"]],[null,3,7,7,[[[330000000,1246050000],[386200000,1293600000]],[[366500000,1297000000],[386200000,1320034790]]],[".12\u0026hl=sv-SE\u0026",".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]]],[".12\u0026hl=sv-SE\u0026",".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]]],[".12\u0026hl=sv-SE\u0026",".12\u0026hl=sv-SE\u0026"]]],[["?","?"]],[["\u0026hl=sv-SE\u0026","\u0026hl=sv-SE\u0026"]],[["\u0026","\u0026"]],[["\u0026","\u0026"]]],["sv-SE","US",null,0,null,"","/","","",""],["","3.3.1b"],[4241463462],1,null,null,null,null,0,""], loadScriptTime);
  };
  var loadScriptTime = (new Date).getTime();
  getScript(".js");
})();

I have a function:

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

And it's called once:

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

I need to get rid of the document.write, because it's not supported in xhtml in firefox. How do I do this? Jquery is fine :)

Full code:

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./vt?lyrs=m@139\u0026src=api\u0026hl=sv-SE\u0026","http://mt1.google./vt?lyrs=m@139\u0026src=api\u0026hl=sv-SE\u0026"]],[["http://khm0.google./kh?v=74\u0026hl=sv-SE\u0026","http://khm1.google./kh?v=74\u0026hl=sv-SE\u0026"],null,null,null,1],[["http://mt0.google./vt?lyrs=h@139\u0026src=api\u0026hl=sv-SE\u0026","http://mt1.google./vt?lyrs=h@139\u0026src=api\u0026hl=sv-SE\u0026"],null,null,"imgtp=png32\u0026"],[["http://mt0.google./vt?lyrs=t@126,r@139\u0026src=api\u0026hl=sv-SE\u0026","http://mt1.google./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./cbk?","http://cbk1.google./cbk?"]],[["http://khmdb0.google./kh?v=33\u0026hl=sv-SE\u0026","http://khmdb1.google./kh?v=33\u0026hl=sv-SE\u0026"]],[["http://mt0.google./mapslt?hl=sv-SE\u0026","http://mt1.google./mapslt?hl=sv-SE\u0026"]],[["http://mt0.google./mapslt/ft?hl=sv-SE\u0026","http://mt1.google./mapslt/ft?hl=sv-SE\u0026"]]],["sv-SE","US",null,0,null,"http://maps.google.","http://maps.gstatic./intl/sv_se/mapfiles/","http://gg.google.","https://maps.googleapis.","http://maps.googleapis."],["http://maps.gstatic./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./intl/sv_se/mapfiles/api-3/3/1b/main.js");
})();
Share Improve this question asked Dec 4, 2010 at 22:11 HimmatorsHimmators 15k39 gold badges136 silver badges232 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

There's a $.getScript() for this already present in jQuery core:

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

It behaves in a similar way, creating a <script> element and appending it to the <head>...but not with document.write().

Why don't you put it in the document?

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

If it requires you to load the script after doing the rest, put both script tags at the end of the body (the googlemaps script as the second one)

You could say:

var tag = $('<script src="' + src + '" type="text/javascript"></script>');
$("head").append(tag);
document.documentElement.appendChild(document.createElement("script")).src = "http://maps.gstatic./intl/sv_se/mapfiles/api-3/3/1b/main.js";
发布评论

评论列表(0)

  1. 暂无评论