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


How to open a new tab with the content and the container together if using ajax load to get the content?

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

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

有没有办法让容器沿着新选项卡中的内容?

我已经遇到过类似的情况,选项卡有页面,菜单在首页。 您可以应用此解决方案:

此逻辑适用于 iframe,如果您使用的是divs,则可以为此进行调整:

带有导航菜单的主页,在这里您需要应用一些逻辑,如下所示:

<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>