NYCPHP Meetup

NYPHP.org

[nycphp-talk] OT: Javascript - Opening a new window, but the window name is getting lost with Facebook.com

Rob Marscher rmarscher at beaffinitive.com
Wed Sep 29 10:17:42 EDT 2010


On Sep 29, 2010, at 1:56 AM, David Roth wrote:
> I was successfully using Javascript code for years to open a new browser window and assign it a unique name without any problems. That is, until now. :-)
> The following Javascript code works properly with any other website I've tried, except for facebook.com

Hi David,

I agree with some of the other suggestions that a modal javascript popup or just using a regular link are the more standard ways of doing this nowadays.  However, I was curious why your code wasn't working.  

Here it is:

In the javascript on Facebook, in addition to checking if window == window.top to break out of iframes, it also sets the window.name to it's own custom name.

When you do window.open(url, name) from your site, it searches the browser windows for one named after what you passed in.  But Facebook has changed the name of your window, so it no longer works.  You can do the same thing on your site by putting this javascript in your html head:

<script type="text/javascript">
window.name = "hahaThisIsMyWindow"; // obviously, you could name it whatever you want
</script>


However, there is a way around it by referencing a DOMWindow object returned by window.open rather than just relying on the name.  This should work for you (note that the stuff in the <script> tag only needs to be included once on your page):

<script type="text/javascript">
var modalWindows = {};
function openModalUrl(url, name)
{
	if (modalWindows[name] == undefined || modalWindows[name].closed) { 
		modalWindows[name] = window.open(
			url, 
			name, 
			'width=600, \
			height=500, \
			directories=no, \
			location=no, \
			menubar=no, \
			resizable=no, \
			scrollbars=1, \
			status=no, \
			toolbar=no'
		); 
	} else {
		modalWindows[name].focus();
	}
	return false;
}
</script>

<a href="#" onclick="return openModalUrl('http://facebook.com/USERNAME_GOES_HERE', 'facebook');">Become a friend on Facebook</a>


-Rob





More information about the talk mailing list