disabling javascript codes using php?
is there a way to disable javascript codes in a particular page using certain php codes? I need to ensure that all the javascripts u开发者_开发问答sed in a page should not give any result (even errors) when run.
Why don't you just not send the javascript down for those particular pages?
You could throw PHP conditionals around the Javascript so they won't display on your page:
<script type="text/javascript">
<?php if($showJavascript): ?>
// executes the following function
myJavascriptFunc();
<?php endif; ?>
function myJavascriptFunc() {
}
</script>
And to resolve any issues from my comments:
<?php var showJavascript = <?php echo ($showJavascript) ? 'true' : 'false'; ?>;
<script type="text/javascript" src="myFile.js"></script>
In the last case you should check the boolean value of showJavascript in myFile.js.
The easiest way (and the dirtiest, slowest, etc) is to turn on the output buffer at the start of the page, and, before echoing the buffer's content at the end, remove all traces of javascript, either through regular expressions, a html parser, or a combination of both.
<?php
ob_start();
// your code here
$output = ob_get_contents();
ob_end_clean();
// Purge your $output here, remove all <script> tags, onclick events, etc.
echo $output;
?>
How to purge the output has already been answered on SO many, many times.
There is no such way because PHP run on your server and JavaScript runs client-side once your server has done it's part and it's done by browser.
What you can do is make sure that your JS doesn't have errors or remove all the JavaScript.
精彩评论