12

The following code works well and launches a facebook popup on the screen, however this popup is not centered.

<script type="text/javascript">
    function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}
</script>

<a href="http://www.facebook.com/share.php?u=<full page url to share" onClick="return fbs_click()" target="_blank" title="Share This on Facebook"><img src="images/facebookimage.jpg" alt="facebook share"></a>

Here is the script that centers a popup window:

<script type="text/javascript">
    function MyPopUpWin(url, width, height) {
        var leftPosition, topPosition;
        //Allow for borders.
        leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
        //Allow for title and status bars.
        topPosition = (window.screen.height / 2) - ((height / 2) + 50);
        //Open the window.
        window.open(url, "Window2",
        "status=no,height=" + height + ",width=" + width + ",resizable=yes,left="
        + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY="
        + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
    }
</script>

How can I update the first script so that it works in combination with the second script to result in the popup window launching in the center of the screen?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
user1736794
  • 265
  • 2
  • 7
  • 12

2 Answers2

32

See if this works for you:

<script type="text/javascript">
function fbs_click(width, height) {
    var leftPosition, topPosition;
    //Allow for borders.
    leftPosition = (window.screen.width / 2) - ((width / 2) + 10);
    //Allow for title and status bars.
    topPosition = (window.screen.height / 2) - ((height / 2) + 50);
    var windowFeatures = "status=no,height=" + height + ",width=" + width + ",resizable=yes,left=" + leftPosition + ",top=" + topPosition + ",screenX=" + leftPosition + ",screenY=" + topPosition + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no";
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer', windowFeatures);
    return false;
}
</script>

<!-- Please change the width and height to suit your needs -->
<a href="http://www.facebook.com/share.php?u=<full page url to share" onClick="return fbs_click(400, 300)" target="_blank" title="Share This on Facebook"><img src="images/facebookimage.jpg" alt="facebook share"></a>
arturhoo
  • 2,492
  • 1
  • 21
  • 22
  • 10
    That works absolutely perfectly! You're a super genius coder ultimate uber cool person. Thanks. :) Pia @arturhoo – user1736794 Oct 14 '12 at 14:18
  • for me also it is working but the problem is this code is not fetching the data and image which I need. can u please help me to fix? I kept even on the header – Sujith Wayanad Feb 01 '15 at 17:01
0

Try this



    <script type="text/javascript">
    function fbs_click() {
      u=location.href;t=document.title;window.open(
      'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
      'sharer','toolbar=0,status=0,width=626,height=436');return false;
      }
    </script>

    <a href="http://www.facebook.com/share.php?u=<url>" onclick="return fbs_click()" target="_blank">Share on Facebook</a>


Or this post with a picture on Facebook



    <script type="text/javascript">
    function fbs_click() {
      u=location.href;t=document.title;window.open(
      'http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),
      'sharer','toolbar=0,status=0,width=626,height=436');return false;
      }
    </script>

    <a href="http://www.facebook.com/share.php?u=<url>" onclick="return fbs_click()" target="_blank"><img src="http://originus.samsung.com/us/images/support/facebook-share.png"/></a>

You can change the image or words
Size pop-up window
width=626,height=436
You can change the size

sn ps
  • 46
  • 1
  • 5