[英]window.open throws invalid argument in IE7-8-9b
我不太了解javascript來弄清楚為什么這個腳本中以“window.open ...”開頭的行在IE7-8-9b中拋出了無效的參數錯誤。 在Firefox和Webkit中運行良好。
(該腳本在html鏈接中使用onclick="share.fb()"
進行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");
}
};
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");
(這基本上是一個正則表達式的替代品,上面寫着“無所謂地替換每個非語言字符序列”)
這里的現場演示(必要時配置你的彈出窗口攔截器)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.