0

I would like to hide the unused js and css files from the php page.

Some of the js files are unused by some pages, so I would like to hide them. By hidding them I can make webpage faster and get good grade in pagetest.

I used simple php code to hide the code for one page. Works good, the file is hidden for that page, for other pages the code not works. It is displaying text instead of link , like a text in textarea..

<?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\"     />"; 
    }
?>

The code works for /rws.css, if I use a http://url.com/rws.css it wont.

1
  • 1
    valid html would be a great start. Commented Jul 16, 2012 at 23:32

2 Answers 2

1

Here an example :

<?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" />'); 
}


?>

You can use __FILE__ instead of $_SERVER['PHP_SELF']

Sign up to request clarification or add additional context in comments.

1 Comment

the code still showing the plan text if i use img.y.com/rws.css, it only works without http://
1

Is the code getting the correct current page and it's entering the ELSE?

you can use singles quotes and inside double quotes to make sure you're not missing any escape.

if($currentp!="main1.php"){
  echo('<link type="text/css" rel="stylesheet" href="http://img.y.com/rws.css" />'); 
}

3 Comments

it not works.. still the code is not executable . it looks in plan text
it only works for path /rws.css. but if i use path with url/rws.css it wont
where are you "echo"ing this text? can you share you full code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.