0

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?

4
  • Why do it like this? PHP is like HTML on steroids. I'd make a header.php where you include your script.js conditionally and then require 'header.php' where needed. Write your JavaScript in JavaScript . Commented Jan 6, 2014 at 16:55
  • Because it requires ultimately two includes in main.php page, first one is header.php and second one is script.js into that header.php. I want to reduce the number of includes per php page as my site is in shared hosting and it has limits of concurrent number of php script/javascript per minute. So, instead of calling script.js from another file, I want to declare the script.js as function parameter. Commented Jan 6, 2014 at 17:01
  • If you're worried about that, then I think the best you can do is minify all your scripts into one, and load it in the body, not the header, then do whatever logic in JavaScript. This is common practice. Commented Jan 6, 2014 at 17:03
  • This would be a better solution. But what about jquery codes which I have to load in head section because some jquery codes depend on page load. Commented Jan 6, 2014 at 17:11

2 Answers 2

1

If you just want an easier why to define a multi-line variable, use NOWDOC:

$javascript  = <<<'EOD'
<script type="text/javascript" language="javascript">
document.oncontextmenu = function() {
    return false;
}
window.onbeforeunload = function() {
        return "Please don't reload this page.";
    }
</script>
EOD;

Although it might be easier to write the javascript in an external .js-file and pass the filename to include instead.

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

2 Comments

I did not listen 'nowdoc' method before. Would you please explain me about this method or would you give me link from which I can know much about this method?
I got the article regarding NOWDOC and HEREDOC. I think it is the best solution, as it does not require any symbol escaping. Thanks Jan Traenkner.
1

PHP supports multiline strings, so you can simply white:

$javascript = '
    <script type="text/javascript" language="javascript">
    document.oncontextmenu = function() {
       return false;
    }
    window.onbeforeunload = function() {
       return "Please don\'t reload this page.";
    }
    </script>
';

And don't forget to escape all ' symbols.

But I think the best decision would be keep js-code in a js-file

1 Comment

Thanks, I just would want to know whether php supports multiline strings or not and how to write those multiline strings, which you told clearly. I want to reduce the include numbers because of my site is in shared hosting and there is limit of concurrent running scripts per minute.

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.