77077

Not getting a variable value?

Question:

I am not getting the value of the variable of "img_srt" in load function, can any body helps me?

$(document).ready(function() { Get_var(); }) var img_srt='<div>address</div>'; function Get_var() { $("<img />").attr("src","http://www.google.com/intl/en_ALL/images/logo.gif") .load(function() { if (this.height > 0) { img_srt += "<div><img src='http://www.google.com/intl/en_ALL/images/logo.gif'/></div>"; } }) alert(img_srt); }

Answer1:

The function which sets img_srt is called asynchronously. That is, it may be executed before or after your alert statement. Likely after, as you're having this problem.

Try something like this:

$('<img/>') .attr('src', 'http://www.google.com/intl/en_ALL/images/logo.gif') .load(function() { var imageLoaded = this.height > 0; if(imageLoaded) { $(this).appendTo('#myDiv'); } else { $('<p/>').text('Unable to load image').appendTo('#myDiv'); } });

Recommend

  • jquery code not working without breakpoint
  • Python PIL to extract number from image
  • Outputting SharePoint Hyperlink Column as URL
  • Uncaught TypeError: $(…).select2 is not a function
  • Disable Kendo Autocomplete
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • Display images in Django
  • d3 v4 drag and drop with TypeScript
  • Meteor: Do Something On Email Verification Confirmation
  • Cannot resolve symbol 'MyApi'
  • FileReader+canvas image loading problem
  • does jqgrid support a multiple checkbox list for editing
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Optimizing database types to compact database (SQLite)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Resize panoramic image to fixed size
  • Cross-Platform Protobuf Serialization
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Importing jscolor library in angular 2
  • Do I've to free mysql result after storing it?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal