window.open在IE7-8-9b中抛出无效参数 [英] window.open throws invalid argument in IE7-8-9b
问题描述
我不太了解javascript来弄清楚为什么这个脚本中以window.open ...开头的行在IE7-8-9b中抛出了无效的参数错误。在Firefox和Webkit中正常工作。
I don't know enough about javascript to figure out why the line in this script that begins "window.open..." throw an invalid argument error in IE7-8-9b. Works fine in Firefox and Webkit.
(脚本使用 onclick =share.fb()
在一个html链接中弹出一个新的浏览器窗口,在FB和Twitter上分享。)
(The script is envoked with an onclick="share.fb()"
in a html link and pops up a new browser window to share at FB and 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");
}
};
推荐答案
IE禁止窗口名称中的空格和其他特殊字符(第二个论点)。你需要在作为参数传递之前删除它们。
IE disallows spaces and other special characters in window name (the second argument). You need to remove them before passing as argument.
替换
"sharewindow"+tpl.substr(6,15)
by
"sharewindow"+tpl.substr(6,15).replace(/\W*/g, '')
以便最终得到
window.open(tpl,"sharewindow"+tpl.substr(6,15).replace(/\W*/g, ''),"width=640,height=480");
(这基本上是一个正则表达式替换,表示替换每个非aplhabetic角色的序列没有)
现场演示这里(必要时配置你的弹出窗口拦截器)
Live demo here (configure if necessary your popup blocker)
这篇关于window.open在IE7-8-9b中抛出无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!