admin管理员组文章数量:1296310
so my domain name is going to change from building it on my puter (localhost) to what my domain name will be when i actually deploy it, and I don't want to go back and have to change it in a million places. Now i know I could do a superglobal variable in php
<?php echo $GLOBALS['domain_name']; ?>
but as far as I know you can't use this in separate .js files. How can I create a "global variable" for my domain name that can be used in html and js?
so my domain name is going to change from building it on my puter (localhost) to what my domain name will be when i actually deploy it, and I don't want to go back and have to change it in a million places. Now i know I could do a superglobal variable in php
<?php echo $GLOBALS['domain_name']; ?>
but as far as I know you can't use this in separate .js files. How can I create a "global variable" for my domain name that can be used in html and js?
Share Improve this question edited Aug 11, 2015 at 5:35 user2864740 62k15 gold badges157 silver badges227 bronze badges asked Aug 11, 2015 at 5:29 Geore ShgGeore Shg 1,3295 gold badges24 silver badges39 bronze badges 4- 1 As a note, you can create a header file where you declare all your default (PHP) load stuff first, including your domain name. This will help you avoid globals, and it's nicer to have all of that same old functionality seperated. – Bono Commented Aug 11, 2015 at 5:32
- 1 Why aren't you using relative links within your pages? Then it doesn't matter where the files are actually located. – user2417483 Commented Aug 11, 2015 at 5:34
- 1 Is it to be a PHP global or a JavaScript global? (HTML has no variables.) – user2864740 Commented Aug 11, 2015 at 5:36
-
1
@jeff: Sometimes relative links are not an option. For example, if you have a layout template that is processed in both
/
and/foo/bar
URLs, and it contains stylesheet/JS/font/image references, there is no way to do it without absolute links (or counting where your caller code is right now, which is even messier than just knowing your base URL). – Amadan Commented Aug 11, 2015 at 5:40
5 Answers
Reset to default 2Print in head of index file (or in php include in header tags)
Any js file can use variables defined in same scope level above it, even if in other files.
http://www.w3schools./js/js_scope.asp
Common form is to, inside the tag and at before any other calls, to define all 'global' js variables.
Printing php into a js file is discouraged highly for a few reasons, even though i have heard its possible, but just use a php include if you need it in multiple places:
<html>
<?php
// include any shared content, in this case i include entire <head> section
include('header.php');
?>
<body>
So if you have something like this at top of your php index file, or in an include you place there:
<script>
var baseUrl = "<?php echo $GLOBALS['domain_name']; ?>";
</script>
Then that can be used as a standard variable anywhere after that point.
Side Note: using object wrapper
As a side note, you could wrap it in an object and pass that object through to other files, such as:
var AppData = {};
AppData.domain_name = "<?php echo $GLOBALS['domain_name']; ?>";
AppData.googleTrackerUID = "<?php echo $GLOBALS['gtrackerid']; ?>";
AppData.whatever = true;
fnct_from_earlier_point_or_elsewhere(AppData);
Thats only really of use if you think you may need to pass any other settings through, or just want to build for that possibility (especially if optional param, as easy to make reusable function that handles if an index doesnt exist)
I prefer the object approach, but dont overplicate if not needed.
EDIT
As @Amadan mented, it would even be easier to create the $AppData array/object in php then just call this:
var AppData = <?php echo json_encode($AppData); ?>;
Also, regarding someone posting to omit the 'var' keyword, its usually bad form as var doesnt stop it being global, and some issues with certain browsers (IE)
javascript var or not var, what's the difference?
You can inject any PHP value into clientside.
<script>
var baseURL = <?php echo json_encode($baseURL); ?>;
</script>
Use json_encode
because this gives foolproof safe way of delivering data in the format JS understands, and works on real arrays, PHP "arrays", numbers, strings, whatever you want.
try this:
<script>
window.yourGlobalVariable = ....
</script>
by using you window it will be available for all your site
see: Define global variable in a JavaScript function
you can have a file like const.php
define('ROOT','http://127.0.0.1/');
and than use everywhere so will only need to change value of ROOT
Never too late!
Usually, it is not a good practice to use php in a js script.
Instead of using ...
<script>
var baseURL = <?php echo json_encode($baseURL); ?>;
</script>
... you might consider using a data- attribute like:
<script data-base-url="<?=json_encode($baseURL)?>">
let baseURL = document.currentScript.getAttribute("data-base-url");
</script>
Alternatively, you can include the above js code in any separate js script and just add the data-base-url attribute to the script tag which inserts this script into your HTML.
本文标签: htmljavascriptphp how do i use a quotglobal variablequotStack Overflow
版权声明:本文标题:htmljavascriptphp: how do i use a "global variable"? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741631242a2389378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论