我们有以下情况:
在default.aspx
我们有一个链接:
<a href="javascript:doPost()">test</a>.
和JS代码:
function doPost() {
$.post('AnHttpHandlerPage.aspx',"{some_data:...}", function(data) {
if(data.indexOf("http://")==0)
window.open(data);
else{
var win=window.open();
with(win.document) {
open();
write(data); //-> how to execute this HTML code? The code also includes references to other js files.
close();
}
}
}).error(function(msg){document.write(msg.responseText);});
}
回调可以首先是网址或必须执行的第二个html代码。 选项1适合,但在选项2中,将打开一个新窗口,其中代码已写入但未执行。
很明显,因为它发生在流中,所以无法执行。所以问题是,你怎么解决它?也许是refresh()
或类似的?
由于客户的要求,工作流程无法更改,因此必须在doPost()
内解决。
修改
案例2中的响应是这样的HTML。 此部分应执行:
<HTML><HEAD>
<SCRIPT type=text/javascript src="http://code.jquery.com/jquery-latest.js">
</SCRIPT>
<SCRIPT type=text/javascript>
$(document).ready(function() {
do_something...
});
</SCRIPT>
</HEAD>
<BODY>
<FORM>...</FORM>
</BODY>
</HTML>
请帮忙。感谢。
答案 0 :(得分:0)
在你的JS代码中它应该是这样的:
function doPost() {
$.post('AnHttpHandlerPage.aspx',"{some_data:...}", function(data) {
//if(data.indexOf("http://")==0)
if (data.type!="url") //i will add a data type to my returned json so i can differentiate if its url or html to show on page.
window.open(); // I dont know why this is there. You should
else{
var win=window.open(data.url); //This data.url should spit out the whole page you want in new window. If its external it would be fine. if its internal maybe you can have an Action on one of your controllers that spit it with head body js css etc.
/* with(win.document) {
open();
write(data); //-> how to execute this HTML code? The code also includes references to other js files.
close(); */ // No need to write data to new window when its all html to be rendered by browser. Why is this a requirement.
}
}
}).error(function(msg){document.write(msg.responseText);});
}
整体逻辑是这个
如果这没有意义,请讨论。