How to call a javascript inside another?

0

Hello, how do I do the following javascript below, work on other .js hosted on my domain? I could simply copy the contents inside 5feaf8db752a6f16f5f860f0bc7c60.js and put in mine, but the same suffers frequent changes by the company.

<script type='text/javascript' src='http://360popunder.com/5feaf8db752a6f16f5f860f0bc7c60.js?wkid=xxx&wid=xxx&k=xxx&traffic=0&cap=10&cid=downloads'></script>
    
asked by anonymous 23.02.2017 / 03:20

2 answers

1

Solution 1:

Inside your .JS file, put the code below:

document.write('<script src="http:\/\/360popunder.com\/5feaf8db752a6f16f5f860f0bc7c60.js?wkid=xxx&wid=xxx&k=xxx&traffic=0&cap=10&cid=downloads"type="text\/javascript"><\/script>');

This will add the javascript content on the page.

Solution 2:

Another solution is to create a TAG script dynamically, where you can call your code as soon as the file is loaded:

var script = document.createElement('script');
script.onload = function() {

        // seu codigo aqui

};
script.src = "http://360popunder.com/5feaf8db752a6f16f5f860f0bc7c60.js?wkid=xxx&wid=xxx&k=xxx&traffic=0&cap=10&cid=downloads";
document.getElementsByTagName('head')[0].appendChild(script);

JSFiddle

Solution 3 Use HeadJS (www.headjs.com) to load the file and then run your code in the JS file load. Example:

head.load("http://360popunder.com/5feaf8db752a6f16f5f860f0bc7c60.js?wkid=xxx&wid=xxx&k=xxx&traffic=0&cap=10&cid=downloads", function() {

    // seu codigo aqui

});

JsFiddle

or even upload multiple files:

head.load(["http://360popunder.com/5feaf8db752a6f16f5f860f0bc7c60.js?wkid=xxx&wid=xxx&k=xxx&traffic=0&cap=10&cid=downloads", "http://www.seusite.com/outro_arquivo.js"], function() {

    // seu codigo aqui

});
    
23.02.2017 / 04:24
0

You can user the RequireJS library.

To use it, simply download it and include it in your index.html, replacing static / js with the folder where your javascripts are:

<script data-main="static/js/main" src="static/js/require.js"></script>

In this example, there would be one main.js file, which would be responsible for importing all other files, using RequireJS. So you should import your example.js file into main.js:

// main.js
require(['exemplo'], function() {
});

In the file where you need the external javascript, you should do something like this:

// exemplo.js
require(['http://360popunder.com/5feaf8db752a6f16f5f860f0bc7c60.js?wkid=xxx&wid=xxx&k=xxx&traffic=0&cap=10&cid=downloads', 'outro_arquivo_necessário'], function(){
    // Toda sua lógica aqui dentro
});

Any question, or if it does not work for you, you can leave a comment that we try to find out why.

I suggest you read the RequireJS documentation if you choose to use it.

    
23.02.2017 / 04:55