javascript - 防止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>
解决方案
问题是,您将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>";
推荐阅读
- php - mongodb用正则表达式匹配完整字符串
- angularjs - 如何验证具有不同字段的表内的模板驱动表单?
- c - 无法在 costum Buildroot 上执行 C 二进制文件
- reactjs - ReactJS:在悬停事件的单个元素中添加类
- android - 唤醒设备,一旦进入打盹模式
- spring - 缺少返回语句以使该方法起作用
- select - 如何在不使用 GROUP BY 子句的情况下获取列值
- c# - 从 C# 到 C++ 触发 COM 事件的正确方法是什么
- css - 更改 md-subheader-inner css 属性
- r - 单击“运行”按钮时闪亮的崩溃 - 但仅在某个选项卡中启动时