Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

javascript - window.open throws invalid argument in IE7-8-9b

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.

(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");
    }
    };
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

IE disallows spaces and other special characters in window name (the second argument). You need to remove them before passing as argument.

Replace

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

by

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

so that you end up with

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

(that's basically a regex replacement which says "replace every sequence of non-aplhabetic character by nothing")

Live demo here (configure if necessary your popup blocker)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...