简体   繁体   English

JavaScript在Chrome中的页面之前加载

[英]Javascript loads before page in Chrome

I am having a strange problem with my google chrome browser. 我的Google Chrome浏览器出现了一个奇怪的问题。 Every time I try to run a simple small javascript code and do alert box my HTML page is white and alert box pop-ups first. 每次我尝试运行一个简单的小型javascript代码并执行警报框时,我的HTML页面都是白色的,并且警报框弹出窗口优先。

So far I tried: 到目前为止,我尝试了:

  • Putting scrip tag <script></script> at the end of body tag </body> <script></script>标签<script></script>放在body标签</body>的末尾
  • Using defer 使用defer
  • Using async 使用async
  • Clearing cache of my browser and restarting it. 清除浏览器的缓存并重新启动。

Keep in mind that in Mozilla, edge and other browsers I have no problems and simple solution as placing a script tag at the end near closing body tag does the trick. 请记住,在Mozilla,Edge和其他浏览器中,我没有问题,也没有简单的解决方案,因为在关闭body标记的末尾放置一个脚本标记可以解决问题。 So it's working in every other browser just not in chrome. 因此,它可以在其他所有浏览器中运行,而不仅限于chrome。

I am using: 我在用:

  • Windows 10 Windows 10
  • Chrome Version 67.0.3396.87 (Official Build) (64-bit) Chrome版本67.0.3396.87(官方内部版本)(64位)

(I did see similar questions posted here but didn't find an answer for fixing it as I tried all they recomended) (我确实在这里看到过类似的问题,但在我尝试所有建议的过程中都没有找到解决问题的答案)

Try to check window.onload() function in JavaScript 尝试检查JavaScript中的window.onload()函数

It load the JavaScript contents after the window is fully loaded. 窗口完全加载后,它将加载JavaScript内容。

You can put or initialize all your JavaScript inside window.onload() 您可以在window.onload()放置或初始化所有JavaScript。

For example; 例如;

window.onload() {
    // Put your JavaScript code here.
}

There. 那里。 Just use the window.onload = function() {} 只需使用window.onload = function() {}

 window.onload = function() { document.getElementById("elementToChange").innerHTML = "changed!"; } 
 <div id="elementToChange">hiiiiii</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM