首页 > 解决方案 > 防止php在新标签/窗口中打开

问题描述

我有在新选项卡中打开 XML 链接的 PHP。如何防止这样的 PHP 文件在新选项卡中打开 XML 链接,以便每当我单击搜索结果时,链接总是在同一个选项卡上打开?以及如何设置其下拉菜单的样式以删除搜索结果周围的边框以及字体颜色的样式?

PHP file

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");

$x=$xmlDoc->getElementsByTagName('link');

//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)->getElementsByTagName('title');
    $z=$x->item($i)->getElementsByTagName('url');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
          $hint="<a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        } else {
          $hint=$hint . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?>

链接.xml

<link>
<title>JavaScript Date Object</title>
<url>https://www.w3schools.com/jsref/jsref_obj_date.asp</url>
</link>
<link>
<title>JavaScript Array Object</title>
<url>https://www.w3schools.com/jsref/jsref_obj_array.asp</url>
</link>
<link>
<title>Jordan Barton</title>
<url>https://www.w3schools.com/cssref/pr_border.asp</url>
</link>
<link>
<title>Adduli Khan</title>
<url>https://www.w3schools.com/jsref/jsref_obj_date.asp</url>
</link>
<link>
<title>James Andrew</title>
<url>https://www.w3schools.com/jsref/jsref_obj_array.asp</url>
</link>
</pages>

标签: javascriptphp

解决方案


问题是,您将target="_blank"属性添加到搜索结果中。这会强制创建一个新选项卡,甚至是一个新的浏览器窗口。

换这个就行了...

$hint="<a href='" .
      $z->item(0)->childNodes->item(0)->nodeValue .
      "' target='_blank'>" .
      $y->item(0)->childNodes->item(0)->nodeValue . "</a>";

……到这……

$hint="<a href='" .
      $z->item(0)->childNodes->item(0)->nodeValue .
      "'>" .
      $y->item(0)->childNodes->item(0)->nodeValue . "</a>";

推荐阅读