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?

  • 写回答

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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog