简体   繁体   中英

Open AJAX link in new window or tab

I have a page with links that load new content into one of the divs. 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. 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!)

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>

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