如果使用ajax加载来获取内容,如何打开包含内容和容器的新选项卡?

时间:2016-04-23 15:45:16

标签: php ajax tabs load

我有一个带有导航和内容div的php页面。当我在导航中选择一个选项时,通过在其中加载另一个php文件来修改内容。为此我使用带加载功能的ajax。我的问题是,如果我右键单击并选择在新选项卡或新窗口中打开,显然,只有内容在选项卡中打开。

我知道我可以在每个php文件中拥有整个页面(容器+内容)并只加载内容div,但我认为这没有多大意义。

有没有办法在新标签页中的内容中获取容器?

1 个答案:

答案 0 :(得分:2)

我已经遇到过类似情况,标签页面都有,菜单位于首页。你可以应用这个解决方案:

此逻辑适用于iframe,如果您使用div,则可以对此进行调整:

主页面带有导航菜单,在这里你需要应用一些这样的逻辑:

<script>
        //Object to get values of URL (GET)
        var request = {
            get get() {
                var vars = {};
                if (window.location.search.length !== 0)
                    window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
                        key = decodeURIComponent(key);
                        if (typeof vars[key] === "undefined") {
                            vars[key] = decodeURIComponent(value);
                        }
                        else {
                            vars[key] = [].concat(vars[key], decodeURIComponent(value));
                        }
                    });
                return vars;
            },
            getParam: function (param) {
                var vars = request.get;
                if (vars[param] != undefined) {
                    return vars[param];
                } else {
                    return null;
                }

            }, getParams: function () {
                return request.get;
            }
        };
        //variable that defines if the client are in mainWindow
        window.mainWindowFrame = true;

        var tabName = request.getParam("tab");
        console.log("tabName",tabName);


        //call some function to reload the content of iframe or div to requested tab.
        //if(tabName != null || tabName != ""){
        //tabs.load(tabName) ....
        //}
    </script>

在内容页面中,如果菜单不存在,您需要使用这段代码重新加载页面:

<script>
        //name of this tab
        var tabName = "someTab";
        //check if the menu are present here
        if(window.top.mainWindowFrame == undefined){
            window.location.href = "mainpage.html?tab="+tabName;
        }
    </script>