1

i'm having a problem when i want to document.getElementById(); in html tag. example =

<html>
<head>
<script type="js"> //note this example is not found so browser is take time to execution
<script type="js"> //note this example is not found so browser is take time to execution
<script type="js"> //note this example is not found so browser is take time to execution
<script type="js"> //note this example is not found so browser is take time to execution
</head>
<body>
<div> ...
<div id="test">
</div>
</body>
</html>

my question is how to execution document.getElementById("test"); without waiting load script is not found?that can make more time.how i can fix this.thanks for your answer.

note: solve not to remove or modify script to found directory. just in case,if the script not found.

3 Answers 3

1

Add defer="defer" to those <script /> tags for which you want to defer execution.

NOTE: defer works on IE and newer versions of Firefox and Chrome

Sign up to request clarification or add additional context in comments.

2 Comments

any other suggestion because my browser also execution script not found. i just want to priority document.getElementById("test"); is first execution.and i can get the innerHTML.
It's always better to load your JS files at the bottom of your page. See this why its better to load at the end : stackoverflow.com/questions/1638670/…. Please mind, there's no <srctype />, it should be, <script type="text/javascript" src="path/to/your/js/file"></script>
1

Try using headJS which will load your scripts async and after document.ready.

What is another option, is to move the srctype to the bottom of the file so they are loaded last. But I don't think the document.ready will be executed untill the browser has loaded those files

Comments

1

Assuming that <srctype> is supposed to be <script>, and that what you're asking is what you can do about non-existent scripts being referenced from a page, the only possible answer is that you switch to a dynamic script loading approach like LABjs or something, or, of course, to stop including script tags that refer to non-existent scripts.

Browsers will always attempt to load and run <script> tags that aren't marked "async", and that of course is a fairly new attribute whose behavior is not quite uniform across even those browsers that support it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.