echo php 变量可以在外部 javascript 上吗?

发布于 2024-12-07 15:32:53 字数 578 浏览 1 评论 0原文

我(绝对的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

药祭#氼 2024-12-14 15:32:53

是的。将 javascript 变量设置为全局变量,您可以在外部 js 文件中访问它。
像这样

<script type="text/javascript">
  var dateFuture = new Date(<?php echo $var1; ?>);
</script>
<script src="your-external-js-file.js" type="text/javascript"></script>

在 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

<script type="text/javascript">
  var dateFuture = new Date(<?php echo $var1; ?>);
</script>
<script src="your-external-js-file.js" type="text/javascript"></script>

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.

妞丶爷亲个 2024-12-14 15:32:53

外部 JavaScript 文件本身可以指向 PHP 文件 - 前提是 PHP 文件输出有效的 JavaScript。这样,您可以执行如下操作:

myJS.php:

<?php

// Initialize your PHP variable(s) here, or include the PHP script(s) to do so.
$var1="2011,9,31,18,0,0";

...

?>

dateFuture = new Date(<?php echo $var1; ?>);

在您的 HTML 文件中:

<script type="text/javascript" src="myJS.php"></script>

由于 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:

<?php

// Initialize your PHP variable(s) here, or include the PHP script(s) to do so.
$var1="2011,9,31,18,0,0";

...

?>

dateFuture = new Date(<?php echo $var1; ?>);

In your HTML file:

<script type="text/javascript" src="myJS.php"></script>

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.

悟红尘 2024-12-14 15:32:53

如果您的目的是将 javascript 代码移动到外部脚本以实现更好的模块化,则可以将其移动到 php 文件,然后将 php 文件引用为 javascript。

<script type="text/javascript" src="myscript.php"></script>

在 myscript.php 内部 -

<?
Header("content-type: text/javascript");
dateFuture = new Date(<?php echo $var1; ?>);
?>

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.

<script type="text/javascript" src="myscript.php"></script>

Inside myscript.php -

<?
Header("content-type: text/javascript");
dateFuture = new Date(<?php echo $var1; ?>);
?>
如痴如狂 2024-12-14 15:32:53

虽然 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"

云淡月浅 2024-12-14 15:32:53

是的。这是可能的。但是,您必须将 javascript 文件转换为 php 文件,或者强制 Apache 或您使用的任何 Web 服务器将 javascript 作为 php 运行(完全无害,因为 只是写入输出)。

js.js 中(无论您选择哪个,但我推荐前者,因为它需要较少的配置):

<?php require_once 'file-which-defines-var1.php'; ?>

dateFuture = new Date("<?php print $var1; ?>");

my-external-js.phpmy-external - :您应该始终使用 require_once 而不是 includerequire,这样同一个文件就不会被包含两次,从而导致变量混乱和冲突的函数/类。此外,如果脚本无法加载,requirerequire_once 会出现致命错误,而 includeinclude_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 or my-external-js.js (whichever you choose, though I would recommend the former, because it requires less configuration):

<?php require_once 'file-which-defines-var1.php'; ?>

dateFuture = new Date("<?php print $var1; ?>");

Note: you should always use require_once instead of include or require, so that the same file is never included twice, which leads to messed-up variables and colliding functions/classes. Also, require and require_once case Fatal errors if the script could not be loaded, while include and include_once do not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文