简体   繁体   English

如何从函数内部的URL调用外部javascript

[英]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. 所以我必须使用标签从我的CPA网络提供CPA。

<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. 当用户单击下载按钮时,将调用<script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372715"></script>

When a new user clicked download again the <script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372716"></script> will show. 当新用户再次单击下载时,将显示<script type="text/javascript" src="https://megadownloder.com/script_include.php?id=372716"></script>

In ramdom manners. 在ramdom礼仪。

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. 只需为脚本标记指定一个id,然后读取src属性即可。 With slice cut of the last character, and add 5 + (0 or 1 random) to the string again. 使用最后一个字符的切片,并再次向字符串添加5 +(0或1随机)。 So the result will be 372715 or 372716 as the id. 因此结果将是372715372716作为id。

If there is no JS active, the src is still valid, but only with this id: 372715 如果没有JS活动,则src仍然有效,但只有这个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. 操作脚本src属性的时间已经加载了脚本。 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.round将其四舍五入。 Math.random will give you a number between 0 and 1 . Math.random将为您提供01之间的数字。 Rounding it will ensure it will be 0 or 1 . 舍入它将确保它将为01 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. 之后你可以在if语句中使用strait,因为0false ,而1 - 实际上一切都不是0 - 如果是数字则是true 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. 通过否定你可以得到true / false!(Math.round(Math.random())) ,当然是以相反的顺序,但因为它是随机的,所以无关紧要。 But if that bothers you just add extra ! 但如果这困扰你只需添加额外的! to negate it again like: !!(Math.round(Math.random())) . 再次否定它: !!(Math.round(Math.random()))

But if you need more than two outcomes, just multiply Math.random by the number of outcomes minus one. 但是,如果您需要两个以上的结果,只需将Math.random乘以结果数减一。 For example for a three way you could use: Math.round(Math.random() * 2) . 例如,对于三种方式,您可以使用: Math.round(Math.random() * 2) But then you'll have to use if statement like in your example. 但是你必须在你的例子中使用if语句。

In case you need 1 or 2 as outcomes, just add one to the random number like: Math.round(Math.random() + 1) . 如果您需要1或2作为结果,只需将一个添加到随机数,如: Math.round(Math.random() + 1) This will return 1 or 2 . 这将返回12

<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! 如果您的代码使用document.write()并且您不希望它覆盖整个页面(如果在DOMReady之后调用),为了安全地将整个脚本放在<head> ,并使用此loadScript函数来确保同步!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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