简体   繁体   中英

Convert script in Jquery function when clicked on button

I am trying to integrate a button function in jquery, I am a learner of jquery but i am not sure how scripting can be converted into jquery

Here is a code

<script type="text/javascript">
var _hmc = _hmc || [];
_hmc.push(['app', '<application ID>']);
_hmc.push(['title','Talk with us.']);
(function() {
var hm = document.createElement('script'); hm.type = 'text/javascript';
hm.async = true;
hm.src = 'https://hipmob.s3.amazonaws.com/hipmobchat.min.js';
var b = document.getElementsByTagName('script')[0]; b.parentNode.insertBefore(hm, b);
})();
</script>

I need to place it inside button click

 jQuery('.live-chat').click(function()
   {

   });

Also if anyone would like to elaborate how things work eg what is || [] etc

The code you would need to do what you want is the following:

jQuery('.live-chat').click(function() {
    var _hmc = _hmc || [];
    _hmc.push(['app', '<application ID>']);
    _hmc.push(['title','Talk with us.']);

    var hm = document.createElement('script');
    hm.type = 'text/javascript';
    hm.async = true;
    hm.src = 'https://hipmob.s3.amazonaws.com/hipmobchat.min.js';
    var b = document.getElementsByTagName('script')[0];
    b.parentNode.insertBefore(hm, b);

    return false;
});

As for your questions:

  1. || means OR in javascript
  2. [] is shorthand for creating a new array, or an 'array literal'
  3. This means the line var _hmc = _hmc || []; var _hmc = _hmc || []; means _hmc equals _hmc OR a new array

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