document.write在jsonp回调 [英] document.write in jsonp callback

查看:119
本文介绍了document.write在jsonp回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用脚本标记hack和jsonp进行跨域请求。在回调函数中,我想使用document.write()将接收到的数据写入DOM。

I am making a cross domain request using script tag hack and jsonp. In the callback function, I want to write the data received to DOM using document.write().

我知道我应该使用appendChild / innerHTML而不是doc.write()。我的约束是我没有类/ id钩子我想写的元素。我只能依靠document.write()来写in place。

I know that I should use appendChild/innerHTML instead of doc.write(). My constraint is I do not have a class/id hook to the element I want to write to. I can only rely on document.write() to write "in place".

以下代码包含在脚本标签的HTML div中:

Following code is included in HTML div in a script tag.:

function request_jsonp(){
var script = document.createElement('script');
var server_path = 'http://somedomain.com/?q=querystring&callback=request_callback';
script.setAttribute('src', server_path);
document.getElementsByTagName('head')[0].appendChild(script);
console.log('data requested');
// document.write('hello world'); // this gets written
}


function request_callback(data){
    console.log('data received');
    document.write(data);
}


 request_jsonp();

 window.onload = function(){
     console.log('onload fired');
 }


/* 
above code prints

data requested
data received
onload fired
*/

基本上document.write即使在回调函数中使用静态字符串也不起作用。我认为如果docment.write在onload之前调用,它会在页面中插入文本,如这里的JavaScript和HTML脚本标签

Basically document.write does not work even with a static string inside the callback function. I thought that if docment.write is called before onload, it inserts text in the page as noted here JavaScript and HTML Script Tags

什么是阻止document.write()写入DOM?

What is preventing the document.write() to write to DOM? Also, is there a better approach to do this.

推荐答案

您应该注意 document.write ); 调用隐式的 document.open(); ,因此,实际上,清除文档中迄今为止的所有内容。
使用 document.write(); 无法向文档中添加 内容。但是,可以使用 document.write(); 来编写整个文档。

You should note that document.write(); calls an implicit document.open();, thus, in effect, clearing everything you have had in the document so far. It is impossible to add content to a document using document.write();. It is, however, possible to use document.write(); to write an entire document.

几种可能的解决方案:


  • 您可以将目标元素作为GET参数传递,如@kirilloid所述。

  • 您可以在所需的位置插入IFrame对象,并使用 myIFrame.document.write(); 更新其内容。

  • You can pass the target element as a GET parameter, as @kirilloid noted.
  • You can insert an IFrame object at the desired place, and use myIFrame.document.write(); to update its content.

这基本上是发生的,因为你正在更新一个容器的内容作为您的脚本代码的父,因此,尚未关闭。

This is basically happening because you are updating the content of a container that is the parent to your script code, and as such, is not closed yet.

或者,我完全偏离这里,让我们面对它,是完全可能的。 ; - )

Either that, or I'm entirely off track here which, let's face it, is entirely possible. ;-)

这篇关于document.write在jsonp回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆