2015-01-20 44 views
0

我有一組小部件根據用戶請求動態加載。我有這方面的工作(創建一個腳本標籤隨着適當的小部件要求),以及小部件瀏覽到單獨的組件。可能將信息傳遞給一個browserfied文件?

我的問題是,是否有可能將配置信息傳遞給瀏覽器化的小部件,而不使用全局變量加載?

例如:

var pageHead = document.getElementsByTagName("head")[0]; 
var script = document.createElement('script'); 
script.src = baseUrl +'/' + widget + '.js'; 
script.onload = function (event) { 
    // do something here maybe to pass information into the 'ad' widget 
    // for instance 
}; 
pageHead.appendChild(script); 

回答

1

好的做法傳遞配置參數來browsified窗口小部件將在屬性來聲明它。

<div prop1="abc" prop2="def" id="testwidget"></div> 

,並根據需要使用的getAttribute()方法訪問它們在javascript ...

document.getElementById("testwidget").getAttribute("prop1"); 

這將幫助你保持在DOM級別配置,而不是保持一個全局變量。

希望它有幫助!