简体   繁体   中英

How to call external javascript from URL inside function

I'm will try to make my previous question a little more clearer.

So I have to CPA offer from my CPA network using tag.

<script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372715"></script>

and

<script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372716"></script>

Now, I want it to randomly call and choose between the two when the user click the download button.

Example scenario:

When a user clicked download button, the <script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372715"></script> will be called.

When a new user clicked download again the <script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372716"></script> will show.

In ramdom manners.

I hope I make my question clear and you guys can help me. Thank you very much.

Just give your script tag an id and then read the src attribute. With slice cut of the last character, and add 5 + (0 or 1 random) to the string again. So the result will be 372715 or 372716 as the id.

If there is no JS active, the src is still valid, but only with this id: 372715

As a side note:
This won't work in your case. The time you manipulate the script src attribute, the script was already loaded. So you should do this on server side.

 var dlScr = document.getElementById('dl-script'); dlScr.src = dlScr.src.slice(0, -1) + (5+Math.round(Math.random())); 
 <script id="dl-script" type="text/javascript" src="https://megadownloder.com/script_include.php?id=372715"></script> 

You just need to round it up using Math.round . Math.random will give you a number between 0 and 1 . Rounding it will ensure it will be 0 or 1 . After that you can use it strait in the if statement, because 0 is false , and 1 - and everything not 0 actually - is true , in case of numbers. By negating you could get true / false instead: !(Math.round(Math.random())) , of course in reverse order, but because it's random, it does not matter. But if that bothers you just add extra ! to negate it again like: !!(Math.round(Math.random())) .

But if you need more than two outcomes, just multiply Math.random by the number of outcomes minus one. For example for a three way you could use: Math.round(Math.random() * 2) . But then you'll have to use if statement like in your example.

In case you need 1 or 2 as outcomes, just add one to the random number like: Math.round(Math.random() + 1) . This will return 1 or 2 .

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
    function chose() {

        // Rounding up like: Math.round(Math.random()).
        // Will return 0 or 1.

        // By negating you could get true/false instead:
        // !(Math.round(Math.random()))
        // !0 > true
        // !1 > false

        // For a three way (or more) random switch use:
        // Math.round(Math.random() * [ways - 1]).
        // Will return 0, 1 or 2.

        // For a case where you want 1 or 2 as results:
        // Math.round(Math.random() + 1).
        // Will return 1 or 2.

        return Math.round(Math.random() + 1);
    }

    function GetResult() {
        if ( chose() === 1 ) {
            OfferOne();
        } else { 
            OfferTwo();
        }
    }

    function loadScript ( id ) {
        $('<scr'+'ipt type="text/javascript" src="https://megadownloder.com/script_include.php?id=' + id +'"></scr'+'ipt>').appendTo(document.head);
    }

    function OfferOne() {
        loadScript(1000000);
    }

    function OfferTwo() {
        loadScript(2000000);
    }
</script>

If your code uses document.write() and you do not want it to overwrite the whole page if called after DOMReady , to be safe put the whole script above into the <head> , and use this loadScript function instead, to ensure synchronicity!

<script> 
    function loadScript ( id ) {
        document.write('<scr'+'ipt type="text/javascript" src="https://megadownloder.com/script_include.php?id=' + id +'"></scr'+'ipt>');
    }
</script>

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