简体   繁体   中英

How to make a link not to “Open in new window”

When we click with right mouse button on a any site link, there's a dropdown menu. One of the options here is "Open in new window". For some reasons, I need to disable this possibility for all site links. Is it possible?

Thanks in advance.

UPDATE

The reason for doing this

The site menu has 2 types of menu items: ordinary links to pages and fake links, that cause popup window with certain information. I have a jquery function, that shows this popup and returns false for the link. And when a user "Open in new window" some of fake links, page reloads, but popup does not appear. And the user is looking at the empty page. That's not good I think :)

No, not in all browsers, probably not in any browser. You can't change it's GUI without the specific browser giving you access to it, as the browser GUI isn't a standard in any way.

And why would you like to do that anyway?

As far as I know you cant disable this option since its provided by the web browser itself and each one implements the feature differently. You can however check the referrer value in the HTTP headers and ensure that its a correct value. When a user opens a new window the referrer should be set to null allowing you to intervene.

I would also hope that you consider not implementing this because some browsers have inconsistent behavior or an appliance on the network might remove the header information which would leave people unable to use your site.

If you are using javascript, you might as well make clicking links execute a javascript method. That way opening it up in a new page does nothing.

that ain't good for website reference, but if you REALLY need to block access from new windows...

How about generating your page content with Ajax?

something like that:

HTML

<a href="#" onclick="page('Main')"></a>
<a href="#" onclick="page('Sub')"></a>
<a href="#" onclick="page('Sub')"></a>

JAVASCRIPT

function xmlhttp() {
  var x;
  try {
    x = new ActiveXObject('Microsoft.XMLHTTP');
  } catch (e) {
    try {
      x = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
      try {
        x = new XMLHttpRequest();
      } catch (e) {
        x = false;
      }
    }
  }
  return x;
}

function page(idMenu) {
  var http = xmlhttp();
  if (!http) {
    alert('XmlHttpRequest non supporté');
  } else {
    var url = 'pageOutput.php?pageNo=' + idMenu;
    http.open('GET', url, true);
    http.onreadystatechange = function () {
      if (http.readyState == 4 && http.status == 200) {
        document.getElementById('pageContent').innerHTML = http.responseText;
      }
    }
    http.send();
  }
}

now all you have left to do is create a PHP where you check whatever menu ID is called and echo page content according to $_GET['pageNo']. if you already got your pages on many PHP/HTML you may also just do include and echo them...

if(isset($_GET['pageNo'])){
    //echo page code here according to $_GET['pageNo'] value
}else{
    //echo main page
}

EDIT: You may also add URL param to refer the current page so the user can reload your page from a new window without having no params loaded...

您可以使用javascript更改url并在单击时添加操作,因此,当用户尝试在新窗口中打开它时,没有任何反应

Remove the HREF attribute from the tag, but apply CSS styling to make it look like a link in all browsers. Then use a Javascript onclick function to do what it is you want to do. Easy. Case in point: look at the "add comment" link / button on this very page. If you right-click on it, you notice that there is no "Open in new Window" option :)

UPDATE

It's possible to disable context menu on any element we want:

$('selector').contextmenu( function() {
    return false;
});

To disable context menu on the page completely, we can use the following:

$('*').contextmenu( function() {
    return false;
});

OLD ANSWER

OK, guys, if we can't change browsers specific features, then we have to think of some other way. As for me, the most suitable idea has been suggested by Quentin. So I wrote a little jquery script to replace all links, that I need to be fake, with span elements.

// transform links with .fake class only
$('.nav >li > a.fake').each(function () {

    // save all necessary link information
    var href = $(this).attr('href');
    var text = $(this).html();

    // add fake span
    $(this).after('<span>'+text+'</span>');

    // save new span into a variable and after this we can remove the link
    var mySpan = $(this).siblings('span');
    $(this).remove();

    // emulate link default behaviour
    mySpan.click(function () {
        window.location.href = href;
    });

});

disable right click should do it...

http://www.billybear4kids.com/clipart/riteclic.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM