从Jquery加载的网页获取变量

时间:2023-02-05 23:50:14

I am using the jquery.load to load a div back into the same page then refresh the div to reflect any changes. This works fine until I need a variable from the page I am loading to use within the javascript. For example, if localStorage.username2 changes the browser should reload. But it is not.

我使用jquery.load将div加载回同一页面,然后刷新div以反映任何更改。这工作正常,直到我需要从我加载的页面中的变量在javascript中使用。例如,如果localStorage.username2发生更改,则应重新加载浏览器。但事实并非如此。

<script type="text/javascript">
var partyid = '<?php echo $_GET["Party_ID"]; ?>';
jQuery(document).ready( function($){

if (localStorage.user2==localStorage.username2){

$('#Submission-2').load('http://example.com/?Party_ID=' + partyid + ' #Submission');
refresh();
}

});

</script>

<script type="text/javascript">
function refresh() 
{ 
 setTimeout( function() { 
if (localStorage.user2==localStorage.username2)
{
 $('#Submission-2').fadeOut('slow').load('http://example.com/?Party_ID=' + partyid + ' #Submission').fadeIn('slow'); 
document.getElementById("Submit-answer").innerHTML = localStorage.username2;
refresh();
}
else{
window.location.reload();
}

document.getElementById("Submit-answer").innerHTML = localStorage.username2;
 }, 5000); 
} 
</script>

This code works until the username changes and the code does not detect it. So the window.location.reload(); function is never executed.

此代码有效,直到用户名更改并且代码未检测到它。所以window.location.reload();函数永远不会执行。

I also have this code in my Submission div.

我的Submission div中也有这个代码。

 <div id="submission">

localStorage.setItem("user2", "<?php echo $user; ?>");
localStorage.setItem("username2", "<?php echo $party_information[0]->Username; ?>");

</script>
</div>

So my issue is how do i get a variable from a page loaded using .load? I am quite new to this only started a few days ago. Thanks in advance.

所以我的问题是如何从使用.load加载的页面中获取变量?我刚刚开始几天前对这个很新。提前致谢。

3 个解决方案

#1


0  

Something like the following should fire your $('#Submission-2).load(...); refresh(); functions when local storage changes.

像下面这样的东西应该激活你的$('#Submission-2).load(...);刷新();本地存储更改时的功能。

$(window).bind('storage', function (e) {
     $('#Submission-2').load('http://example.com/?Party_ID=' + partyid + ' #Submission');
    refresh();
});

#2


0  

you can set an interval that fires periodically to check if the variable has changed or not

您可以设置定期触发的间隔,以检查变量是否已更改

window.setInterval(function(){
    if (localStorage.user2 != localStorage.username2){
       window.location.reload();
    }
}, 5000);

This runs every 5 second to check if the variable has changed. Note that it could be pretty taxing

每5秒运行一次以检查变量是否已更改。请注意,这可能非常费力

#3


0  

This is what I ended up doing for the js. Works!

这就是我最终为js做的事情。作品!

<script type="text/javascript">
function refresh() 
{ 
var quitfunction = 0;
 setTimeout( function() { 
$('#Submission-2').fadeOut('slow').load('http://example.com/play-with-friends/?Party_ID=' + partyid + ' #Submission' , function() {
var username = '<?php echo $party_information[0]->Username; ?>';
if (document.getElementById('total').value!=username)
{
window.location.reload();
}
        });
$('#Submission-2').fadeIn('slow').load('http://example.com/play-with-friends/?Party_ID=' + partyid + ' #Submission' );
{
refresh();
}
 }, 5000); 
}
</script>

#1


0  

Something like the following should fire your $('#Submission-2).load(...); refresh(); functions when local storage changes.

像下面这样的东西应该激活你的$('#Submission-2).load(...);刷新();本地存储更改时的功能。

$(window).bind('storage', function (e) {
     $('#Submission-2').load('http://example.com/?Party_ID=' + partyid + ' #Submission');
    refresh();
});

#2


0  

you can set an interval that fires periodically to check if the variable has changed or not

您可以设置定期触发的间隔,以检查变量是否已更改

window.setInterval(function(){
    if (localStorage.user2 != localStorage.username2){
       window.location.reload();
    }
}, 5000);

This runs every 5 second to check if the variable has changed. Note that it could be pretty taxing

每5秒运行一次以检查变量是否已更改。请注意,这可能非常费力

#3


0  

This is what I ended up doing for the js. Works!

这就是我最终为js做的事情。作品!

<script type="text/javascript">
function refresh() 
{ 
var quitfunction = 0;
 setTimeout( function() { 
$('#Submission-2').fadeOut('slow').load('http://example.com/play-with-friends/?Party_ID=' + partyid + ' #Submission' , function() {
var username = '<?php echo $party_information[0]->Username; ?>';
if (document.getElementById('total').value!=username)
{
window.location.reload();
}
        });
$('#Submission-2').fadeIn('slow').load('http://example.com/play-with-friends/?Party_ID=' + partyid + ' #Submission' );
{
refresh();
}
 }, 5000); 
}
</script>