简体   繁体   中英

$.getScript not working within a click function

I'm not sure why the $getScript doesn't work within the click function on firefox browser but does work on other browsers. The same code outside the click function works on firefox. Can someone explain what the problem is?

 <script type="text/javascript">
    var page = "";
    page = window.location.href;
        jQuery(document).ready(function($){
             $( "#socialemail" ).click(function() {
                $.getScript('/social-media.php?email&' + page);
                window.location.href="mailto:?subject=text here Systems&body="+escape(window.location.href);
             });  
       });    
    </script>

Since $.getScript() is asynchronous and I expect you want to wait for the results of it, you should execute the following window.location.href property change in a callback function passed to $.getScript() . Something like

var page = "";
page = window.location.href;
    jQuery(document).ready(function($){
         $( "#socialemail" ).click(function(e) {
            // prevent the default action of clicking on whatever element
            // with id 'socialemail' is
            e.preventDefault();

            $.getScript('/social-media.php?email&' + page, function () { 
                // run this after the script has been fetched
                window.location.href="mailto:?subject=text here Systems&body="+encodeURIComponent(window.location.href);
            });
         });  
   });   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM