如何为外部javascript文件设置变量? [英] how to set a variable for external javascript file?
本文介绍了如何为外部javascript文件设置变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想弄清楚如何设置我的 session_id 变量以在外部 js 文件中使用.这是我所拥有的.
I am trying to figure out how to set my session_id variable to to be used in an external js file. Here is what I have.
php 文件:
$output = '<script src="test.js">
var session_id = '.$_SESSION['id'].'
</script>';
print $output;
js 文件:
alert(session_id);
我收到一个错误,说 session_id 没有定义.有没有办法传递这些信息?
With this I am getting an error saying session_id is not defined. Is there a way to pass this info across?
我知道我可以用信息设置一个隐藏的输入,但我认为有更好的方法.
I know I could just setup a hidden input with the info but I am thinking there is a better way of doing it.
推荐答案
嗯,怎么样:
$output = '<script type="text/javascript> var session_id = '.$_SESSION['id'].'</script><script type="text/javascript" src="test.js"></script>';
print $output;
?
这篇关于如何为外部javascript文件设置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文