0

Is there a way to serve or add a js file to the code php/html pages when the site is loaded. I have 500+ pages and I am looking for a way to add a small snippet of code to all the pages.

e.g. onload www.xyz.com - add javascript file/snippet to that page on runtime

The idea is to add a javascript snippet on the page loaded so it wont be server side it has to be client side.

And the code provided below still needs me to add the checking script on each page.

I need to know if there is a way to serve the file thru apache on site load ?

2
  • Do you mean "include()" from php? You can put that in your header pointing to your file you want to add...? (There is also "require()" available) Commented Oct 23, 2012 at 18:00
  • Do you want to add the file at server-side runtime or client-side runtime. Commented Oct 23, 2012 at 18:01

4 Answers 4

2

Assuming you have static pages with <html><head></head><body>whatever</body></html> kind of stuff. The following should work and be a quick fix. My method modifies the bottom closing </body> tag but you can modify it to alter the <head> or opening <body> tag

.htaccess

<IfModule php5_module>
    php_value auto_prepend_file /var/www/vhosts/example.com/httpdocs/auto_prepend.php
    php_value auto_append_file  /var/www/vhosts/example.com/httpdocs/auto_append.php
</IfModule>

auto_prepend.php

<?php 
ob_start();
?>

auto_append.php

<?php 
$output = ob_get_clean();
$script = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/script.inc');
$output = str_replace('</body>', $script.PHP_EOL.'</body>', $output);
echo $output;
?>

script.inc

<script>
// using jquery method to attach onload:
jQuery(function(){
    // do your js stuff here or load your external js, like with jQuery.getScript() or jQuery.ajax()
    // http://api.jquery.com/jQuery.getScript/
    // http://api.jquery.com/category/ajax/
});

// or if you want go without a framework and attach your onload event. 
// in the most cross browser way see: 
// http://stackoverflow.com/questions/1235985/attach-a-body-onload-event-with-js

</script>
Sign up to request clarification or add additional context in comments.

Comments

0

yes you can. Look how they loaded jquery dynamically: Loading jQuery on-the-fly

a typical use case: jqueryfy http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet

1 Comment

This is probably something better solved server-side in the PHP than in Javascript.
0

You may call a function, that calls script in body onload attribute

<body onload="addScript()">
<script type="text/javascript">
function addScript()
{
    document.write("<script src='path\/to\/script.js' type=\"text/javascript\"><\/script>");
}
</script>

2 Comments

yes this is right but i still need to add this to all the body tags of the 500+ pages is there a way to automate this
You may write a script to alter you body tag, add onload attribute and add script section before closing </html> on all your 500+ pages
-1

Use the Anthony Hatzopoulos solution, above.

ob_start();

in prepend and in append(via htaccess or php.ini) use:

<?php
$output = ob_get_clean();
$yourcode = "<yourscript/></body>";
$output = str_replace('</body>', $yourcode, $output);
echo $output;
?>

This is the best solution for me.

Comments

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.