13

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 invoked 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");
    }
    };
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
markratledge
  • 17,322
  • 12
  • 60
  • 106

1 Answers1

33

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)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • How would I remove the spaces? I didn't write the JS, just found it. – markratledge Jan 30 '11 at 02:31
  • Thanks, but still an invalid argument error. There is a space in between the two single quotes? – markratledge Jan 31 '11 at 14:59
  • What is the actual value? Do an `alert("sharewindow"+tpl.substr(6,15))` to see it. – BalusC Jan 31 '11 at 15:01
  • The alert is sharewindow/twitter.com/ho and sharewindow/www.facebook.c Is that an incomplete message? Or did I add the alert in the wrong way? – markratledge Jan 31 '11 at 21:15
  • I'm not sure about those slashes and dots as well. Remove them as well. Use `replace(/\W*/g, '')` instead. This will replace every sequence of non-alphanumeric character by nothing. – BalusC Jan 31 '11 at 21:25
  • This should work. I did a quick test here, IE indeed throws *invalid argument* on `/` and `.`. Note that the regex is case sensitive. I've updated the answer with a copy'n'pasteable example. – BalusC Feb 01 '11 at 03:08
  • Works! Even with a popup blocker. Thanks. Time for me to learn more JS. Using the alert function is a great idea, as is jsfiddle. – markratledge Feb 01 '11 at 15:00
  • replace(/\W+/g, '') is better because \W* will also replace '' with '', which isn't that usefull. EG: https://regex101.com/r/jD6cO5/1 \w* on "test test test" gives you 18 matches, https://regex101.com/r/jD6cO5/2 \w+ gives you 2 matches, the spaces only. – Filip Cornelissen Dec 28 '15 at 15:12