Javascript document.write不起作用,但console.log起作用

Javascript document.write不起作用,但console.log起作用,javascript,facebook-javascript-sdk,facebook-fql,document.write,Javascript,Facebook Javascript Sdk,Facebook Fql,Document.write,所以我用facebook上某个用户创建的事件创建了一个数组,我所有的API都在工作,但我无法记录。出于某种原因,我写了它 这是我的密码: for( var i = 0 ; i < response.length ; i++ ) { if (response[i]['eid'] > 0) { document.write([response[i]['name']] + '</br>' + response[i]['description']

所以我用facebook上某个用户创建的事件创建了一个数组,我所有的API都在工作,但我无法记录。出于某种原因,我写了它

这是我的密码:

for( var i = 0 ; i < response.length ; i++ ) 
{
    if (response[i]['eid'] > 0)
    {
        document.write([response[i]['name']] + '</br>' + response[i]['description']),
        console.log([response[i]['name']] + '</br>' + response[i]['description']);
    }
}
for(变量i=0;i0)
{
文件。写入([response[i]['name']]+'
'+response[i]['description']), log([response[i]['name']]+'
'+response[i]['description']); } }
当我记录它时,它很好,但我不能在页面上实际显示它。提醒()'它也可以工作


你知道我如何把这些变量吐出来吗?

当你调用document.write时,页面加载后,它会重写当前页面,其中不包含返回的数据或循环遍历该数据。由于您使用的是FBAPI,我猜这是在页面加载后运行的。尝试使用客户端模板解决方案来呈现所有这些数据。这样,您就不必进行大量的字符串连接来为数据创建HTML。

当您在页面加载后调用document.write时,它会重写当前页面,该页面不包含返回的数据或循环迭代该数据。由于您使用的是FBAPI,我猜这是在页面加载后运行的。尝试使用客户端模板解决方案来呈现所有这些数据。这样,您就不必做大量的字符串连接来为数据创建HTML。

如果页面的唯一目的是显示FB api调用的结果,那么只要页面设置为有效的HTML,并且所有javascript都包含在文档的头部,document.write就可以工作。document.write通常仅在页面加载之前和正文中使用。页面加载后,将覆盖并替换文档的整个正文部分。因此,如果您的任何脚本在主体中,它也将被替换

在我看来,一个更好的选择是使用div并用结果填充div

HTML:


Javascript:

var results = "";

for( var i = 0 ; i < response.length ; i++ ) 
{
    if (response[i]['eid'] > 0)
    {
        results += response[i]['name'] + '</br>' + response[i]['description'];
        console.log(response[i]['name'] + '</br>' + response[i]['description']);
    }
}

document.getElementById("results").innerHTML = results;
var结果=”;
对于(变量i=0;i0)
{
结果+=响应[i]['name']+'
'+响应[i]['description']; log(响应[i]['name']+'
'+响应[i]['description']); } } document.getElementById(“结果”).innerHTML=results;
编辑:我上面的解释是错误的,document.write会在页面加载后重写整个文档。我上面的解决方案仍然是100%有效的

上面被接受的答案不是100%正确。。。下面的代码清楚地表明,即使文档被覆盖,至少在全局对象(窗口)中已经设置的函数和变量没有丢失,它们仍然在运行。因此,如果您在已设置的数据中循环,它仍将运行并显示结果,因此问题不仅仅是javascript被覆盖

试试这个:

<!DOCTYPE html>
<html>
<head>
<title>hi</title>
<script type="text/javascript">
    window.onload = function () {
        setTimeout(function () {
            for (i = 0; i < 10; i++)
                // this runs 3 seconds after the page loads, so after the first iteration
                // the entire document is erased and overwritten with 'hi',
                // however this loop continues to run, and write() continues to execute,
                // showing that the javascript still exists and operates normally.
                write();
        }, 3000);
    };

    // this variable will still exist after the page is overwritten
    window.someVar = "foo";

    // this function still exists and executes after the page is overwritten
    function write() {
        document.write("hi");
    }
</script>
</head>
<body>
<div>
    <b>hello</b>
</div>
</body>
</html>

你好
window.onload=函数(){
setTimeout(函数(){
对于(i=0;i<10;i++)
//这将在页面加载后运行3秒,因此在第一次迭代之后
//整个文档将被擦除并覆盖为“hi”,
//但是这个循环继续运行,write()继续执行,
//显示javascript仍然存在并正常运行。
write();
}, 3000);
};
//此变量在页面被覆盖后仍然存在
window.someVar=“foo”;
//此函数仍然存在,并在页面被覆盖后执行
函数写入(){
文件。填写(“hi”);
}
你好

如果页面的唯一目的是显示FB api调用的结果,那么只要页面设置为有效的HTML,并且所有javascript都包含在文档的头部,document.write就可以工作。document.write通常仅在页面加载之前和正文中使用。页面加载后,将覆盖并替换文档的整个正文部分。因此,如果您的任何脚本在主体中,它也将被替换

在我看来,一个更好的选择是使用div并用结果填充div

HTML:


Javascript:

var results = "";

for( var i = 0 ; i < response.length ; i++ ) 
{
    if (response[i]['eid'] > 0)
    {
        results += response[i]['name'] + '</br>' + response[i]['description'];
        console.log(response[i]['name'] + '</br>' + response[i]['description']);
    }
}

document.getElementById("results").innerHTML = results;
var结果=”;
对于(变量i=0;i0)
{
结果+=响应[i]['name']+'
'+响应[i]['description']; log(响应[i]['name']+'
'+响应[i]['description']); } } document.getElementById(“结果”).innerHTML=results;
编辑:我上面的解释是错误的,document.write会在页面加载后重写整个文档。我上面的解决方案仍然是100%有效的

上面被接受的答案不是100%正确。。。下面的代码清楚地表明,即使文档被覆盖,至少在全局对象(窗口)中已经设置的函数和变量没有丢失,它们仍然在运行。因此,如果您在已设置的数据中循环,它仍将运行并显示结果,因此问题不仅仅是javascript被覆盖

试试这个:

<!DOCTYPE html>
<html>
<head>
<title>hi</title>
<script type="text/javascript">
    window.onload = function () {
        setTimeout(function () {
            for (i = 0; i < 10; i++)
                // this runs 3 seconds after the page loads, so after the first iteration
                // the entire document is erased and overwritten with 'hi',
                // however this loop continues to run, and write() continues to execute,
                // showing that the javascript still exists and operates normally.
                write();
        }, 3000);
    };

    // this variable will still exist after the page is overwritten
    window.someVar = "foo";

    // this function still exists and executes after the page is overwritten
    function write() {
        document.write("hi");
    }
</script>
</head>
<body>
<div>
    <b>hello</b>
</div>
</body>
</html>

你好
window.onload=函数(){
setTimeout(函数(){
对于(i=0;i<10;i++)
//这将在页面加载后运行3秒,因此在第一次迭代之后
//整个文档将被擦除并覆盖为“hi”,
//但是这个循环继续运行,write()继续执行,
//显示javascript仍然存在并正常运行。
write();
}, 3000);
};
//此变量在页面被覆盖后仍然存在
window.someVar=“fo