douganbi7686
2015-08-11 05:29
浏览 197
已采纳

html / javascript / php:我如何使用“全局变量”?

so my domain name is going to change from building it on my computer (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?

图片转代码服务由CSDN问答提供 功能建议

所以我的域名将从在我的计算机上构建它(localhost)到我的域名将是什么 当我实际部署它时,我不想回去并且必须在一百万个地方改变它。 现在我知道我可以在php中做一个超全局变量

&lt;?php echo $ GLOBALS ['domain_name']; ?&gt;

但据我所知,你不能在单独的.js文件中使用它。 如何为我的域名创建一个可以在html和js中使用的“全局变量”?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongranding3909 2015-08-11 05:32
    已采纳

    Print 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.com/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 overcomplicate if not needed.


    EDIT

    As @Amadan commented, 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?

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douji4223 2015-08-11 05:32

    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.

    评论
    解决 无用
    打赏 举报
  • duanji1924 2015-08-11 05:33

    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

    评论
    解决 无用
    打赏 举报
  • duanhu7400 2015-08-11 05:34

    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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题