Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
How to know which file called index.js?

I've three html files a.html,b.html & c.html

All of them call index.js.

Inside index.js, I'm importing a variable from a.js,b.js & c.js. Based on which html file called index.js , I want to use the variable from the corresponding js file inside index.js.

pic

It's not possible for me to remove index.js as it contains logic, which is needed for some other tasks.

Some more clarification:

index.js


if calling_html == a.html:
   import {variable} from a.js
else if calling_html == b.html:
   import {variable} from b.js
else 
   import {variable} from c.js

How to find calling_html?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I'd suggest looking at this post : How to pass parameters to a Script tag?

Basically you pass in a value in the script tag, which tells the index.js script which from which file you are loading it.

And then use

document.currentScript.getAttribute()

Edit : I find it less recommendable, but if you absolutely so wish, you can also use :

the

window.location

attribute

using window.location.href, or window.location.pathname

Alternatively, what is shown here :

https://www.sitepoint.com/get-url-parameters-with-javascript/

by doing something like

const urlParams = new URLSearchParams(window.location.search); const caller = urlParams.get('caller');

And load your a.html page with "[...]a.html?caller=a"

Now with all that said, I'm assuming that you don't want define a global constant with a header script tag in the page before using index.js as in

<head>
    //called before loading any other script
    <script>const caller='a.html';</script>
    // Rest of the header
</head>
// somewhere else
<script src="./index.js" />

and

<head>
    //called before loading any other script
    <script>const caller='b.html';</script>
    // Rest of the header
</head>
// somewhere else
<script src="./index.js" />
about 4 years ago · Juan Pablo Isaza Relatório

0

<script scr="./a.js" defer> // const a = 5 </script>
<script scr="./index.js" defer> // const myVariable = b || c || a </script>

Since you are defining your variables globally, you don't need to import them. You just need to make sure they are defined and parsed before your index.js file in HTML. After you do this, you can simply access those variable like this:

const myVariable = a || b || c; 

For example, if you are on c page, a and b will be undefined because they simply not exist on that page then c will be your value.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda