You can achieve this by making
<!-- first.tpl -->
<script>
```
$(document).ready(function() {
objIns.loadNames = '{$names|json_encode}';
}
)
```
</script>
{include file="second.tpl"}
<!-- end of first.tpl -->
<!-- second.tpl -->
some code here
<script>
```
$(document).ready(function() {
objIns.loadNames = '{$names|json_encode}';
})
```
</script>
objIns as a global variable.Declaring variables inside the function will make there scope local, so declare variables outside the function so that it can be used globally.
in fileAdd these lines inside document ready function
<script>
var objIns;
$(document).ready(function() {
objIns.loadNames = '{$names|json_encode}';
</script>
{include file="second.tpl"}
In first.tpl file
Access storagedata in second.tplclickobjIns variable before $(document).ready function in first.tpl file as global
var objIns='';
我有以下的代码结构。
我的问题是,为什么我需要声明变量 javascript localstorage
objIns.loadNamesfirst.tpl
在second.tpl文件中,因为它已经在first.tpl文件中加载了?有什么办法可以不在second.tpl中重新声明这个变量。我试过用 "var "关键字来声明,但是没有用。
objIns.loadNames = '{$names|json_encode}';
localStorage.setItem("objIns",objIns.loadNames);
我的代码结构如下。
localStorage.getItem("objIns");
$(document).ready(function() { objIns.loadNames = '{$names'}。