3

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.

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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>

8 Comments

Thanks for your great solution about getting random number. But what I'm trying to accomplish is to get random number between 1 and 2. and when I get the number 1, the offer will display the "OfferOne()" function, else it will display the "OfferTwo()" function.
@RafaelMendoza See updated answer. Last section will give you 1 or 2, by adding one to the random number before or after rounding it.
@RafaelMendoza But why? If you do not pass it to the OfferOne function it does not matter at this point if you choose between 0/1 or 1/2.
Thank you very much for your help, I already know now to randomly get 1 and 2. I have to figure out now how can I call <script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372715"></script> when I get 1 and <script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372716"></script> when I get 2. Hope you could help me with this one.
@RafaelMendoza Please see update, using jQuery, a very simple method.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.