2009年09月10日 Thursday , 406 次点击

今年早些时候遇到过一个诡异的问题。

需求是在一个弹出窗口中打开/download.php?id=123456这样的URI,并由download.php输出要下载的资源。问题是如果使用Javascript监听OnClick事件,然后调用window.open()方法弹窗,则会报如下错误:

而如果使用target="_blank"形式的超链接弹窗,则一切正常。

示例代码如下:

HTML:
  1. function test(){
  2.     window.open('/download.php?id=1');
  3. }
  4. <a href="#" onclick="test();">test1</a>
  5. <a href="/download.php?id=1" target="_blank">test2</a>

PHP:
  1. <?
  2. if (1 == $_GET['id']) {
  3.     $fp = fopen('test.zip', 'rb');
  4.     $content = fread($fp, filesize('test.zip'));
  5.     fclose($fp);
  6.     header("Content-type:message/rfc822;charset=gb2312");
  7.     header("Content-type: application/download");
  8.     header("Content-disposition: attachment; filename=test.zip");
  9.     header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
  10.     header( "Pragma: no-cache" );
  11.     header( "Expires: 0" );
  12.     echo $content;
  13. }else {
  14.     echo 'Error';
  15. }
  16. ?>

第一个HTML文件提供两个超链接,test1和test2。前者使用JavaScript监听OnClick事件弹窗,后者使用target="_blank"弹窗。两者都调用同样的download.php进行下载,但点击test1就会报错,而test2正常。

我对这个问题感到疑惑。

Tags :

随机日志

來留言吧!


Please copy the string oNB8VN to the field below:

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

尚未有留言

尚未有留言

留言板RSS 引用 URI

來留言吧!

«
»