简体   繁体   English

重定向到新的HTML页面后隐藏div

[英]Hide div after redirecting to new HTML page

I have two html pages index.html and register.html From index.html I need to go to register.html and show Login page after skipping select region. 我有两个html页面index.html和register.html从index.html开始,我需要转到register.html并跳过选择区域后显示“登录”页面。 below is the code I am trying. 下面是我正在尝试的代码。

$("#lgnToProfile").on("click", function(){
window.location.href = "register.html";
$("#slctRgn").hide();
$("#lgnPage").show();
});

Can anyone check and suggest me how to resolve this and what I am doing wrong? 谁能检查并建议我如何解决此问题以及我做错了什么?

I referred the solutions provide at Show DIV or Section after redirecting to html page But no success. 重定向到html页面后,我提到了在Show DIV或Section提供的解决方案,但没有成功。

Once you navigate to a new page, none of the Javascript that ran on the previous page has any effect anymore. 导航到新页面后,在前一页上运行的Javascript都不再起作用。

What you would need to do, is somehow inform register.html that it needs to hide the select region div and then hide the div from a script running in register.html . 您需要做的是以某种方式告知register.html它需要隐藏选择区域div,然后从register.html运行的脚本中隐藏div。

For example, from index.html you could add a query string parameter to notify register.html . 例如,可以从index.html添加查询字符串参数以通知register.html

$("#lgnToProfile").on("click", function() {
    window.location.href = "register.html?hideSlctRgn=1";
}

Then, from register.html , on document ready , check if the parameter is set and hide the div accordingly. 然后,从register.html在文件ready上 ,检查参数是否已设置并相应地隐藏div。

$(function() {
    if ( window.location.search.indexOf('hideSlctRgn=1') != -1 ) {
        $("#slctRgn").hide();
        $("#lgnPage").show();
    }
})

There are other (better) ways to test for the existence of a query string parameter, but the above is a simple way. 还有其他(更好)的方法来测试查询字符串参数的存在,但是以上是一种简单的方法。 Instead of a query string parameter you could set a cookie, or perhaps use local storage. 除了设置查询字符串参数外,您还可以设置Cookie,或者使用本地存储。

If you don't want to modifie your URL path, you can do it with "localStorage": 如果您不想修改您的URL路径,则可以使用“ localStorage”进行:

var currentPath = window.location.pathname;
var savedPath   = localStorage.getItem('previousPage');

// if a page has been saved yet
if (typeof savedPath === 'string') {

    // if the current page is "register.html" and the saved page is "index.html"
    if (currentPath.match('/register.html') && savedPath.match('/index.html')) {
        $("#slctRgn").hide();
        $("#lgnPage").show();
    }
}

// save current page
localStorage.setItem('previousPage', currentPath);

https://developer.mozilla.org/fr/docs/Web/API/Window/localStorage https://developer.mozilla.org/fr/docs/Web/API/Window/localStorage

You need to check the referer page to take further action on the next page. 您需要检查引荐页面以在下一页上采取进一步的措施。 Because JavaScript can't access the referer page, you could achieve that using an anchor or hash to check for the referer and show/hide your elements. 由于JavaScript无法访问引用页面,因此您可以使用锚点或哈希值来检查引用并显示/隐藏元素来实现。

On index.html you just need the link (no click jQuery event needed) to the registration page appending a hash name: register.html#referer . index.html您只需要指向注册页面的链接(无需click jQuery事件),并附加一个哈希名称: register.html#referer The word referer could be whatever you want to. referer词一词可以是您想要的任何词。

Then on register.html you need to evaluate the hash value: 然后在register.html您需要评估哈希值:

$(document).ready(function() {

    // get the hash value
    var hash = window.location.hash;

    // if the hash is defined and it’s equal to “referer” (the choosen hash value)
    if(hash == "#referer") {

        // execute your code to show or hide elements

    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM