【问题标题】:window.open throws invalid argument in IE7-8-9bwindow.open 在 IE7-8-9b 中抛出无效参数
【发布时间】:2011-06-17 22:38:10
【问题描述】:

我对 javascript 了解得不够多,无法弄清楚为什么此脚本中以“window.open...”开头的行会在 IE7-8-9b 中引发无效参数错误。在 Firefox 和 Webkit 中运行良好。

(该脚本在 html 链接中使用 onclick="share.fb()" 进行调用,并弹出一个新的浏览器窗口以在 FB 和 Twitter 上共享)。

var share = {
    fb:function(title,url) {
    this.share('http://www.facebook.com/sharer.php?u=##URL##&t=##TITLE##',title,url);
    },
    tw:function(title,url) {
    this.share('http://twitter.com/home?status=##URL##+##TITLE##',title,url);
    },
    share:function(tpl,title,url) {
    if(!url) url = encodeURIComponent(window.location);
    if(!title) title = encodeURIComponent(document.title);

    tpl = tpl.replace("##URL##",url);
    tpl = tpl.replace("##TITLE##",title);

    window.open(tpl,"sharewindow"+tpl.substr(6,15),"width=640,height=480");
    }
    };

【问题讨论】:

  • 您是否尝试在fbtw 函数中将this.share 切换为share.share

标签: javascript internet-explorer javascript-events


【解决方案1】:

IE 不允许在窗口名称(第二个参数)中使用空格和其他特殊字符。您需要在作为参数传递之前删除它们。

替换

"sharewindow"+tpl.substr(6,15)

通过

"sharewindow"+tpl.substr(6,15).replace(/\W*/g, '')

这样你就结束了

window.open(tpl,"sharewindow"+tpl.substr(6,15).replace(/\W*/g, ''),"width=640,height=480");

(这基本上是一个正则表达式替换,它说“用任何内容替换每个非 aplhabetic 字符序列”)

现场演示here(必要时配置您的弹出窗口拦截器)

【讨论】:

  • 如何删除空格? JS不是我写的,随便找的。​​span>
  • 谢谢,但仍然是一个无效的参数错误。两个单引号之间有空格吗?
  • 实际值是多少?发送alert("sharewindow"+tpl.substr(6,15)) 即可查看。
  • 警报是 sharewindow/twitter.com/ho 和 sharewindow/www.facebook.c 这是一条不完整的消息吗?还是我以错误的方式添加警报?
  • 我也不确定那些斜线和圆点。也将它们删除。请改用replace(/\W*/g, '')。这将用任何内容替换每个非字母数字字符序列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 1970-01-01
  • 2012-05-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
相关资源
最近更新 更多