0

我在页面上的图像有问题。我正在使用 Javascript 创建元素,在 FireFox 中,我用来设置 innerHTML 的字符串似乎没有被正确解析。当使用无效的 GET 变量请求服务器页面时,我会看到这一点。它们看起来像这样(来自 PHP 脚本的错误处理程序):

GET[] = Array
(
  [shrink] => true
  [file_id] => \'   file_id   \'
  [refresh] => \'   now.getTime()   \'
)

这只发生在大约 5% 的请求中,这使得解决起来很困难。我已经能够自己在 FireFox 中重现这一点,Firebug 会显示它试图获取的 URL 是:https ://www.domain.com/secure/%27%20+%20image_src%20+%20% 27

我在某处读到它可能与 FireFox 预取内容有关(现在无法在谷歌上搜索),因为它似乎只发生在 FireFox 上。在 about:config 中禁用预取确实可以防止问题发生,但我正在寻找另一种不涉及最终用户更改其配置的解决方案或解决方法。

这是细节和代码:我在 HTML 页面上有一个空的表格单元格。在页面的 JQuery 的 $(document).ready() 函数中,我使用 JQuery 的 $.ajax() 方法从服务器获取一些关于该单元格中应该包含什么的数据。它返回file_id变量,为简单起见,我在下面设置了该变量。然后它将空表格单元格设置为具有 src 的图像,该图像指向将根据传递的 file_id 提供图像文件的页面。这部分代码最初是 JQuery,所以我将其更改为直接 Javascript,但这没有任何帮助。

//get data about image from server
//this is actually done through JQuery's $.ajax() but you get the idea
var file_id = 12;

//create the src for the img
//the refresh is to prevent the image from being cached ever, since the page's
//javascript will be it changes
//during the course of the page's life
var now = new Date();
var image_src = 'serve_image.php?shrink=true&file_id=' + file_id + '&refresh=' + now.getTime();

//create 
document.getElementById('image_cell').innerHTML =
    '<A target="_blank" href="serve_image.php?file_id=' + file_id + '">' +
        '<IMG id=image_element src="' + image_src + '" alt="Loading...">' + 
    '</A>';`

任何帮助将不胜感激。谢谢!

4

2 回答 2

1

而不是改变document.getElementById("image_cell").innerHTML,尝试给你的 <a> 一个 ID 并做

document.getElementById('a_tag_id_here').href = 'serve_image.php?file_id=' + file_id;
document.getElementById('image_element').src = image_src;

如果你不能设置锚标签的 id,还有其他的 DOM 函数可以很容易地访问。关键是,我认为更改 innerHTML 是您问题的根源。

于 2010-05-19T19:29:55.007 回答
0

看起来你正在混合双引号和单引号。由于您的代码示例已修改,我无法确定,但我的猜测是这一行:

var image_src = 'serve_image.php?shrink=true&file_id=' + file_id + '&refresh=' + now.getTime();

真的被这样对待:

var image_src = "'serve_image.php?shrink=true&file_id=' + file_id + '&refresh=' + now.getTime()";

您的 JavaScript 是由 PHP 生成的,还是在单独的文件中?如果您要输出内联 JavaScript,您可能需要考虑将其放在单独的 *.js 文件中,以减少 PHP 对引号的处理不会干扰 JavaScript 引号的可能性。

希望这可以帮助!

于 2010-05-19T16:24:04.633 回答