javascript 在 PHP 上打开一个新选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13427934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a new tab on PHP
提问by mrjimoy_05
I have a simple form:
我有一个简单的表格:
<form id="frm" action="process.php" method="post">
<input type="text" id="shortcut" name="shortcut" />
</form>
In process.php
:
在process.php
:
$gomenu = $_POST['shortcut'];
if(strpos($gomenu, "/") === 0) {
//if $gomenu contains `/`, then it should open in new tab
header('Location: newTab.php'); //--> How to open this into a new tab?
} else {
header('Location: somePage.php'); //--> Redirect to somePage.php on the same tab
}
When the shortcut
contains value of /
, then it should be redirect to a new tab, else the same tab. How to do that? I know it's impossible to do that by using header();
, but I have no idea how to do that. Any ideas? Thanks.
当shortcut
包含 的值时/
,则应将其重定向到新选项卡,否则应重定向到相同的选项卡。怎么做?我知道使用 不可能做到这一点header();
,但我不知道该怎么做。有任何想法吗?谢谢。
PS: The form is intended to fill by a menu code, then it has to redirect the page based on the menu code filled on shortcut
(as in SAP). But if it contains a specific prefix, a different way should be implemented.
PS:表单旨在通过菜单代码填写,然后它必须根据填写的菜单代码重定向页面shortcut
(如在 SAP 中)。但是如果它包含特定的前缀,则应该采用不同的方式。
UPDATE
更新
Above the form, I've added this script:
在表单上方,我添加了这个脚本:
<script>
$("#frm").ajaxForm({
url: 'process.php', type: 'post'
});
</script>
And on the process.php
:
并在process.php
:
$gomenu = $_POST['shortcut'];
if(strpos($gomenu, "/") === 0) {
print "<script>
window.open('newTab.php', '_newtab');
</script>";
} else {
header('Location: somePage.php');
}
But then it opened in a new pop-up window. How to open it on the newtab?
但随后它在一个新的弹出窗口中打开。怎么在newtab上打开?
ANSWER (@fedmich's answer)
答案(@fedmich 的回答)
$(document).ready(function () {
$('frm#frm').submit(function(){
var open_new_tab = false;
var v = $('#shortcut').val();
if(v.match('/') ){
open_new_tab = true;
}
if ( open_new_tab ) {
$('frm#frm').attr('target', '_blank');
} else {
$('frm#frm').attr('target', '_self');
}
});
});
回答by Brad
Submit your form via AJAX, send the new URL in the response from PHP, and use JavaScript to open your new window, if your PHP script specifies that it should.
通过 AJAX 提交表单,在来自 PHP 的响应中发送新 URL,并使用 JavaScript 打开新窗口(如果您的 PHP 脚本指定它应该这样做)。
回答by fedmich
I think you could do it this way, put the target blankby default.
我认为你可以这样做,默认情况下将目标设置为空白。
<form id="frm" action="process.php" method="post" target="_blank">
</form>
then when submitting on the form submit() and modify & navigate away if you need to. you could just use javascript before submitting, change the action or targetattributes
然后在提交表单 submit() 并根据需要修改和导航时。您可以在提交之前使用 javascript,更改操作或目标属性
http://jsfiddle.net/fedmich/9kpMU
http://jsfiddle.net/fedmich/9kpMU
$('form#frm').submit(function(){
var open_new_tab = false;
var v = $('#shortcut').val();
if(v.match('/') ){
open_new_tab = true;
}
if ( open_new_tab ) {
alert('opening to new tab');
$('form#frm').attr('target', '_blank');
$('form#frm').attr('action', 'http://www.youtube.com');
}
else{
alert('opening to self_page');
$('form#frm').attr('target', '_self');
$('form#frm').attr('action', 'http://www.yahoo.com');
}
});
回答by Mohit Mehta
<?php
$gomenu = $_POST['shortcut'];
if(strpos($gomenu, "/") === 0)
{
echo "<script type='text/javascript'>window.open('newTab.php', '_blank')</script>";
}
else
{
echo "<script type='text/javascript'>window.open('newTab.php', '_parent')</script>";
}
?>