【发布时间】:2013-02-05 15:44:40
【问题描述】:
我正在尝试传递标签
<script src="http://news.agc.org/agccorner/rss_feed.php?src=http%3A%2F%2Fnews.agc.org%2Fagccorner%2Frss_feed.php%3Ffdurls%255B%255D%3D4%26fdurls%255B%255D%3D5&num=5&targ=y&utf=y" type="text/javascript"></script>
我正在尝试传入通过 src 解析的 RSS Feed:
$str ='<script type="text/javascript">';
$str .= "document.write('<ul>');";
//each rss feed is gone through
foreach($rss_array as $each_rss) {
foreach($each_rss as $item){
//$num=1;
// if you want to channel title shown uncomment this
/*
if(@$item['title'])
echo "Channel Title: " . $item['title'];
*/
foreach($item as $record) {
//this statement is to not display the array that contains the title
if($record['title'] == "A") {
}else {
if($num <= $num_limit) {
$num++;
$str .= "document.write('<li> <a href='".$record['link']. "' target='".$new_target."'>" . $record['title']. "</a></li>');";
}
}
}
}
}
$str .= "document.write('</ul>');";
$str .= '</script>';
header("Content-type: application/x-javascript");
echo $str;
当您将该代码放在 html 页面上时,它不会显示任何信息,但是当您转到它自己的页面时,它将返回 rss 提要。
我尝试过使用 document.write 函数和 echo。
任何机构有什么建议吗?
<script type="text/javascript">document.write('<ul>');document.write('<li> <a href='http://news.agc.org/2013/01/28/house-ti-committee-finalizes-chair-assignments-chairman-shuster-meets-chairman-boxer-to-discuss-highway-trust-fund-concerns/?utm_source=rss&utm_medium=rss&utm_campaign=house-ti-committee-finalizes-chair-assignments-chairman-shuster-meets-chairman-boxer-to-discuss-highway-trust-fund-concerns' target='_blank'>House T&I Committee Finalizes Chair Assignments, Chairman Shuster Meets Chairman Boxer to Discuss Highway Trust Fund Concerns</a></li>');document.write('<li> <a href='http://news.agc.org/2013/01/25/states-look-to-increase-transportation-funding/?utm_source=rss&utm_medium=rss&utm_campaign=states-look-to-increase-transportation-funding' target='_blank'>States Look to Increase Transportation Funding</a></li>');document.write('<li> <a href='http://news.agc.org/2013/01/25/nebraska-approves-keystone-xl-pipeline-route/?utm_source=rss&utm_medium=rss&utm_campaign=nebraska-approves-keystone-xl-pipeline-route' target='_blank'>Nebraska Approves Keystone XL Pipeline Route</a></li>');document.write('<li> <a href='http://news.agc.org/2013/01/24/house-ti-committee-finalizes-chair-assignments/?utm_source=rss&utm_medium=rss&utm_campaign=house-ti-committee-finalizes-chair-assignments' target='_blank'>House Transportation and Infrastructure Committee Finalizes Chair Assignments</a></li>');document.write('<li> <a href='http://news.agc.org/2013/01/24/states-look-to-increase-transportation-funding-2/?utm_source=rss&utm_medium=rss&utm_campaign=states-look-to-increase-transportation-funding-2' target='_blank'>States Look to Increase Transportation Funding</a></li>');document.write('</ul>');</script>在php页面上
如果您使用 RSS 作为 echo 语句而不是 document.write,它将输出:
<ul><li> <a href='http://news.agc.org/2013/01/02/worker-shortage-in-construction-industry/?utm_source=rss&utm_medium=rss&utm_campaign=worker-shortage-in-construction-industry' target='_blank'>Worker Shortage in Construction Industry</a></li><li> <a href='http://news.agc.org/2012/12/18/15125/?utm_source=rss&utm_medium=rss&utm_campaign=15125' target='_blank'>Contractors Collaborate, Develop Guide to Streamline Development of Environmental Management Program</a></li><li> <a href='http://news.agc.org/2012/12/18/billing-index-continues-to-see-improvement/?utm_source=rss&utm_medium=rss&utm_campaign=billing-index-continues-to-see-improvement' target='_blank'>Billing Index Continues to See Improvement</a></li><li> <a href='http://news.agc.org/2012/12/12/new-changes-in-epa-policy-governing-disposal-of-pcb-contaminated-building-materials/?utm_source=rss&utm_medium=rss&utm_campaign=new-changes-in-epa-policy-governing-disposal-of-pcb-contaminated-building-materials' target='_blank'>New Changes in EPA Policy Governing Disposal of PCB-Contaminated Building Materials</a></li><li> <a href='http://news.agc.org/2012/12/04/consensusdocs-releases-updated-federal-subcontract-to-help-construction-firms-perform-federal-work/?utm_source=rss&utm_medium=rss&utm_campaign=consensusdocs-releases-updated-federal-subcontract-to-help-construction-firms-perform-federal-work' target='_blank'>ConsensusDocs Releases Updated Federal Subcontract to Help Construction Firms Perform Federal Work</a></li></ul>
使用 document.write 的 RSS 提要的链接是:
http://news.agc.org/agccorner/rss_feed.php?src=http%3A%2F%2Fnews.agc.org%2Fagccorner%2Frss_feed.php%3Ffdurls%255B%255D%3D4%26fdurls%255B%255D%3D5&amp;num=5&amp;targ=y&amp;utf=y
使用 php echo 链接到 rss 提要:
http://news.agc.org/agccorner/rss_feed_2.php?src=http%3A%2F%2Fnews.agc.org%2Fagccorner%2Frss_feed.php%3Ffdurls%255B%255D%3D4%26fdurls%255B%255D%3D5&amp;num=5&amp;targ=y&amp;utf=y
链接到html页面:
http://news.agc.org/agccorner/rss.htm
【问题讨论】:
-
好像你想用 AJAX 加载 PHP 文件。
-
我建议使用更好的解决方案,例如 AJAX,但无论哪种方式,它的输出是什么?即来源?
-
如果您发送的内容类型为
x-javascript,则不要包含<script>标签。 -
<script>标签是多余的。文件必须只包含 javascript。<script>- 是 html。如果 php 创建的 js-code 是正确的,那么它必须能够正常工作。
标签: php javascript html rss