[Solved] Cocos2d-JS publish to web but get "Cannot read property 'style' of null"

Hi,

The project is published to web with command

cocos compile -p web -m release
cocos compile -p web -m release --advanced

When i open the index.html, i see this error on console

Uncaught TypeError: Cannot read property 'style' of null

The error happen in game.min.js with code start from _loadJs4Dependency:function and before loadImg:function

_loadJs4Dependency:function(a,
b,c,d){if(c>=b.length)d&&d();else{var e=this;e._createScript(cc.path.join(a,b[c]),!1,function(f){if(f)return d(f);e._loadJs4Dependency(a,b,c+1,d)})}},_loadJsImg:function(){var a=document,b=a.getElementById("cocos2d_loadJsImg");if(!b){b=cc.newElement("img");cc._loadingImage&&(b.src=cc._loadingImage);a=a.getElementById(cc.game.config.id);a.style.backgroundColor="black";a.parentNode.appendChild(b);var c=getComputedStyle?getComputedStyle(a):a.currentStyle;c||(c={width:a.width,height:a.height});b.style.left=
a.offsetLeft+(parseFloat(c.width)-b.width)/2+"px";b.style.top=a.offsetTop+(parseFloat(c.height)-b.height)/2+"px";b.style.position="absolute"}return b},loadTxt:function(a,b){if(cc._isNodeJs)require("fs").readFile(a,function(a,c){a?b(a):b(null,c.toString())});else{var c=this.getXMLHttpRequest(),d="load "+a+" failed!";c.open("GET",a,!0);/msie/i.test(navigator.userAgent)&&!/opera/i.test(navigator.userAgent)?(c.setRequestHeader("Accept-Charset","utf-8"),c.onreadystatechange=function(){4===c.readyState&&
(200===c.status?b(null,c.responseText):b(d))}):(c.overrideMimeType&&c.overrideMimeType("text/plain; charset\x3dutf-8"),c.onload=function(){4===c.readyState&&(200===c.status?b(null,c.responseText):b(d))});c.send(null)}},_loadTxtSync:function(a){if(cc._isNodeJs)return require("fs").readFileSync(a).toString();var b=this.getXMLHttpRequest();b.open("GET",a,!1);/msie/i.test(navigator.userAgent)&&!/opera/i.test(navigator.userAgent)?b.setRequestHeader("Accept-Charset","utf-8"):b.overrideMimeType&&b.overrideMimeType("text/plain; charset\x3dutf-8");
b.send(null);return 4===!b.readyState||200!==b.status?null:b.responseText},loadCsb:function(a,b){var c=new XMLHttpRequest;c.open("GET",a,!0);c.responseType="arraybuffer";c.onload=function(){var d=c.response;d&&(window.msg=d);4===c.readyState&&(200===c.status?b(null,c.response):b("load "+a+" failed!"))};c.send(null)},loadJson:function(a,b){this.loadTxt(a,function(c,d){if(c)b(c);else{try{var e=JSON.parse(d)}catch(f){throw"parse json ["+a+"] failed : "+f;}b(null,e)}})},_checkIsImageURL:function(a){return null!=
/(\.png)|(\.jpg)|(\.bmp)|(\.jpeg)|(\.gif)/.exec(a)},loadImg:function

I’m tried to publish with Cocos2d-JS v3.6.1 and Cocos2d v3.7.
Both give same error.

The project working fine in other platform.

Anyone have solution on this?

Your help is much appreciated.

I found the solution.

If you direct open the index.html, it won’t work.
All the published files in html folder must put into host in order to work.

Localhost is working but there is cross origin policy issue.

If you are using Google Chrome, use this command to open Google Chrome can bypass the issue.

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security