简体   繁体   English

如何建立一个不指向“在新窗口中打开”的链接

[英]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 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. 我有一个jquery函数,它显示此弹出窗口并为链接返回false。 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. 没有特定的浏览器可以访问它,就无法更改它的GUI,因为浏览器GUI绝不是标准。

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. 据我所知,您不能禁用此选项,因为该选项由Web浏览器本身提供,并且每个选项都以不同的方式实现该功能。 You can however check the referrer value in the HTTP headers and ensure that its a correct value. 但是,您可以在HTTP标头中检查引荐来源网址的值,并确保其值为正确的值。 When a user opens a new window the referrer should be set to null allowing you to intervene. 用户打开新窗口时,应将引荐来源网址设置为null,以便您进行干预。

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. 如果您使用的是javascript,则还可以使单击链接执行javascript方法。 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? 如何使用Ajax生成页面内容?

something like that: 像这样的东西:

HTML HTML

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

JAVASCRIPT 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']. 现在剩下要做的就是创建一个PHP,在其中检查调用的菜单ID,并根据$ _GET ['pageNo']回显页面内容。 if you already got your pages on many PHP/HTML you may also just do include and echo them... 如果您已经在许多PHP / HTML上获得了页面,则也可以只包含并回显它们...

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... 编辑:您还可以添加URL参数来引用当前页面,以便用户可以从新窗口重新加载页面而无需加载参数...

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

Remove the HREF attribute from the tag, but apply CSS styling to make it look like a link in all browsers. 从标记中删除HREF属性,但应用CSS样式使其在所有浏览器中看起来像一个链接。 Then use a Javascript onclick function to do what it is you want to do. 然后,使用Javascript onclick函数执行您要执行的操作。 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 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. 因此,我编写了一个小小的jquery脚本,用span元素替换所有我需要伪造的链接。

// 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 http://www.billybear4kids.com/clipart/riteclic.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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