0

First, this is a noob question as I don't know anything on web development. I'm creating a prestashop website and I have a module which display a javascript popup with a coupon code for the website, what I would like to do il to call the same script when somebody click on a specific link in another place in the main page. I searched the source code for the script and I think this is the one :

<script>
var ju_num=(typeof ju_num === 'undefined') ? '58D7493F-E32E-4ADF-AB9B-9DE26502404C' : ju_num;var asset_host='//d2j3qa5nc37287.cloudfront.net/';
(
    function() {
        setTimeout(
                function(){
                    var s=document.createElement('script');
                    s.type='text/javascript';
                    s.async=true;
                    s.src=asset_host+'coupon_code1.js';
                    var x=document.getElementsByTagName('script')[0];
                    x.parentNode.insertBefore(s,x);},500
                  )
                }
)()
</script>

How can I call this script on a link click ?

Thanks by advance.

EDIT :

With answer help I create a javascript file with the following :

 function show_coupon() {

 var a = document.getElementById("my_coupon");
 a.onclick = function(){
                var ju_num=(typeof ju_num === 'undefined') ? '58D7493F-E32E-4ADF-AB9B-9DE26502404C' : ju_num;
                var asset_host='//d2j3qa5nc37287.cloudfront.net/';
                var s=document.createElement('script');
                s.type='text/javascript';
                s.async=true;
                s.src=asset_host+'coupon_code1.js';
                var x=document.getElementsByTagName('script')[0];
                x.parentNode.insertBefore(s,x);
            }
            return false;
      }

then I loaded the javascript file in my index like this :

    <script type="text/javascript" src="/js/coupon.js"></script>

and finaly I changed my link to :

 <a id="my_coupon" title="" href="javascript:show_coupon()"></a>

but unfortunatly nothing happens when I click on my link.

EDIT 2 :

Ijust understood what was wrong, this script is just loading functions included in the coupon_code1.js, it was just necessary to call the right function in the link. Problem solved.

Cyrille MODIANO
  • 2,246
  • 2
  • 21
  • 33
  • It looks like this question will provide your answer: http://stackoverflow.com/questions/1265887/call-javascript-function-on-hyperlink-click – Harvey A. Ramer May 24 '14 at 14:09
  • Hi, you're right it answer a part of my question but what should I write in my link code to call this, i'm little bit lost. should I create a function from this code and then call the function in the link ? – Cyrille MODIANO May 24 '14 at 14:29

1 Answers1

0

The code as it stands is not callable from elsewhere because the variable declarations are in the global scope and the other code is wrapped in a self executing anonymous function, which doesn't have a reference to it stored anywhere. You can access the global variables but the function is not reachable (after the initial execution).

The easiest way of calling it elsewhere is to wrap it in a function declaration like so:

function popup(){
    // code here
}

This can be called from anywhere, for example to call it from a link:

// use this before the </body> or wrap in a window.onload handler
document.getElementById("yourlinkid").onclick = popup;
MrCode
  • 63,975
  • 10
  • 90
  • 112