我想在“友好”的 IFRAME 中加载 AngularJS 应用程序(由于 SEO 原因)。
请参阅此处的示例代码: https://jsfiddle.net/tomsoderlund/mssca32k/
var embedDiv = document.getElementById('weld-embed');
// Create friendly IFRAME
var newIframe = document.createElement('iframe');
newIframe.src = 'about:blank';
newIframe.width = '100%';
newIframe.height = '100%';
newIframe.frameBorder = '0';
embedDiv.appendChild(newIframe);
// Create contents inside
var htmlContent = '<!doctype html>'
// ...
+ '<\/body><\/html>';
newIframe.contentWindow.document.write(htmlContent);
AngularJS 应用程序无法启动,可能是因为 document.ready 没有以正常方式触发。我可以引导/强制 AngularJS 代码启动吗?
最佳答案
问题已解决:
- 从 JSFiddle 转向更简单的 Web 结构。
- 从单独的
template.html
HTML 文件加载htmlContent
*。 - 从
template.html
** 中引导 AngularJS。
*上面第 2 点的代码(改编自 “HTTP GET request in JavaScript?” ):
var httpGetAsync = function (theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
callback(xmlHttp.responseText);
}
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
httpGetAsync('/template.html', function (htmlContent) {
newIframe.contentWindow.document.write(htmlContent);
});
**上面第 3 点的代码:
<script type="text/javascript">
angular.bootstrap(document, ['myAngularModule']);
</script>
关于javascript - 如何引导在友好的 IFRAME 中加载的 AngularJS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42043983/