简体   繁体   English

在新窗口或标签中打开AJAX链接

[英]Open AJAX link in new window or tab

I have a page with links that load new content into one of the divs. 我有一个页面,其中包含将新内容加载到div之一中的链接。 This work fine, but I would also like to give the user the option to open those links in a new tab if they right-click and choose to 'open in new tab'. 这项工作正常,但如果用户右键单击并选择“在新选项卡中打开”,我还希望为用户提供在新选项卡中打开这些链接的选项。

So, the javascript AJAX would handle the loading of the new content normally, but then if they select 'open in new tab' perhaps the main HREF would fire and bring the user to the full page with content in the other tab. 因此,javascript AJAX将正常处理新内容的加载,但是如果它们选择“在新选项卡中打开”,则可能会触发主HREF并将用户带到另一个选项卡中的内容的整个页面。 Something like: 就像是:

<a href="example.com/fullPageWithContent.html" onclick="loadContentOnly(1);">Click me</a>

<script>
function loadContentOnly(n) {
event.preventDefault(); //Some condition here?

// AJAX load content for n...

};
</script>

How is this best achieved? 如何最好地实现? (I'm using jQuery, but a vanilla solution even better!) (我使用的是jQuery,但更好的解决方案是更好的!)

Not really clear if determination to show in new page is global or not. 不确定是否要在新页面中显示是否是全局的。

Following assumes it is predetermined 以下假设是预定的

<a href="example.com/fullPageWithContent.html" onclick="return loadContent(1, this);">Click me</a>

<script>
function loadContent(n, el) {

  if(showInNewPage){
     el.target = '_blank';
     return true;
  } else{    
      // AJAX load content for n...

      return false;    
};
</script>

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

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