javascript – 无法在“历史记录”上执行’replaceState’无法在原点为’null’的文档中创建

前端之家收集整理的这篇文章主要介绍了javascript – 无法在“历史记录”上执行’replaceState’无法在原点为’null’的文档中创建前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为过渡创建一个页面.

点击页面导航到另一个页面 – 适用于Firefox,但不适用于Chrome.

错误显示

Uncaught SecurityError: Failed to execute ‘replaceState’ on ‘History’:
A history state object with URL ‘file:///C:/Users/athite/Desktop/DEMO/page.html’ cannot be created in a document with origin ‘null’.**

这是我的代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta name="viewport" content="width=device-width,initial-scale=1">
  5. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  6. <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  7. <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
  8. </head>
  9. <body>
  10.  
  11. <div data-role="page" id="pageone">
  12. <div data-role="header">
  13. <h1>Welcome To My Homepage</h1>
  14. </div>
  15.  
  16. <div data-role="main" class="ui-content">
  17. <p>Click on the link to see the slide effect.</p>
  18. <a href="#pagetwo" data-transition="slide">Slide to Page Two</a>
  19. </div>
  20.  
  21. <div data-role="footer">
  22. <h1>Footer Text</h1>
  23. </div>
  24. </div>
  25.  
  26. <div data-role="page" id="pagetwo">
  27. <div data-role="header">
  28. <h1>Welcome To My Homepage</h1>
  29. </div>
  30.  
  31. <div data-role="main" class="ui-content">
  32. <p>Click on the link to go back. </p>
  33. <a href="#pageone" data-transition="slide" data-direction="reverse">Go to Page One</a>
  34. </div>
  35.  
  36. <div data-role="footer">
  37. <h1>Footer Text</h1>
  38. </div>
  39. </div>
  40.  
  41. </body>
  42. </html>

解决方法

问题出现在jquery.mobile-1.4.5.min.js:3中

解:

在导入之前添加此脚本,如下所示:

  1. <script>
  2. $(document).bind('mobileinit',function(){
  3. $.mobile.pushStateEnabled = false;
  4. });
  5. </script>
  6. <script type="text/javascript" src="js/jquery.mobile-1.4.5.min.js"></script>

猜你在找的JavaScript相关文章