MozTW 討論區

各項 Mozilla 相關軟體與技術討論
現在的時間是 2025-06-27, 16:36

所有顯示的時間為 UTC + 8 小時





發表新文章 回覆主題  [ 6 篇文章 ] 
發表人 內容
 文章主題 : 動態匯入 js 檔
文章發表於 : 2006-09-22, 22:01 
離線

註冊時間: 2005-03-07, 19:06
文章: 43
這是我在 http://csp.no-ip.com/ajax/ 看到的


以下為主程式:
代碼:
function importScript(src) {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = src;
  document.getElementsByTagName("head")[0].appendChild(script);
  }

function a(js) {
  importScript(js);

  b = b * 5;
  document.getElementById("某div").innerHTML = b;
  }


a("1.js");


以下為 1.js 的內容:
代碼:
b = 9;


我的 Web Developer 會一直說 --> 變數b 沒有定義...
可是,如果 1.js 檔有照情況匯入,應該沒問題阿?!
麻煩各位了... :oops:


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2006-09-23, 11:41 
離線

註冊時間: 2005-01-16, 13:34
文章: 131
因為 1.js 的內容是加在最後的,粗糙點說會變成這樣:

代碼:
function a(js) {
  importScript(js);

  b = b * 5;
  document.getElementById("某div").innerHTML = b;
  }

b = 9;

b 在定義前就已經被使用了…。

_________________
Notes redesigned.
--
Opera 9.5 just tastes good. ;-)


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2006-09-25, 19:20 
離線

註冊時間: 2005-03-07, 19:06
文章: 43
李某人 寫:
因為 1.js 的內容是加在最後的,


有辦法...放到前面嗎? :oops: 謝謝


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2006-09-27, 13:27 
離線
[網站管理員]
頭像

註冊時間: 2004-09-27, 09:24
文章: 1685
直接在 DOM 內插入一個 <script> 元素在 Safari 下沒有用,所以最好的方法就是用可怕的 document.write()。

_________________
korp + korp 中文站(沒精神更新)


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2006-09-27, 21:12 
離線
頭像

註冊時間: 2005-04-03, 13:41
文章: 95
來自: Hong Kong
kourge 寫:
直接在 DOM 內插入一個 <script> 元素在 Safari 下沒有用,所以最好的方法就是用可怕的 document.write()。


真可怕  :lol:

script.aculo.us 用的方法:
代碼:
var Scriptaculous = {
  Version: '1.6.1',
  require: function(libraryName) {
    // inserting via DOM fails in Safari 2.0, so brute force approach
    document.write('<script type="text/javascript" src="'+libraryName+'"></script>');
  },
  load: function() {
    if((typeof Prototype=='undefined') ||
       (typeof Element == 'undefined') ||
       (typeof Element.Methods=='undefined') ||
       parseFloat(Prototype.Version.split(".")[0] + "." +
                  Prototype.Version.split(".")[1]) < 1.5)
       throw("script.aculo.us requires the Prototype JavaScript framework >= 1.5.0");
   
    $A(document.getElementsByTagName("script")).findAll( function(s) {
      return (s.src && s.src.match(/scriptaculous\.js(\?.*)?$/))
    }).each( function(s) {
      var path = s.src.replace(/scriptaculous\.js(\?.*)?$/,'');
      var includes = s.src.match(/\?.*load=([a-z,]*)/);
      (includes ? includes[1] : 'builder,effects,dragdrop,controls,slider').split(',').each(
       function(include) { Scriptaculous.require(path+include+'.js') });
    });
  }
}

Scriptaculous.load();

_________________
圖檔


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
 個人資料  
引用回覆  
 文章主題 :
文章發表於 : 2006-09-27, 21:45 
離線

註冊時間: 2005-03-07, 19:06
文章: 43
感謝三位..!!
試過了再來跟大家報告... :o


回頂端
Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7
 個人資料  
引用回覆  
顯示文章 :  排序  
發表新文章 回覆主題  [ 6 篇文章 ] 

所有顯示的時間為 UTC + 8 小時


誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 11 位訪客


不能 在這個版面發表主題
不能 在這個版面回覆主題
不能 在這個版面編輯您的文章
不能 在這個版面刪除您的文章
不能 在這個版面上傳附加檔案

搜尋:
前往 :  
Powered by phpBB® Forum Software © phpBB Group
正體中文語系由 竹貓星球 維護製作
© moztw.org, Mozilla Foundation
MozTW,Mozilla 台灣社群