简体   繁体   English

直到刷新后变量才加载到javascript中

[英]Variables not loading into javascript until refresh

I've a strange situation in which I echo php variables into javascript to use it with jquery. 我有一种奇怪的情况,我将php变量回显到javascript中,以与jquery一起使用。 However, the variables are not defined when I open the page for the first time, and when I refresh the page or just go to the same link again, the variables are loaded. 但是,当我第一次打开页面时,并没有定义变量,并且当我刷新页面或只是再次进入相同的链接时,将加载变量。

Echoed in the javascript it looks like 在javascript中回显

   <script type="text/javascript">
            $( document ).ready(function() {
                console.log('test');
                window.GlobalVar = {show_table : "<tr><td>%.0f</td><td>%s</td><td>&#8364; %0.02f</td><td>&#8364; %0.02f</td><td><a href=''><img class='delete_task' src='/brugginkadmin/public_html/images//icons/delete.png' data-id='%s'></a></td><td><input type='checkbox' name='delete_query' value='%s'></td></tr>"};
            });
   </script>

And in later jquery functions they are not reconized, until refresh. 并且在以后的jquery函数中,直到刷新后才对它们进行协调。

I've included jquery before these scripts. 在这些脚本之前,我已经包含了jQuery。

In conclusion: The problem is that this variable window.GlobalVar.show_table is not defined later on in the javascript, however after hitting refresh, it is. 结论:问题在于此变量window.GlobalVar.show_table稍后在javascript中未定义,但是在刷新后才定义。

Echoing PHP inside script tags causes odd and inconsistent behaviour. 在脚本标签内回显PHP会导致奇怪且不一致的行为。 Build the entire script, tags and all, in php then echo to the page and see if that works better. 在php中构建整个脚本,标签和所有内容,然后回显该页面,看看效果是否更好。

var script = <<<SCR
    <script type="text/javascript">
            $( document ).ready(function() {
                console.log('test');
                window.GlobalVar = {show_table : "<tr><td>%.0f</td><td>%s</td><td>&#8364; %0.02f</td><td>&#8364; %0.02f</td><td><a href=''><img class='delete_task' src='/brugginkadmin/public_html/images//icons/delete.png' data-id='%s'></a></td><td><input type='checkbox' name='delete_query' value='%s'></td></tr>"};
            });
   </script>
SCR;
echo script;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM