Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中刷新当前页面,然后调用jQuery函数_Javascript_Jquery_Page Refresh - Fatal编程技术网

如何在javascript中刷新当前页面,然后调用jQuery函数

如何在javascript中刷新当前页面,然后调用jQuery函数,javascript,jquery,page-refresh,Javascript,Jquery,Page Refresh,单击a.format24时,我希望重新加载页面,然后.fadeOut()一个元素并更改一些文本。显然,下面的代码设置方式是错误的,因为页面重载和jQuery函数同时发生。通过location.reload()运行jQuery函数不起作用,所以我没有主意了 $(document).ready(function() { $("a.format24").toggleClick(function() { location.reload();

单击
a.format24
时,我希望重新加载页面,然后
.fadeOut()
一个元素并更改一些文本。显然,下面的代码设置方式是错误的,因为页面重载和jQuery函数同时发生。通过
location.reload()
运行jQuery函数不起作用,所以我没有主意了

$(document).ready(function() {
        $("a.format24").toggleClick(function() {
            location.reload();
            $("div.toggleWrap").fadeOut();
            $(this).text("Use am/pm format");
            }, function() {
            location.reload();
            $("div.toggleWrap").fadeIn();
            $(this).text("Use 24-hour format");
        });
});

解决此问题的通常方法(如果您必须实际重新加载页面)是向URL添加一个参数,然后加载该URL。因此,如果您的原始URL是
http://example.com/play.html
,然后加载URL:
http://example.com/play.html?special=1

页面上的启动代码将在URL的查询参数中检查
special=1
,如果发现了,它将在页面加载时启动特殊行为

显然,您必须提前计划要支持的特殊页面加载行为,但这是非常可行的


好的,假设第二次加载页面时,您希望a.format24文本发生更改。我们将设计它,使URL中的
time=24
表示24小时格式,URL中的
time=12
表示12小时am/pm格式。如果两者都不存在,则默认为12小时格式

处理URL中的搜索参数时,需要处理三个条件:

  • 没有搜索参数
  • 有一些搜索参数,但不是您要查找的参数
  • 有一些搜索参数,包括您要查找的参数
  • 根据您找到的条件,您必须以不同的方式使用所需的搜索参数构造新的URL。这就是为什么下面的代码有三个不同的分支来构造URL

    // initialize the time format based on what's in the search parms of the URL
    $(document).ready(function() {
        var format24 = /time=24/.test(document.location.search);
        if (format24) {
            $("a.format24").data("format", 24).hide().text("Use 24-hour format").fadeIn();
        }
    });
    
    $(document).ready(function() {
        // upon click, switch to the other time format in the URL
        $("a.format24").click(function() {
            var self = $(this), newFormat;
            // if currently in 24 hour mode
            if (self.data("format") == 24) {
                newFormat = "time=12";
            } else {
                newFormat = "time=24";
            }
            var newUrl = location.href;
            // if no search value yet in the URL
            if (!window.location.search) {
                // no parms on the URL at all so
                // just add ?time=24 to the end of the URL
                newUrl += "?" + newFormat;
            } else if (window.location.search.indexOf("time=") !== -1) {
                // replace any existing format with the new one
                newUrl = newUrl.replace(/time=\d+/, newFormat);
            } else {
                // some search params exist, but not the time format 
                // so add onto the end after the other search params
                newUrl += "&" + newFormat;
            }
            self.fadeOut(500, function() {
                window.location = newUrl;
            });
        });
    });
    
    工作演示:



    另一种方法是根本不重新加载页面,只使用DOM操作在不重新加载的情况下根据需要更改页面。

    为什么需要重新加载页面?您正在设置cookie吗?我正在尝试使用页面重新加载作为一种避免对由极不可能的用例导致的计算错误进行故障排除的方法。最终,该网站将重建,所以应用绷带是所有的时间,我想投资。如何向url添加参数?@UncleSlug-
    window.location.href
    提供当前页面url。如果您知道上面已经没有参数,您可以只执行
    window.location=window.location.href+“?special=1”。如果已经有了参数,那么就需要多一点逻辑,但这都只是字符串操作,所以我想你可以处理它。然后,当页面加载时,您可以检查
    window.location.search
    以查看页面URL上的参数。非常感谢您的帮助,jfriend00。没有涉及其他参数。但是,我不明白我将用什么信息替换
    ?special=1
    。这代表一个变量吗?它只是URL末尾的一个字符串。我会在我的答案中加上一些代码。