Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript history.pushState错误,我不知道为什么_Javascript_Html_Css - Fatal编程技术网

Javascript history.pushState错误,我不知道为什么

Javascript history.pushState错误,我不知道为什么,javascript,html,css,Javascript,Html,Css,不要假装文件:///与网页相同:浏览器没有使用与真实网页相同的机制加载它们,网页可以做的许多事情对于普通文件都不起作用 如果您想使用web API查看代码作为web页面的行为,则需要使用https正确加载代码。这意味着使用一个简单的服务器,甚至不是一个完整的Apache或类似的服务器,只是一个单行http服务器,如python-m SimpleHTTPServer,或node.js或packages等,然后加载它,其中someport是单线服务器所说的正在使用的任何端口号,yourfilenam

不要假装文件:///与网页相同:浏览器没有使用与真实网页相同的机制加载它们,网页可以做的许多事情对于普通文件都不起作用


如果您想使用web API查看代码作为web页面的行为,则需要使用https正确加载代码。这意味着使用一个简单的服务器,甚至不是一个完整的Apache或类似的服务器,只是一个单行http服务器,如python-m SimpleHTTPServer,或node.js或packages等,然后加载它,其中someport是单线服务器所说的正在使用的任何端口号,yourfilename显然就是您的文件名。

可能是@mevius的副本这并不是错误地调用pustState,而是使用file:///并期望web行为。因此,现在我只需要在网页上进行模拟,这是正确的吗?不是模拟:列出的所有解决方案都是真正的网页服务解决方案。是的,它们都解决了使用CORS时遇到的CORS来源问题file:///not 当然,是谁在4年后的同一天决定对这个答案投两张反对票,但这个答案100%适用于这个问题。当使用file:///时,由于安全模型的限制更大,几乎不适用于正常的浏览器行为。
<!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>
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