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>
这是最好的方法吗?是否存在我没有考虑到的跨域或跨站攻击安全问题?
谢谢
您可以导入单个文件并使用资源加载器来管理依赖关系,而不是使用外部资源来管理代码加载。而不是让外部脚本执行代码(触发同源策略),您可以在一个地方管理所有内容,并避免任何跨域问题。
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 */
}, ... ]);
相关文章:
- 使用javascript在Flash中加载外部图像
- 如何在运行时在angular 2中加载外部js脚本
- Chrome扩展无法通过清单加载外部javascript
- 加载Angular视图后加载外部脚本
- Cordova/Phonegap在主要的Cordova网络视图中加载外部站点
- IE 9 BHO插件.加载外部JS文件时出现错误SCRIPT1014
- 是否有用于加载外部脚本的JavaScript库
- 使用纯Javascript加载外部URL内容
- 在Chrome扩展中加载外部javascript
- 在扩展 jQuery 之前加载外部资源
- 无法加载外部 JavaScript 文件 - Codeigniter
- jquery .load() 不加载外部 JS
- GO - html 文件无法加载外部 js 文件
- jQuery 不会使用 .load() 加载外部 html 模板
- 使用HTML5缓存时无法加载外部资源
- 色盒延迟打开;锁定“;当加载外部javascript文件时
- 使用load()加载外部文件,然后使用fancybox插件进行修改并显示
- 在Ember应用程序中加载外部域脚本
- html javascript加载外部脚本
- 如何防止加载外部资源,同时防止“在'文档'上执行'写入'失败”错误