How do I make link_to open external URLs in a new window?

前端 未结 4 431
抹茶落季
抹茶落季 2021-01-30 14:39

I need to convert a rails 2.3 site so that all external URLs open in a new window. I could go though every call to link_to and add :target => \'_blank\'

4条回答
  •  北海茫月
    2021-01-30 15:14

    You should not have to change your server-side code for this view problem.

    You should use Unobscursive javascript. This example will only make external links showing up in a new window :

    // jQuery
    //
    $(document).ready(function() {
      $("a").click(function() {
        link_host = this.href.split("/")[2];
        document_host = document.location.href.split("/")[2];
    
        if (link_host != document_host) {
          window.open(this.href);
          return false;
        }
      });
    });
    

提交回复
热议问题