从php文件中定义js文件中的变量 [英] Defining a variable in a js file, from a php file

查看:77
本文介绍了从php文件中定义js文件中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript文件,需要来自调用JS文件的php脚本中的变量值。

I have a javascript file that needs a value from a variable in a php script in which the JS file is being called.

<?
$sid = session_id();
?>
<html>
<head>
<script src="javascript.js" type="text/javascript"></script>
...

如何将$ sid输入js文件以供使用?

How can i get $sid into the js file for use?

推荐答案

<html>
<head>
<script src="javascript.js" type="text/javascript"></script>
<script type="text/javascript">
    var myFile = 'myfile.php?sid=' + "<?php echo session_id() ?>";        
    $('#stuff').fadeOut("fast").load(myFile).fadeIn("fast");
</script>

包含的顺序在某种程度上取决于javascript.js中的内容。如果你在里面使用声明的'myFile'变量,先声明它。如果它只是一个lib,例如jQuery,然后声明它。

The order of inclusion somewhat depends on what is inside javascript.js. If you are using the declared 'myFile' variable inside, declare it first. If it's just a lib, e.g. jQuery, declare it afterwards.

这篇关于从php文件中定义js文件中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆