【问题标题】:For some reason, the Javascript loads even before my HTML/CSS page is loaded出于某种原因,Javascript 甚至在我的 HTML/CSS 页面加载之前加载
【发布时间】:2019-04-06 06:48:48
【问题描述】:

我对网络开发真的很陌生。出于某种原因,Javascript 代码(警报)甚至在我的 HTML/CSS 页面加载之前就加载了。我正在使用示例警报来查看代码会发生什么。理想情况下,它应该在显示警报之前加载整个网页。我在谷歌上查了一会儿,几乎每个人都说最后要调用 Java Script 文件,这就是我所拥有的。还是不行。

    function go() {
	alert('hi');
	alert('hey there');
    }

    go();
		<header> 
			<nav>
				<h1 class = "second"> RESUMAKER </h1>
				<ul>
					<li><a href="build.html" target="_blank"><span> Build Your Resume </span></a></li>
					<li><a href="createaccount.html" target="_blank"><span> Create Account </span></a></li>
					<li><a href="signin.html" target="_blank"><span> Sign in </span></a></li>
					<li><a href="resources.html" target="_blank"><span> Resources </span></a></li>
					<li><a href="contacts.html" target="_blank"><span> Talk To Us </span></a></li>
				</ul>
			</nav>
		</header>

		<h1 class = "third"><strong> What we do? </strong></h1>
		<p> 1. We create your resume when you input information such as personal information, educational history, work experience and skills. </p>
		<p> 2. The inputted information will be collected, stored, converted and in turn generate arrays of information which can be transformed into a resume.</p>
		<p> 3. Then we let you select a predefined resume template all with different fonts and designs, to generate a resume. </p>
		<p> 4. After all that is done, we allow you to be able to download generated resume as a PDF, Google or Microsoft Word document. </p>

		<div class="footer">
  			<li><a href="build.html" target="_blank"> Build a resume </a></li>
			<li><a href="createaccount.html" target="_blank"> Create Account </a></li>
			<li><a href="signin.html" target="_blank"> Sign in </a></li>
			<li><a href="resources.html" target="_blank"> Resources </a></li>
			<li><a href="contacts.html" target="_blank"> Contact us </a></li>
		</div>

		<div class="icon-bar">
			<a href="#" class="facebook"><i class="fa fa-facebook"></i></a> 
			<a href="#" class="twitter"><i class="fa fa-twitter"></i></a> 
			<a href="#" class="google"><i class="fa fa-google"></i></a> 
			<a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
			<a href="#" class="youtube"><i class="fa fa-youtube"></i></a> 
		</div>

	

【问题讨论】:

  • 问题是您使用的是alert,它阻止浏览器-使用更用户友好的不阻止的东西,不应该有一个问题

标签: javascript html css frontend


【解决方案1】:

您可以设置超时。这将导致它在运行 go 之前等待 20 毫秒。似乎警报在 dom 准备好的那一刻运行,没有时间渲染。在此示例中,它先呈现,然后发出警报。

var go = () => {
    alert('hi')
    alert('hey there')
}
window.addEventListener('load', async () => {
    await new Promise(resolve => {setTimeout(resolve, 20)})
    go()
})
<head>
    <title>Resumaker</title>
    <link rel="stylesheet" type="text/css" href="main.css">
    <!--<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">-->
</head>

<body>

    <header> 
        <nav>
            <h1 class = "second"> RESUMAKER </h1>
            <ul>
                <li><a href="build.html" target="_blank"><span> Build Your Resume </span></a></li>
                <li><a href="createaccount.html" target="_blank"><span> Create Account </span></a></li>
                <li><a href="signin.html" target="_blank"><span> Sign in </span></a></li>
                <li><a href="resources.html" target="_blank"><span> Resources </span></a></li>
                <li><a href="contacts.html" target="_blank"><span> Talk To Us </span></a></li>
            </ul>
        </nav>
    </header>

    <h1 class = "third"><strong> What we do? </strong></h1>
    <p> 1. We create your resume when you input information such as personal information, educational history, work experience and skills. </p>
    <p> 2. The inputted information will be collected, stored, converted and in turn generate arrays of information which can be transformed into a resume.</p>
    <p> 3. Then we let you select a predefined resume template all with different fonts and designs, to generate a resume. </p>
    <p> 4. After all that is done, we allow you to be able to download generated resume as a PDF, Google or Microsoft Word document. </p>

    <div class="footer">
        <li><a href="build.html" target="_blank"> Build a resume </a></li>
        <li><a href="createaccount.html" target="_blank"> Create Account </a></li>
        <li><a href="signin.html" target="_blank"> Sign in </a></li>
        <li><a href="resources.html" target="_blank"> Resources </a></li>
        <li><a href="contacts.html" target="_blank"> Contact us </a></li>
    </div>

    <div class="icon-bar">
        <a href="#" class="facebook"><i class="fa fa-facebook"></i></a> 
        <a href="#" class="twitter"><i class="fa fa-twitter"></i></a> 
        <a href="#" class="google"><i class="fa fa-google"></i></a> 
        <a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
        <a href="#" class="youtube"><i class="fa fa-youtube"></i></a> 
    </div>


    <script type="text/javascript" src="main.js"></script>

</body>

【讨论】:

  • 很抱歉,我必须对此投反对票。这是一种解决方法,充其量是相当糟糕的一种。除非你能解释为什么window.onload 不起作用,否则这个答案基本上是“这样做的方法由于某种原因不起作用,所以这里有一些不好的代码来修复它”。此外,如果浏览器真的、真的、真的很糟糕,并且在 1 秒内没有加载 DOM,那么它就无法工作。
  • 嗯?好的。我有一个新想法来做这件事
  • @JermahlWhite 太棒了!当您编辑时,回复评论我一定会改变我的投票:)
  • Promise 是我的最爱!我非常喜欢使用异步函数。我也编辑了
【解决方案2】:

发生这种情况是因为您已经在调用 go 函数,正如您在上一条语句 (go();) 中指定的那样。因此,一旦加载脚本就会发出警报,这可能早于加载 HTML/CSS 页面。

为防止这种情况发生,请删除该语句,然后在您的 HTML 页面中调用 go 函数。

【讨论】:

  • 我只是在观看 YouTube 上的一个教程视频,这家伙做了同样的事情(像我一样调用 go 函数),但它是在为他加载网页后加载的。
  • 这可能是由其他一些原因引起的,因为该代码从未指定是否必须在页面加载后调用 go() 函数。要指定这一点,您可以查看这个问题 (stackoverflow.com/questions/11936816/…)
  • HTML页面加载完成后如何加载?
  • 请看我提供的链接
  • 我做了,但我没有使用 JQuery,我想要单独的 JS 文件,只想在 HTML 中调用 JS 文件,就像 CSS 一样。有没有更简单的方法让它做到这一点?该链接让我更加困惑。
猜你喜欢
  • 2021-12-06
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 2018-05-26
  • 1970-01-01
相关资源
最近更新 更多