echo php 变量可以在外部 javascript 上吗?
我(绝对的 php 初学者)得到了一个带有不同变量的脚本,这些变量基于 xhtml strict 页面顶部的日期和时间:
<?php
$var1="2011,9,31,18,0,0";
[...]
?>
在 html 正文中,我有一个 javascript,当前启动如下:
<script type="text"/javascript">
dateFuture = new Date(<?php echo $var1; ?>);
[...]
</script>
是否有可能使 javascript 外部,但仍然从索引页顶部拉出变量 $var1 ,然后让它在索引页上显示与当前相同的输出?
我发现一个示例,外部 .js 的开头应该如下所示:
dateFuture = new Date() ;
不幸的是,这似乎不起作用。 有什么可能的解决方案吗? 非常感谢任何帮助。 先感谢您。
I (absolute php beginner) was given a script with different variables that are based on date and time on the top of xhtml strict page:
<?php
$var1="2011,9,31,18,0,0";
[...]
?>
Inside the html body I have a javascript that currently starts like this:
<script type="text"/javascript">
dateFuture = new Date(<?php echo $var1; ?>);
[...]
</script>
Is it possible to make the javascript external, but still pull the variable $var1 from the top of the index page and then have it show the same output on the index page as it currently does?
I have found one example where the beginning of the external .js is supposed to look like this:
dateFuture = new Date(<?php include("/index.html");echo $var1;?>);
Unfortunately that doesn't seem to work.
Is there any possible solution for this?
Any help is greatly appreciated.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。将 javascript 变量设置为全局变量,您可以在外部 js 文件中访问它。
像这样
在 your-external-js-file.js 中,您可以访问 dateFuture。
或者您可以将外部 js 文件中的代码封装在一个类中,并将 php 中的日期作为参数传递给该类的构造函数。
Yes. Make the javascript variable global and you can access it inside you external js file.
Something like this
In your-external-js-file.js, you can access the dateFuture.
Or you can encapsulate the code in external js file in a class and pass on the date from php as a parameter to the constructor of that class.
外部 JavaScript 文件本身可以指向 PHP 文件 - 前提是 PHP 文件输出有效的 JavaScript。这样,您可以执行如下操作:
myJS.php:
在您的 HTML 文件中:
由于
myJS.php
的输出纯粹是 JavaScript 代码,因此文件扩展名对浏览器来说并不重要。与您的 PHP 代码当前输出纯 HTML 代码的方式相同,并且您的浏览器也了解如何解析它。The external JavaScript file itself can point to a PHP file — provided that the PHP file outputs valid JavaScript. That way, you can do something like the following:
myJS.php:
In your HTML file:
Since the output of
myJS.php
is purely JavaScript code, the file extension will not matter to the browser. Same way as your PHP code currently outputs purely HTML code, and your browser understands how to parse that as well.如果您的目的是将 javascript 代码移动到外部脚本以实现更好的模块化,则可以将其移动到 php 文件,然后将 php 文件引用为 javascript。
在 myscript.php 内部 -
If your purpose is to move the javascript code to an external script for better modularization, you can move it to a php file and then reference php file as javascript.
Inside myscript.php -
虽然 Jonathan Newmuis 和 RonakG 给出的答案完全可以接受并且有效,但此答案的目的是尽可能接近您现在的设置来回答您的问题。不过我个人同意 RonakG 的回答。
如果您在服务器上使用 Apache,则将以下行添加到您的
.htaccess
文件中:AddType application/x-httpd-php .js
。或者,如果您关心性能,您可以将该代码添加到 Apache 配置中。该代码的目标本质上是说 PHP 应该解析所有以“.js”结尾的文件,就好像它们是“.php”一样
Whilst the answers given by Jonathan Newmuis and RonakG are perfectly acceptable and will work, the purpose of this answer is to answer your question as close to the setup you've got now as possible. However I'd personally agree with RonakG's answer.
If you're using Apache on your server then add the following line to your
.htaccess
file:AddType application/x-httpd-php .js
. Alternatively you could add that code into the Apache configuration if performance is an concern for you.The goal of that code is, essentially, to say that PHP should parse all files ending in ".js" as if they were ".php"
是的。这是可能的。但是,您必须将 javascript 文件转换为 php 文件,或者强制 Apache 或您使用的任何 Web 服务器将 javascript 作为 php 运行(完全无害,因为
只是写入输出)。
js.js 中(无论您选择哪个,但我推荐前者,因为它需要较少的配置):
在
my-external-js.php
或my-external - :您应该始终使用
require_once
而不是include
或require
,这样同一个文件就不会被包含两次,从而导致变量混乱和冲突的函数/类。此外,如果脚本无法加载,require
和require_once
会出现致命错误,而include
和include_once
则不会。Yes. It is possible. However, you are going to have to make the javascript file into a php file, or force Apache, or whatever web server you use, to run javascript as php (perfectly harmless, because all code outside of
<?php ... ?>
is just written to output).In
my-external-js.php
ormy-external-js.js
(whichever you choose, though I would recommend the former, because it requires less configuration):Note: you should always use
require_once
instead ofinclude
orrequire
, so that the same file is never included twice, which leads to messed-up variables and colliding functions/classes. Also,require
andrequire_once
case Fatal errors if the script could not be loaded, whileinclude
andinclude_once
do not.