简体   繁体   English

从单独的HTML / php文件中调用javascript变量

[英]Calling javascript variables from a separate HTML/php file

I have a separate javascript file(buy.js) from my html file(pay.php) that the html file posts to once a form is submitted. 我的HTML文件(pay.php)中有一个单独的javascript文件(buy.js),一旦提交表单,该HTML文件就会发布到该文件中。 However, there are some javascript variables in pay.php that are outside the scope of the form that I need to call in the separate javascript file (buy.js). 但是,pay.php中有一些javascript变量不在我需要在单独的javascript文件(buy.js)中调用的形式范围之内。 Is this possible to do? 这可能吗?

This is the snippet of javascript that I need to access in my html file (buy.js): 这是我需要在html文件(buy.js)中访问的javascript代码段:

  var planTerm = term;

This is the javascript in the html that I need the separate javascript file to access (pay.php): 这是html中的javascript,我需要单独的javascript文件才能访问(pay.php):

var term = 0;
var price = 0;

function updatePrice(e) {
     price = parseFloat(select.value);
    if (price == select.options[1].value){
        term = 12;
    } else if (price == select.options[2].value) {
        term = 1;
    }
}

Here is a Pastie of all the code. 这是所有代码的粘贴。

Using PHP you can simply do (using the include() function): 使用PHP,您可以简单地做到(使用include()函数):

include("File.php");

So, as my example goes, 所以,正如我的例子

The JS_INCLUDE.php File : The JS_INCLUDE.php File

<html>
<body>
  <script>
    var JSvar = "This is a external JS var.";
  </script>
</body>
</html>

The index.php

<html>
<body>
    <?php
    include("JS_INCLUDE.php");
    ?>

    <script>
    document.write(JSvar);
    </script>
</body>
</html>

But of course if the .php file is remote from the directory, you cannot. 但是,当然,如果.php文件远离目录,则不能。

Sources: 资料来源:

Include() function: http://www.php.net/manual/en/function.include.php Include()函数: http : //www.php.net/manual/zh/function.include.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM