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 ?

iamvaruns
  • 83
  • 1
  • 7
  • 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) – Landjea Oct 23 '12 at 18:00
  • Do you want to add the file at server-side runtime or client-side runtime. – Waleed Khan Oct 23 '12 at 18:01

4 Answers4

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>
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
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

Community
  • 1
  • 1
skyrail
  • 139
  • 1
  • 7
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>
Gtx
  • 231
  • 1
  • 10
  • 17
  • 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 – iamvaruns Oct 23 '12 at 18:07
  • You may write a script to alter you body tag, add onload attribute and add ```script``` section before closing ``` – Gtx Oct 23 '12 at 18:12
-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.