JS页面重定向未触发

JS page redirect not firing

本文关键字:重定向 JS      更新时间:2023-09-26

我在单击按钮和一些验证后无法重定向页面。 我已经尝试了多种解决方案,并不断得到相同的结果,其中地址栏填充了 URL,但没有重定向。 如果我使用 F5 刷新或单击浏览器的刷新按钮,它就可以工作,页面重定向和加载正常。 这是我尝试过的所有浏览器。所以我一定错过了什么或做错了什么。下面是我的代码:


            ........
            ..............
            //render function for json + templates like handlebars, xml + xslt etc.
            render: function (dataItem, statuses) {
                $list.html(template(dataItem.content));
                // action button events
                //$('.action a').on("click",function (e){
                $('.action a').bind('click', function(e){
                    var $this = $(this),
                        action = $this.attr("action"),
                        $id = $this.closest('ul').attr('id');
                    // get which link was click for appropriate action
                    switch(action){
                        case 'prev' :
                        break;;
                        case 'edit' :
                            if($this.hasClass("actionEnabled")) {
                                $().ajaxCall(checkURL,{'ID':$id})
                                .done(function(data){
                                    if(data.result == 0) {
                                        var baseURL = window.location.href.replace(window.location.hash, '') + '#',
                                            url = baseURL + secondUrl_part + "?ID=" + $id;
                                            // url = "http://mysite/index.php#/page/destPage.php?ID=idabcdefgh"
                                        // tried with javascript all these different methods -> still did not work
                                        // method 1
                                        $(location).attr('href', "http://mysite/index.php#/page/destPage.php?ID=idabcdefgh");
                                        // method 2
                                        window.location = "http://mysite/index.php#/page/destPage.php?ID=idabcdefgh";
                                        // method 3
                                        window.location.href = "http://mysite/index.php#/page/destPage.php?ID=idabcdefgh";
                                        // method 4
                                        window.location.replace("http://mysite/index.php#/page/destPage.php?ID=idabcdefgh");
                                        // even tried with return false as suggested by some people
                                        //return false;
                                    } else {
                                        // todo error message no longer exist
                                    }
                                })
                                .fail(function(){
                                    // todo error : could not be loaded
                                })
                            } else {
                                e.preventDefault();
                                // to do add message cannot edit
                            }
                        break;;
                        case 'del' :
                        break;
                    }
                });
            }
            ..........
            .......
            .....

这是基于Ivan的答案的代码

            //render function for json + templates like handlebars, xml + xslt etc.
        render: function (dataItem, statuses) {
            $list.html(template(dataItem.content));
            // action button events
            //$('.action a').on("click",function (e){
            $('.action a').bind('click', function(e){
                var $this = $(this),
                    action = $this.attr("action"),
                    $id = $this.closest('ul').attr('id');
                // get which link was click for appropriate action
                switch(action){
                    case 'prev' :
                    break;;
                    case 'edit' :
                        if($this.hasClass("actionEnabled")) {
                            $().ajaxCall(checkURL,{'ID':$id})
                            .done(function(data){
                                if(data.result == 0) {
                                    var baseURL = window.location.href.replace(window.location.hash, '') + '#',
                                        url = baseURL + secondUrl_part + "?ID=" + $id;
                                    $(location).attr('href', url);
                                    location.reload();
                                } else {
                                    // todo error message no longer exist
                                }
                            })
                            .fail(function(){
                                // todo error : could not be loaded
                            })
                        } else {
                            e.preventDefault();
                            // to do add message cannot edit
                        }
                    break;;
                    case 'del' :
                    break;
                }
            });
        }