简体   繁体   English

函数调用中的document.write和innerHTML

[英]document.write and innerHTML in function call

I' m new to javascript and I was wondering why in this script the document.write works well while innerHTML doesn't(I've given a div id timer in the body). 我是javascript的新手,我想知道为什么在此脚本中document.write可以很好地工作,而innerHTML却不起作用(我在体内指定了div id计时器)。 Is it that innerHTML needs Onload event in the body part of html? innerHTML是否需要html正文部分中的Onload事件? If so why? 如果可以,为什么? (I'm calling the function writehere() in the head section of html document. (我在html文档的开头部分调用了函数writehere()

<script language="javascript">

 function writehere()

 {
       document.write("hello");

       var Timer = document.getElementById("timer");

        Timer.innerHTML = "hello";


}

writehere();

</script>

html code goes here..... html代码在这里.....

Most likely your issue is that the DOM isn't ready yet by the time your code executes. 您的问题很可能是在代码执行时DOM还没有准备好。 For your example you can probably just move it down to the bottom of your page and it will work. 对于您的示例,您可能只需将其向下移动到页面底部即可使用。

In general you would want to make sure that the DOM is fully loaded before executing your javascript that needs to interact with the DOM, if you are using jQuery you can call your code in the document ready function to ensure the DOM is loaded, for example 通常,您需要在执行需要与DOM交互的JavaScript之前确保DOM已完全加载,如果您使用的是jQuery,则可以在文档就绪函数中调用代码以确保DOM被加载。

   $(document).ready({

    });

Have a look at this SO question for vanilla javascript equivalents 看看这个普通香草javascript等效问题

What is the non-jQuery equivalent of '$(document).ready()'? $(document).ready()的非jQuery等效项是什么?

I think your problem is that the DOM has not been loaded yet, so there is no div element with id="Timer". 我认为您的问题是尚未加载DOM,因此没有id =“ Timer”的div元素。 You should call this method on the onLoad event. 您应该在onLoad事件上调用此方法。

It's all about HTML tags elements and execution time. 这一切都与HTML标签元素和执行时间有关。 In your example, the script is positioned in the head tag , so when the script is executed, the element div#timer does not exists at all. 在您的示例中,脚本位于head标记中 ,因此在执行脚本时,根本不存在div#timer元素。

Then you have 2 solutions: 然后,您有2个解决方案:

If your DOM is not complex, and does not contains img tags or any kind of elements that need to be fetched from the network, the use of onload is not needed. 如果您的DOM不复杂,并且不包含img标签或需要从网络中获取的任何类型的元素,则不需要使用onload You can use it in two cases: 您可以在两种情况下使用它:

  • you have to wait for all elements to be loaded, like images, videos etc, 您必须等待所有元素加载,例如图片,视频等,
  • you want to delay the execution of a low-priority script, like a script to load ads on your site for example. 您想延迟低优先级脚本的执行,例如在网站上加载广告的脚本。 This script is not vital for your website and your visitor, the execution can be delayed 该脚本对于您的网站和访问者而言并不重要,执行可能会延迟

In general, is it considered as a good practice to put script tags at the end of the document (and so you don't need to use onload ), it prevents the Flash Of Unstyled Content (FOUC). 通常,将script标签放在文档的末尾是一种好习惯(因此您不需要使用onload )被认为是一种好习惯,因此它可以防止未样式化内容(FOUC)闪烁。 Javascript blocks your browser rendering engine. Javascript会阻止您的浏览器呈现引擎。 So if JavaScript tags are place before the content becomes available to the user (read: in the head tag), the browser will executed it, the DOM being almost empty, and the user will only see a blank page during this execution time. 因此,如果将JavaScript标记放在内容对用户可用之前(在head标记中读取:),则浏览器将执行它,而DOM几乎为空,并且在此执行期间用户将仅看到空白页面。

Also, about jQuery.fn.ready method, you have to be aware of this: 另外,关于jQuery.fn.ready方法,您必须意识到这一点:

$(document).ready(function() {
    // my first useless function
    console.log( "first", 1 );
});

$(document).ready(function() {
    // my second useless function
    // this function will throw an (intentional) error
    console.log( "second", someUndefinedVariable );
});

$(document).ready(function() {
    // my third useless function
    // because the previous function contains an error
    // this function will never be called
    console.log( "third ", 3 );
});

So let's say you're using some plugins, and you've some hand-written code and so on. 假设您使用的是一些插件,并且有一些手写的代码,依此类推。 If every plugin and your code is using the jQuery.fn.ready method, and if for some reason (let's say in some specific browser version under some circumstances) a function throw an error, then all handler after this function would be never runt... 如果每个插件和您的代码都使用jQuery.fn.ready方法,并且由于某种原因(例如,在某些情况下在某些特定浏览器版本中)某个函数抛出错误,则此函数之后的所有处理程序将永远不会崩溃。 ..

Also, doing so, you're delaying all the "real" JavaScript execution at the end, and if you've a lot of methods to run in the queue, then you could blocks the browser during some seconds, and the user will notify it. 此外,这样做会延迟最后所有“真正的” JavaScript执行,如果您有很多方法要在队列中运行,则可以在几秒钟内阻止浏览器,并且用户会通知它。

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

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