I’d like to auto_prepend_file all my js files with:
var SITE_ROOT = 'http://www.mydomain.com';
Can this be done in .htaccess like it can for php files, if so, how?!!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could accomplish what you’re seeking with a mod_rewrite rule and a small php file:
js_wrapper.php:
.htaccess:
You should modify the RewriteRule directive to reflect a different path if your scripts aren’t under a folder named ‘js’.
Alternatively…
Since in your example you’re merely defining a variable at the start, if this is all you’re looking to do, you should be able to accomplish this by using a
<script>block in your<head>block of the page including the js scripts. The js scripts will be able to access any variables you define there. This is much simpler, easier to maintain, uses less ‘magic’ (people reading the rendered html source and js won’t understand where the prepended data is coming from unless they read .htaccess), and is how I would probably do what you’re trying to accomplish.