如何在第二次访问页面时激活javascript?

时间:2016-04-13 09:24:31

标签: javascript html5 twitter-bootstrap css3

我非常喜欢堆栈溢出,但这是我第一次写作!

当您点击公司徽标时,我有一个页面,其中有一个右下方的菜单。它是直接开发的。 我使用这个脚本来保持菜单打开,同时浏览内容页面,如“约”,“画廊”,“联系人”,ecc ......

<script>
    $(document).ready(function () {
    $("#slide").click();
});
</script>

我需要的是在您从之前告诉您的其他内部页面返回时,在主页上激活此脚本。而不是在第一个主页加载,我需要关闭徽标,其菜单隐藏在其中!

任何人都有一些提示或建议吗? 非常感谢大家!

还有其他可能的解决方案吗?

2 个答案:

答案 0 :(得分:2)

第一次访问时,在文档就绪条款中:

localStorage.setItem("displayDropDown", "true");

每次访问:

$(document).ready(function() {
    if(localStorage.getItem("displayDropDown") === "true") {
        $("#slide").click();
    }
});

将它们放在同一个doc-ready子句中:

$(document).ready(function() {
    if(localStorage.getItem("displayDropDown") === "true") {
        $("#slide").click();
    }
    localStorage.setItem("displayDropDown", "true");
});

答案 1 :(得分:1)

这是一个完整的例子。它使用localStorange和Cookie(通过js-cookie)作为后备:

现代浏览器示例:

<!DOCTYPE html>
  <html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1 id="count"></h1>
    <script type="text/javascript">
      const getNumberOfPreviousVisits = () => {
        const numberOfVisits = window.localStorage.numberOfVisits ? parseInt(window.localStorage.numberOfVisits, 10) + 1 : 0;

        window.localStorage.numberOfVisits = numberOfVisits;

        return numberOfVisits;
      }

      document.addEventListener('DOMContentLoaded', function(event) {
        if (getNumberOfPreviousVisits() > 0) {
          // $("#slide").click();
        }
      });
    </script>
  </body>
</html>

示例,支持旧版浏览器:

<!DOCTYPE html>
  <html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1 id="count"></h1>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.0/js.cookie.min.js"></script>
    <script type="text/javascript">
      var localStorageAvailable = function() {
        try {
          var x = '__storage_test__';
          window.localStorage.setItem(x, x);
          window.localStorage.removeItem(x);
          return true;
        } catch(e) {
          return false;
        }
      }

      var getNumberOfPreviousVisitsLocalStorage = function() {
        var numberOfVisits = window.localStorage.numberOfVisits ? parseInt(window.localStorage.numberOfVisits, 10) + 1 : 0;

        window.localStorage.numberOfVisits = numberOfVisits;

        return numberOfVisits;
      }

      var getNumberOfPreviousVisitsCookie = function() {
        var numberOfVisits = Cookies.get('numberOfVisits') ? parseInt(Cookies.get('numberOfVisits'), 10 ) + 1 : 0;

        Cookies.set('numberOfVisits', numberOfVisits);

        return numberOfVisits;
      }

      var getNumberOfPreviousVisits = function() {
        if (localStorageAvailable()) {
          return getNumberOfPreviousVisitsLocalStorage();
        }

        return getNumberOfPreviousVisitsCookie();
      }

      document.addEventListener('DOMContentLoaded', function(event) {
        if (getNumberOfPreviousVisits() > 0) {
          // $("#slide").click();
        }
      });
    </script>
  </body>
</html>

原始示例:

<!DOCTYPE html>
  <html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1 id="count"></h1>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.1.0/js.cookie.min.js"></script>
    <script type="text/javascript">
      const localStorageAvailable = () => {
        try {
          const x = '__storage_test__';
          window.localStorage.setItem(x, x);
          window.localStorage.removeItem(x);
          return true;
        } catch(e) {
          return false;
        }
      }

      const getNumberOfPreviousVisitsLocalStorage = () => {
        const numberOfVisits = window.localStorage.numberOfVisits ? parseInt(window.localStorage.numberOfVisits, 10) + 1 : 0;

        window.localStorage.numberOfVisits = numberOfVisits;

        return numberOfVisits;
      }

      const getNumberOfPreviousVisitsCookie = () => {
        const numberOfVisits = Cookies.get('numberOfVisits') ? parseInt(Cookies.get('numberOfVisits'), 10 ) + 1 : 0;

        Cookies.set('numberOfVisits', numberOfVisits);

        return numberOfVisits;
      }

      const getNumberOfPreviousVisits = () => {
        if (localStorageAvailable()) {
          return getNumberOfPreviousVisitsLocalStorage();
        }

        return getNumberOfPreviousVisitsCookie();
      }

      document.addEventListener('DOMContentLoaded', function(event) {
        if (getNumberOfPreviousVisits() > 0) {
          // $("#slide").click();
        }
      });
    </script>
  </body>
</html>