How to call one javascript inside another?

Asked

Viewed 1,580 times

0

Hello, how do I make the following javascript below, work on another .js hosted on my domain? I could simply copy the content into the 5feaf8db752a6f16f5f860f0bc7c60.js and put in mine, but it 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>
  • The name of the tb file changes?

  • Why doesn’t this . js load directly on your website? Access is blocked?

2 answers

1

Solution 1:

Inside your file . JS, 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 to the page.


Solution 2:

Another solution is to create a script TAG dynamically, where it is possible to 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 its 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 several 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

});
  • Please collaborate with the quality of community content by better elaborating your response. Very short answers will hardly be clear enough to add anything to the discussion. If your response is based on some function or feature of the language/tool, link to the official documentation or reliable material that supports your response. Searching for similar questions in the community can also be interesting to indicate other approaches to the problem.

  • Okay, thanks for the tip.

  • Hi, I tried it, but the script didn’t work. Remembering that I need to make the javascript passed, work in one that I will create in my domain called "example.js".

  • Sorry @Bernardo. enter the above code Jsfiddle. You will get the example of them with an Alert()... take the code and insert it into your example.js file

  • Hi @Rafaelfischer hasn’t worked yet. As I passed the script without the working 'key', maybe you can not properly test the functioning, I will be leaving here. // In place of "wkid=xxx" add "c49e9754a2c10d66ae436a121268dc3c" in place of the first xxx

  • @Bernardo, it worked for me. See this link of Jsfiddle: [https://jsfiddle.net/rafaelfischer/3uu4ztr9/]. I put your wkid and called the method contained in the content js, and put it this way: alert(getParams(_0xa41a[0]).cap) From what I understand, this file . js has its contents obfuscated via Obfuscator, example done at this link [https://javascriptobfuscator.com/Javascript-Obfuscator.aspx].

  • @Bernardo, continuing, and the problem is that the next time they update this file. JS, is 99.9% sure that the variables will be changed, and then the variable _0xa41a who was caught in the getParams() won’t work anymore.

  • @Bernardo, if it’s nothing like that, please explain your need better.

Show 3 more comments

0

You can user the library Requirejs.

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

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

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

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

In the file where you need external javascript, just 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 questions, or if it doesn’t work for you, you can leave a comment that we tried to find out why.

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

  • Hi, I tried it, but it didn’t work. Remembering that I need to make the javascript passed, work in one that I will create in my domain called "example.js".

  • Did you create a main.js file and import your example.js into it? I updated the answer to make it more explained

Browser other questions tagged

You are not signed in. Login or sign up in order to post.