簡體   English   中英

JS:外部加載

[英]JS: external loading

我在不同領域中有很多頁面,需要包含一些.js .css和html。 我想“遠程”更新包含的代碼。

因此,在這些靜態頁面中,我很難放置:

<script src="http://centraldomain.com/include.js" type="text/javascript"></script>

然后在該文件中執行以下操作:

document.write('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script><div id="results"></div>');

$('#result').load('http://domain.com/include-rest.html');

並將所有要插入的html放在這些html中,例如:

<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/cupertino/jquery-ui.css" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
some html without <head> or <body>

這是最好的方法嗎? 我沒有考慮到任何跨域或XSS攻擊安全性問題嗎?

謝謝

無需使用外部資源來管理代碼加載,而是可以導入單個文件,然后使用資源加載器來管理依賴關系。 無需讓外部腳本執行代碼(將相同的原始策略絆倒),您就可以在一處管理所有內容並回避任何跨域問題。

yepnope.js是可以幫助的異步資源。 您可以對任何站點使用test,導入必要的文件,然后對您在哪個站點進行測試,以確定是否需要執行任何其他代碼。

看看http://yepnopejs.com/ -快速瀏覽。

yepnope([{
  test : /* boolean(ish) - Something truthy that you want to test             */,
  yep  : /* array (of strings) | string - The things to load if test is true  */,
  nope : /* array (of strings) | string - The things to load if test is false */,
  both : /* array (of strings) | string - Load everytime (sugar)              */,
  load : /* array (of strings) | string - Load everytime (sugar)              */,
  callback : /* function ( testResult, key ) | object { key : fn }            */,
  complete : /* function                                                      */
}, ... ]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM