gpt4 book ai didi

jquery - 有没有办法使用 AJAX 只加载一次页面?

转载 作者:行者123 更新时间:2023-12-01 00:59:29 25 4
gpt4 key购买 nike

页面加载后,我有一个 AJAX 调用来加载一个函数,该函数会打开一个类似框架的灯箱,其中包含一些数据。问题是,如果我单击关闭(灯箱框架),页面会一次又一次加载框架,因此使用永远不会到达框架层下的页面,因为框架加载无限循环。 Ajax 正在重复调用我猜想的函数,但我想加载一次框架,当用户单击 X(关闭)时,他可能会返回到原始页面。

$(document).ready(function(){
var city = $('#citycode').html();

$.ajax({
//when page is loaded, fire the following function
success: function(){
//the function to be fired located in the page in seperate file
openX(city + //some other parameters);
}});


});

有什么建议吗?

最佳答案

您可以在页面顶部设置一个全局变量,并在页面加载后将其设置为 true,然后在代码中检查该变量是否为 false加载页面而不是其他方式。这是原型(prototype):

<script type="text/javascript">

var loaded = false;

$(document).ready(function(){
var city = $('#citycode').html();

$.ajax({
//when page is loaded, fire the following function
success: function(){
if (loaded === false)
{
//the function to be fired located in the page in seperate file
openX(city + //some other parameters);
loaded = true;
}
}});


});
</script>

关于jquery - 有没有办法使用 AJAX 只加载一次页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2753053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com