ASP.NET Facebook Connect Confusion: Property 'open' of object [object DOMWindow] is not a function
-
26-10-2019 - |
سؤال
I am trying to implement Facebook Connect functionality into a website that I am working on. The site uses ASP.NET, a master page that wraps every existing page (including my login page) and Ajax Control Toolkit for some controls. I am using JavaScript SDK functions as the Facebook Documentation dictates. Here is the code to call Facebook API:
`<div id="fb-root">
</div>
<script type="text/javascript">
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=APP_ID";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
</script>`
And here is the code that supplies users an entry point to Facebook Login with an anonymous JavaScript function:
<input type="button"
onclick=" FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
FB.logout(function(response) {
console.log('Logged out.');
});
});
}
else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});" value="Login Facebook" />
The problem is that I get an exception saying that window.open is not a function but a property of the object and the Facebook Login dialog won't appear (The exact exception message is as in the title). I don't understand where this error comes from. I have been dealing with this problem for a day and now I have got desperate about solving it. Is there a way to find the source of problem? Can you please help?
Thanks in advance.
Note: The weird thing is that it works when I create a new page without using the Master Page and migrate all the Facebook calls to the new page.
المحلول
Thanks to everyone who took a look at the question.
Fortunately, I could spare some more time to solve the problem and I found where this error is reasoning from. It turns out that another developer declared a variable open = false;
in one of the included JavaScript files without using the "var" keyword. Therefore, it overrode the existing "window.open" function and prevent it from functioning properly. All I had to do was changing the declaration to var open = false;
.
Neither I was experienced enough to notice it quickly nor JavaScript is that advanced to keep developers away from making such mistakes. So that led me to this mess :)
نصائح أخرى
Your appId is not specified in the JS SDK loading code. It's easy to overlook that it would need to be replaced as it is hidden.
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=APP_ID";