0
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        window.onload = function(){
            var oInput = document.getElementById('input1');
            var oDiv = document.getElementById('div1');
            var iNow = 1;
            oInput.onclick = function(){
                var number = randomNum(35,7);
                oDiv.innerHTML = number;
                history.pushState(number,'');

            }
            window.onpopstate = function(event){
                var number = event.state || '';
                oDiv.innerHTML = number;
            }

            function randomNum(alls,now){
                var arr = [];
                var newArr = [];
                for(var i=1;i<=alls;i++){
                    arr.push(i);
                }

                for(var i=0;i<now;i++){
                    newArr.push(arr.splice(Math.floor(Math.random()*arr.length),1));
                }
                return newArr;
            }
        }
    </script>
</head>
<body>
    <input type="button"  id="input1" value="35選7" />
    <div id="div1"></div>
</body>

I don't know why history.pushState does not work, it throws the error:

history.html:14 Uncaught SecurityError: Failed to execute 'pushState' on   

'History': A history state object with URL  
'file:///C:/Users/TED/Documents/HBuilderProjects/javascript-%E7%9F%A5%E8%AD%98%E9%A1%9E/history.html' cannot be created in a document 
with origin 'null' and URL 
'file:///C:/Users/TED/Documents/HBuilderProjects/javascript-%E7%9F%A5%E8%AD%98%E9%A1%9E/history.html'.oInput.onclick @ history.html:14
MWiesner
  • 8,868
  • 11
  • 36
  • 70
Andrew
  • 1
  • 1

1 Answers1

-2

Don't pretend that file:/// is the same as "web pages": they didn't get loaded by the browser using the same mechanism that real web pages go through, and lots of things that web pages can do will not work for "plain files".

If you want to see how your code behaves as web page, using web APIs, then you'll need to load it properly using http(s). That means using a simple server (not even a full blow Apache or the like, just a one-liner http server like python -m SimpleHTTPServer, or php -S localhost:8000 or node.js's http-server or live-server packages, etc. etc.) and then load it through http://localhost:someport/yourfilename, where "someport" is whatever port number the one-line server says is being used, and "yourfilename" is obviously the name of your file.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • so now i just need to simulate on web page, and it is ok right? – Andrew Mar 21 '16 at 02:47
  • not "simulate": all the solutions listed are real web page serving solutions. And yes, each of them solves the CORS origin problem you get when you use `file:///` – Mike 'Pomax' Kamermans Mar 21 '16 at 20:47
  • not sure who decided to throw two downvotes at this answer on the same day 4 years after the fact, but this answer is 100% applicable to the problem. When using `file:///`, almost nothing about normal browser behaviour applies, due to an incredibly more restricted security model. – Mike 'Pomax' Kamermans Mar 22 '20 at 18:13