我想从php页面隐藏未使用的js和css文件。
有些js文件未被某些页面使用,所以我想隐藏它们。通过隐藏它们,我可以使网页更快,并在页面测试中获得好成绩。
我使用了简单的php代码来隐藏一个页面的代码。工作良好,该文件对于该页面是隐藏的,对于其他页面,代码不起作用。它显示的是文本而不是链接,就像文本区域中的文本一样。。
<?php
function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
$currentp=curPageName();
if($currentp=="main1.php"){
echo "";
} else {
//works
echo "<link type='"text/css'" rel='"stylesheet'" href='"../rws.css'" />";
/// not works
echo "<link type='"text/css'" rel='"stylesheet'" href='"http://img.y.com/rws.css'" />";
}
?>
该代码适用于/rws.css
,如果我使用http://url.com/rws.css
,则不会。
这里有一个例子:
<?php
function get_current_file_name () {
$Exploded_Data = explode(‘/’, $_SERVER['PHP_SELF']); //explode the path to current file to array
$Exploded_Data = array_reverse ($Exploded_Data); //reverse the arrays order cos php file name is always after last /
$CurrentPage = $Exploded_Data[0]; // assign the php file name to the currentpage variable
return $CurrentPage;
}
$CurrentPage = get_current_file_name ();
if($CurrentPage!="main1.php"){
echo('<link type="text/css" rel="stylesheet" href="style1.css" />');
}
else if$CurrentPage!="main2.php"){
echo('<link type="text/css" rel="stylesheet" href="style2.css" />');
}
?>
您可以使用__FILE__
而不是$_SERVER['PHP_SELF']
代码是否获得了正确的当前页面,并且正在进入ELSE?
您可以使用单引号和双引号,以确保您没有遗漏任何转义符。
if($currentp!="main1.php"){
echo('<link type="text/css" rel="stylesheet" href="http://img.y.com/rws.css" />');
}