简体   繁体   English

Javascript重定向到动态创建的HTML

[英]Javascript redirect to dynamically created HTML

I have a javascript routine that dynamically creates an HTML page, complete with it's own head and script tags. 我有一个JavaScript例程,可以动态创建HTML页面,并带有其自己的head和script标签。

If I take the contents of the string and save it to a file, and view the file in a browser, all is well, but if I try document.write(newHTML), it doesn't behave the same. 如果我获取字符串的内容并将其保存到文件中,然后在浏览器中查看该文件,一切都很好,但是如果我尝试使用document.write(newHTML),它的行为就不会一样。 The javascript in the header of the dynamic newHTML is quite complicated, and I cannot include it here... But please believe me that it works great if I save it to a file, but not if I try to replace the current page with it using document.write. 动态newHTML标头中的javascript非常复杂,我无法在此处添加它。但是请相信我,如果将其保存到文件中,它会很好用,但是如果尝试用它替换当前页面,则不能使用document.write。 What possible pitfalls could be contributing to this that I'm not considering? 我没有考虑的可能有哪些陷阱会导致这种情况? Do I possibly need to delete the existing script tags in the existing header first? 我是否可能需要首先删除现有标头中的现有脚本标签? Do I need to manually re-call onLoad?? 我需要手动重新调用onLoad吗?

Again, it works great when the string is saved to, for example, 'sample.html' and browsed to, but if I set var Samp="[REAL HTML HERE]"; 同样,当将字符串保存到例如“ sample.html”并浏览至该字符串时,它的效果很好,但是如果我将其设置为var Samp =“ [REAL HTML HERE]”; and then say document.write(Samp); 然后说document.write(Samp); document.close(); document.close(); the javascript routines are not executing correctly. javascript例程无法正确执行。

Any hints as to what I could be missing? 关于我可能会缺少什么的任何提示?

Is there another/better way to dynamically replace the content of the page, other than document.write? 除了document.write之外,还有其他/更好的方法来动态替换页面的内容吗?

Could I somehow redirect to the new page despite the fact that doesn't exist on disk or on a server, but is only in a string in memory? 尽管磁盘或服务器上不存在,但仅存在于内存中的字符串中,但我能以某种方式重定向到新页面吗? I would hate to have to upload the entire file to my server simply to re-download again it to view it. 我不想将整个文件上传到我的服务器,只是为了再次重新下载以查看它。

How can I, using javascript, replace the current content of the current page with entirely new content including complex client-side javascripting, dynamically, and always get exactly the same result as if I saved the string to the server as an html file and redirected to it? 如何使用javascript动态地以全新的内容(包括复杂的客户端javascript)替换当前页面的当前内容,包括复杂的客户端javascript,并始终获得与将字符串作为html文件保存到服务器并重定向后完全相同的结果要吗?

How can I 'redirect' to an HTML file that only exists as a client-side string? 如何“重定向”到仅作为客户端字符串存在的HTML文件?

Maybe eval() function would help here? 也许eval()函数在这里会有所帮助? It's hard to give ansver without seeing the code. 不看代码就很难回答。

You can do this: 你可以这样做:

var win=window.open("") //open new window and write to it

var html = generate_html();

win.document.write(html)
win.document.close();

Never tried this, but i think it should be possible. 从未尝试过,但是我认为应该是可能的。 Some thoughts on what might make it work: 关于使它起作用的一些想法:

  • Make sure the document containing your js is sent with the correct headers / mimetype / doctype 确保包含您的js的文档以正确的标头/ mimetype / doctype发送
  • Serve the javascript in a valid way, for example by sending a w3c valid page containing the script tag. 以有效的方式服务javascript,例如,通过发送包含script标签的w3c有效页面。

Maybe then it works. 也许那就行了。 If not, try to erase the current html before writing the new one. 如果不是,请尝试在写入新HTML之前擦除当前HTML。

Also, it might be helpful to look how others managed to accomplish this task. 此外,查看其他人如何完成此任务可能会有所帮助。 If i remind it correctly, the google page is also essentially a short html page with a bunch of js. 如果我没记错的话,谷歌页面本质上也是带有一堆js的短HTML页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM