一年前我為了簡少麻煩,因為有三四個js檔,想崁入一個js檔就好了,其他js檔包在被網頁崁入那個js檔中,試好久都沒成功。

最近無意間從Google的程式碼中發現,Js檔崁入Js檔的方式,沒想到那麼簡單。

實作

有三個檔案

Test.html -主頁

<html>
<head>
    
<script type="text/javascript" src="a.js"></script>
</head>
<body>
</body>
</html>

 

A.js         -崁B.js

document.write(unescape("%3Cscript src='b.js' type='text/javascript'%3E%3C/script%3E"));

 

B.js

alert('test')

 

這樣Test.html原本只有崁入a.js,但因為這樣的方法可以崁入更多的js檔

而且還可以調整src的參數,請看原Google的程式碼

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape(
"%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

 

測試平台

Google用的當然是所有瀏覽器都可以用,而我也在

IE 7.0
Google chromium
Firefox 3.0

測試正常

相关文章:

  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2018-08-15
  • 2021-10-31
  • 2022-12-23
  • 2021-05-21
  • 2021-10-17
猜你喜欢
  • 2022-12-23
  • 2022-01-18
  • 2021-10-20
  • 2021-10-10
  • 2021-11-24
  • 2021-07-02
  • 2022-01-03
相关资源
相似解决方案