onload

Javascript read file without using input

时间秒杀一切 提交于 2019-11-28 10:02:57
I have this code and for a file to be converted into base64 I have to click on Choose file and then select it. I want to hardcode the file name so it is converted to base64 on page load. JavaScript: var handleFileSelect = function(evt) { var files = evt.target.files; var file = files[0]; if (files && file) { var reader = new FileReader(); reader.onload = function(readerEvt) { var binaryString = readerEvt.target.result; document.getElementById("base64textarea").value = btoa(binaryString); }; reader.readAsBinaryString(file); } if (window.File && window.FileReader && window.FileList && window

JavaScript - overwriting .onload prototype of HTMLImageElement(s)

送分小仙女□ 提交于 2019-11-28 09:36:44
问题 Is it possible to bind an onload event to each image, declaring it once? I tried, but can't manage to get it working... (this error is thrown: Uncaught TypeError: Illegal invocation ) HTMLImageElement.prototype.onload = function() { console.log(this, "loaded"); }; P.S: I also tried returning this , but doesn't seem to be the issue here... any suggestions / explanations on why my current code isn't working? 回答1: You can't set a handler on the prototype, no. In fact, I'm not aware of any way to

How to trigger onload event when downloading a file in an iframe?

空扰寡人 提交于 2019-11-28 09:14:36
Suppose we have the following HTML file: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Test iframe download</title> <script type="text/javascript"> var init = 0; function download() { document.getElementById("dload_frame").src = "http://example.com/dload.py"; } function alert() { if (init == 0) { init = 1; } else { document.getElementById("alert_span").innerHTML = "Got it!"; } } </script> </head> <body> <span id="alert_span">Main content.</span><br/> <input type="button" value="Download" id="btn" onclick="download()" /> <iframe id="dload_frame" src="http://404.com/404" onload=

Can I sync up multiple image onload calls?

我怕爱的太早我们不能终老 提交于 2019-11-28 07:04:55
I want a function to run when specific images are loaded, but I don't know how to wait for both to load before running. I only know how to chain them, like below: Image1 = new Image(); Image1.src = 'image1-link.jpg'; Image2 = new Image(); Image2.src = 'image2-link.jpg'; Image1.onload = function() { Image2.onload = function() { ... } } The downside to this is it has to wait till Image1 completely loads before getting the second. I want to try something like this: Image1 = new Image(); Image1.src = 'image1-link.jpg'; Image2 = new Image(); Image2.src = 'image2-link.jpg'; (Image1 && Image2).onload

Jquery Chosen focus on page load (onLoad?)

霸气de小男生 提交于 2019-11-28 07:03:23
问题 I'm using the Jquery Chosen plugin on a select box, however I'd like to focus this on page load. I've used the following code to focus a text input: onLoad="document.forms.sales_order_form.customer.focus()" But this doesn't work for the select box: <select data-placeholder="Select a Customer..." class="chzn-select" name="customer" style="width:400px;"> <option value=""></option> </select> 回答1: In Chosen 1.0, this command is now: $('.my_select_box').trigger('chosen:activate'); See here. 回答2:

window.onload vs document.ready jQuery

爷,独闯天下 提交于 2019-11-28 06:50:06
I have a site with two columns. I want to have equal height on both using jQuery. I'm trying to get the logo column height. I had: $(document).ready(function() { alert($('#logo').height()); });​ and it didn't work. So I changed it to: window.onload = function(){ alert($('#logo').height()); } And it's working. What is going on in here? I had a same problem in handling Image height and width inside $(document)ready and I found some better referenses to solve it... I hope this may help some one $(document).ready() The document ready event fired when the HTML document is loaded and the DOM is

When to use “window.onload”?

我怕爱的太早我们不能终老 提交于 2019-11-28 05:20:43
In JavaScript, when I want to run a script once when the page has loaded, should I use window.onload or just write the script? For example, if I want to have a pop-up, should I write (directly inside the <script> tag): alert("hello!"); Or: window.onload = function() { alert("hello!"); } Both appear to run just after the page is loaded. What is the the difference? window.onload just runs when the browser gets to it. window.addEventListener waits for the window to be loaded before running it. In general you should do the second, but you should attach an event listener to it instead of defining

Async-loaded scripts with DOMContentLoaded or load event handlers not being called?

心已入冬 提交于 2019-11-28 04:31:16
I've got a script with a DOMContentLoaded event handler— document.addEventListener('DOMContentLoaded', function() { console.log('Hi'); }); Which I'm loading asynchronously— <script async src=script.js></script> However, the event handler is never called . If I load it synchronously— <script src=script.js></script> It works fine. (Even if I change the DOMContentLoaded event to a load event, it's never called.) What gives? The event handler should be registered irrespective of how the script is loaded by the browser, no? Edit : It doesn't work on Chrome 18.0.1025.11 beta but, with

validate form on load

北城以北 提交于 2019-11-28 02:14:36
问题 I'm implementing the same validation in this link http://alittlecode.com/files/jQuery-Validate-Demo/ So I display check marks next to elements on success, is there a way to display the checkmarks on form load when it has valid input by default, and display errors only on submit ? here is the code $("#frmsubmit").validate({ rules: { citstyle: "required", spelling: "required", uploadfile: {required: function (element) { if($("#filesent").is(':checked')) { return false; } else { return true; } }

Javascript onload and onunload

心已入冬 提交于 2019-11-28 01:49:07
问题 Consider the following HTML snippet containing some javascript utilizing prompt and unload . The prompt() method works fine but I want alerting something like Goodbye, user when reloading or leaving the page. Any help is greatly appreciated. <body onload="promptName()" > <script type="text/javascript"> function promptName() { var userName = prompt("What's your name ?", "") return userName; } function goodBye() { alert("Goodbye, " + promptName() + "!"); } window.onunload = goodBye(); </script>