javascript - Timeout for String.replace -


i trying add width , height attribute tag via javascript. img src web (http://www.....). using javascript.replace add in height , width. here code. (currently width).

<!doctype html> <html>  <body>     <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>     <script>     $(window).load(function() {          var htmlstring = '<div class="sample"> <img src="https://kbob.github.io/images/sample-5.jpg"> </div><div class="sample_another"><img src="http://www.crazymonkeydefense.com/wp-content/uploads/2012/02/sample-img3.png"> </div> ';          function dostuff(img, imgstring) {             img.onload = function() {                  imgstring = imgstring.replace("<img ", '<img width="' + this.width + 'px" ');                 console.log("modified = " + imgstring);             };         }          var modifyimg = function(imgstring) {             console.log("original = " + imgstring)              if (imgstring.indexof('width="') == -1) {                 var newimgstring = (imgstring.substring(imgstring.lastindexof('src="'), imgstring.lastindexof('"'))).replace('src="', '');                 var img = new image();                 img.src = newimgstring;                 dostuff(img, imgstring);             }             return imgstring;         }          modifiedhtmlstring = htmlstring.replace(/<img[^>]+>/g, modifyimg);         console.log(modifiedhtmlstring);       });     </script> </body>  </html> 

the problem htmlstring.replace(/]+>/g, modifyimg) performs instantly , sets modifyimg value undefined rather getting return statement.

how approach solution.

using done() function may resolve this.

$.when(modifyimg).done(function(v){   modifiedhtmlstring =  htmlstring.replace(/<img[^>]+>/g,v);   console.log(modifiedhtmlstring); }); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -