For some reasons, I have to set page head section from another include page. First look at inc.php page below:
page: inc.php
<?php
function sethead($javascript){
echo '<html><head>';
echo '<link rel="stylesheet" type="text/css" href="site.css" />';
echo '<script type="text/javascript" src="jquery1.9.1.js"></script>';
echo $javascript;
echo '</head>';
}
?>
The inc.php page contains sethead() function which will set head for each page like this:
page: main.php
<?php
include('inc.php');
$javascript = '<script type="text/javascript" language="javascript">.......</script>';
sethead($javascript);
<body>
Here body codes go here
.....................
.....................
.....................
</body>
</html>
?>
The main.php page could be any page, like secondpage.php, thirdpage.php .... and so on. But as the javascript for each page might be different, so I have to echo this javascript from the function parameter of sethead($javascript). But I could not find a suitable way to declare this javascript parameter in main.php page before calling the function sethead($javascript) because the javascript code consists of several line of codes which are difficult to put in a variable.
<script type="text/javascript" language="javascript">
document.oncontextmenu = function() {
return false;
}
window.onbeforeunload = function() {
return "Please don't reload this page.";
}
</script>
How can I put the above lines of javascript code in a variable/function parameter ($javascript) in a easy way?
header.phpwhere you include yourscript.jsconditionally and thenrequire 'header.php'where needed. Write your JavaScript in JavaScript .