Google Chrome opens window.open(someurl) just fine…but page/window with clicked link also opens someurl.com

StackOverflow https://stackoverflow.com/questions/4461451

سؤال

As the title says "Google Chrome opens window.open(someurl) just fine...but page/window with clicked link also opens someurl.com.

When I click the "Click here" link with the onclick="shpop..." call attached, my pop up opens /facebook_login.php' correctly...BUT...at the same time, the original window opens /facebook_login.php too!

This happens in Chrome and IE, but FF is fine and doing just what i want..

I have this link:

<a href="/facebook_login.php" onclick="shpop('','','loginfb','');return false">Click here</a>

I know I could remove the href="/facebook_login.php" and replace with href="#" .. but I need the link to work if js is disabled.

I have this js code imported in my tag:

function shpop(u,t,w,v)  
{
    var text = encodeURI(t);
    var uri = encodeURI(u);
    var h = document.location.href;
    h = encodeURI(h);
    var wwidth='600'; /*popup window width*/
    var wheight='300'; /*popup window height*/
    if(v=='' || undefined==v)v=document.domain; /*popup name/title */

   switch(w){
       case 'loginfb':
           var url = '/facebook_login.php';
           wwidth='980';
           wheight='600';
       break;
   }

    window.open(url,v,'width='+wwidth+',height='+wheight);
    return false
}

Any ideas?

هل كانت مفيدة؟

المحلول

what is with returning false, and having false in the onclick?

This

onclick="shpop('','','loginfb','');return false"

Just needs to be

onclick="return shpop('','','loginfb','');"

If the onclick returns any error, the link will still open up. Do you see any errors in the JavaScript console? I wonder if the browsers are freaking out about any . in the window name from using document.domain. Try giving it a name.

onclick="return shpop('','','loginfb','foobar');"

نصائح أخرى

According to the latest browser statistics - well last time it was measured anyway (2008) only 5% of users had Javascript disabled. Nowadays it's likely to be less. Consider that all browsers have it enabled by default. Therefore it's generally only advanced users that for whatever reason choose to disable javascript, and will therefore understand that there's a good chance any website they visit won't work as expected - Facebook, Google, Amazon - everyone uses javascript these days. It's perfectly acceptable to assume the user is using it, with one overall <noscript> version at the start of your page for those users if you really really want to cover all your bases :)

Here is the simplest solution:

<a href="/facebook_login.php" 
    target="FBpopup" 
    onclick="window.open('about:blank','FBpopup','width=980,height=600')">
    Click here
</a>

You don't need return false because you actually want the link to execute.

The trick is to use the same window name in both the window.open and in the link target.

window.open will create the popup, then your login page will run in that popup.

If popups are blocked or Javascript is disabled, your login page will run in a new tab.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top