This question already has answers here:
Official way to ask jQuery wait for all images to load before executing something
(11 answers)
Closed 6 years ago.
I have a simply script. For example:
var rowCount = $('tr').length;
document.write(rowCount);
I want to load this after entire webpage has loaded. How can I do?
Use following script:
<script type="text/javascript">
$(document).ready(function () {
var rowCount = $('tr').length;
document.write(rowCount);
});
</script>
Related
This question already has answers here:
$(document).ready(function(){}); vs script at the bottom of page
(2 answers)
What's the difference between $(document).ready() and including a script at the end of the body?
(3 answers)
What's the difference between $(document).ready() and just omit it?
(4 answers)
what's the difference between onload action and an action in a bottom script
(2 answers)
Closed 2 years ago.
I've replaced jQuery's .onReady() function with both of the following.
<script>
document.addEventListener("DOMContentLoaded", () => {
alert("Page Loaded");
});
</script>
and
<script>
(() => {
alert("Page Loaded");
})();
</script>
They both work. My question is simply, does DOMContentLoaded provide any advantage? Or does it "catch" things that the anonymous function might "miss"? Or are they both perfectly equivalent?
EDIT: I failed to mention in the original post (and it's been pointed out that its location is important) that this <script> is at the bottom of the page following the closing </html> tag.
They're completely different. The anonymous function (you mean an immediately-invoked function expression, or IIFE) may "work" in your particular context, but unlike the DOMContentLoaded event it doesn't wait for the DOM to load.
If you tried the IIFE approach in the head and your code tried to interact with the DOM, it would fail.
<!doctype html>
<head>
<script>
(() => {
let p = document.querySelector('p'); //null
})();
document.addEventListener('DOMContentLoaded', () => {
let p = document.querySelector('p'); //object
});
</script>
</head>
<body>
<p>Hello</p>
</body>
This question already has answers here:
How to add onload event to a div element
(26 answers)
Closed 4 years ago.
I have a .txt file called sample.txt, where I want to load some text and display it into my HTML document.
Here is my code:
<div id="test"></div>
document.getElementById('test').addEventListener("load", getText);
console.log("script loaded succesfully")
function getText(){
fetch('sample.txt').then(function(data){
console.log("hello")
console.log(data.text);
});
}
My issue is that the function getText, is not called on load, how can this be, when it logs out script loaded successfully?
The proper way to call the load event it by attaching it to the window like :
window.addEventListener("load", getText);
I think to be sure that the DOM is loaded you could use DOMContentLoaded event instead :
document.addEventListener("DOMContentLoaded", getText);
This question already has answers here:
eval() doesn't execute external (src=...) scripts
(2 answers)
Closed 6 years ago.
I'm using eval() to execute all <script> tags after total rewrite of a div.
$("#content").find("script").each(function(){
eval($(this).text());
});
It works well for inline-scripts, but has no effect on scripts like:
<script src="/path/to/externalScript.js"></script>
How come? Can I "force" the browser to load and execute also the external scripts?
Besides the very real issues with using eval, you are trying to eval the .text inside those script tags, which is essentially nothing.
When a <script> tag loads, it links in the external file as a resource to your page and executes the script. It does not render anything directly to the dom.
Thus you $(this).text() will return ''.
If you want to reload the external scripts, you will either need to force a page refresh, or potentially change the way you are pulling in those scripts: ex. jQuery.getScript alternative in native JavaScript
$("body").css("background", "silver");
$("button").click(function () {
$("script[src^='data:']").each(function () {
var script = document.createElement('script');
script.src = this.src;
document.body.appendChild(script);
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="data:text/javascript,document.body.style.background='red'"></script>
<button>Go</button>
This question already has answers here:
Best practice for using window.onload
(7 answers)
Closed 7 years ago.
I haven't been working with javascript before, so I am still learning the basics.
I have included two javascript files in my main file called index.html. The reason for this is to let users select which javascript file to show (diagram 1 or diagram 2) by clicking on the navigation tool bar. The problem is that both of these javascript files have a equal method called window.onload, so they replace each other. How can I prevent these two javasript files to replace each other?
First javascript file:
window.onload = function {// Preview diagram 1}
Second javascript file:
window.onload = function {// Preview diagram 2}
Here is a snip of what I have in my index.html
<script type="text/javascript" src="js/diagram1.js"></script>
<script type="text/javascript" src="js/diagram2.js"></script>
In your both diagram1.js & diagram2.js files, use:
$(window).on("load", function() {
// your init code
});
to do stuff on window load instead of registering function to window.onload.
This question already has answers here:
How can I convert an HTML element to a canvas element?
(10 answers)
Closed 8 years ago.
I want to save screenshot of the current page ( full page with scroll) and save it as an image or pdf .
You can use html2canvas js library to take a screen shot and then use canvas in javascript to download/save.
HTML
<button id="save-img">Download</button>
JS/jQuery
$("#save-img").click(function () {
html2canvas(document.body, {
onrendered: function(canvas) {
document.location = canvas.toDataURL("image/png");
}
});
});
Or read this article if you want to achieve this in asp.net using DataVisualization.
Hope that helps.