laravel重定向到新标签

时间:2015-11-25 10:01:08

标签: laravel laravel-4

我需要在发布请求后打开一个新网址。我在控制器的末尾完成了这个。

Redirect::away($url)

上述调用很有效,但我想在新标签页中打开网址

我尝试了,离开了,并且在laravel文档中有预期的方法。没有按预期工作。

3 个答案:

答案 0 :(得分:15)

Redirect::away()Redirect:to()都是服务器端命令,他们所做的只是重定向到与this thread

中提到的略有不同的网址

由于两者都是服务器端命令,因此无法打开新选项卡。

您需要客户端代码才能打开新标签,例如: <a href="#" target="_blank">Open in a new tab</a>

答案 1 :(得分:2)

我知道这是一个老问题,但作为参考,您可以使用

从客户端重定向到新窗口/选项卡
echo "<script>window.open('".$url."', '_blank')</script>";

作为旁注,这是否会打开链接新窗口或新选项卡取决于浏览器和浏览器设置。

答案 2 :(得分:0)

Laravel或(通常为PHP)是服务器端,我们无法在其中打开新标签页,因为新标签页是浏览器,因此解决方案是在前端进行操作,例如:

  • 使用<a>标记<a href="#Link" target="_blank">New tab</a>
  • 使用弹出 JavaScript window.open("Link", "_blank", "scrollbars=yes,width=400,height=400"); 但是在使用它时,请务必小心,如果在没有用户交互的情况下打开窗口,浏览器可能会阻止您的弹出窗口(例如,单击按钮是打开弹出窗口的好方法)。

当窗口应显示为弹出窗口时,我更喜欢第二种方法,此处是完整的示例作为快速使用代码:

      function show_my_receipt() {
         
         // open the page as popup //
         var page = 'http://www.test.com';
         var myWindow = window.open(page, "_blank", "scrollbars=yes,width=400,height=500,top=300");
         
         // focus on the popup //
         myWindow.focus();
         
         // if you want to close it after some time (like for example open the popup print the receipt and close it) //
         
        //  setTimeout(function() {
        //    myWindow.close();
        //  }, 1000);
        
       }
<button type="button" class="btn btn-success" onclick="show_my_receipt()">show the receipt</button>

相关问题