外部js文件 .js 怎么调用php文件中的变量

WBOY
Release: 2016-06-13 10:34:09
Original
1971 people have browsed it

外部js文件 .js 如何调用php文件中的变量
假如有一个init.js文件,想要调用index.php文件中的$language变量,该怎么用,谢谢

------解决方案--------------------
将$language写入html页面,如

=$language?>

js获取:var lang = document.getElementById("lang").innerHTML;
------解决方案--------------------
其实js和html,css是一样的东西,你完全可以让外部js文件由php动态生成。



但是你的需求有点绕,就是想要index.php里计算得到的language,那么其实只要index.php将这个变量放在index.php生成页面的某个位置(script标签里的一个变量/任意dom node的一个属性),让init.js在onload之后去取来用就行了。
------解决方案--------------------
方法有的是,关键是你那边需求合不合适。

你甚至可以存在session里,然后所有js文件都做成php的,让js.php读session。

再简单点,做个独立的php接口,AJAX直接去拿SESSION里的数据。

再简单到死,直接index.php发COOKIE,JS直接从document.cookie里拿值。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!