简体   繁体   English

JS:外部加载

[英]JS: external loading

I have lot of pages in different domains that need to include some .js .css and html. 我在不同领域中有很多页面,需要包含一些.js .css和html。 I want to "remotely" update the included code. 我想“远程”更新包含的代码。

So in those static pages I tought placing: 因此,在这些静态页面中,我很难放置:

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

then in that file do: 然后在该文件中执行以下操作:

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');

and in that html place all the html I want to insert in those pages, eg: 并将所有要插入的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>

Is this the best approach to take? 这是最好的方法吗? Is there any cross domain or XSS attack security issue I'm not taking into account? 我没有考虑到任何跨域或XSS攻击安全性问题吗?

Thanks 谢谢

Instead of managing your code loading using external resources, you can instead import a single file an use a resource loader to manage dependancies. 无需使用外部资源来管理代码加载,而是可以导入单个文件,然后使用资源加载器来管理依赖关系。 Rather than having external scripts execute code (tripping same origin policies), you can manage everything in one place and side-step any cross domain issues. 无需让外部脚本执行代码(将相同的原始策略绊倒),您就可以在一处管理所有内容并回避任何跨域问题。

yepnope.js is an asynchronous resource that would help. yepnope.js是可以帮助的异步资源。 You can use test for whatever site your on, import files necessary, then test what site your on to determine if you need to execute any further code. 您可以对任何站点使用test,导入必要的文件,然后对您在哪个站点进行测试,以确定是否需要执行任何其他代码。

Take a look at http://yepnopejs.com/ -- here's a quick look. 看看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