【发布时间】:2015-11-25 10:01:08
【问题描述】:
我需要在发布请求后打开一个新 URL。我已经在我的控制器的末尾完成了这个。
Redirect::away($url)
上述调用完美无缺,但是,我想在新标签页中打开 URL。
我尝试了 laravel 文档中提供的方法,远离和预期的方法。没有按预期工作。
【问题讨论】:
-
我认为没有服务器端方式可以在客户端打开一个新选项卡。您需要使用 JavaScript 来完成。
我需要在发布请求后打开一个新 URL。我已经在我的控制器的末尾完成了这个。
Redirect::away($url)
上述调用完美无缺,但是,我想在新标签页中打开 URL。
我尝试了 laravel 文档中提供的方法,远离和预期的方法。没有按预期工作。
【问题讨论】:
Redirect::away() 或 Redirect:to() 都是服务器端命令,它们所做的只是重定向到一个略有不同的 url,如 this thread 中所述
因为,两者都是服务器端命令,它们无法打开新标签。
您需要客户端代码来打开新标签,例如:
<a href="#" target="_blank">Open in a new tab</a>
【讨论】:
我知道这是一个老问题,但作为参考,您可以使用
让客户端从服务器重定向到新窗口/选项卡echo "<script>window.open('".$url."', '_blank')</script>";
附带说明,这是否会在新窗口或新标签页中打开链接取决于浏览器和浏览器设置。
【讨论】:
Laravel 或(一般是 PHP)是服务器端,我们无法在其中打开新标签页,因为新标签页是浏览器的东西,所以解决方案是在前端进行,例如:
<a>标签<a href="#Link" target="_blank">New tab</a>。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>
【讨论】: