Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
Acceder a una variable dentro de una etiqueta <script> en línea en un archivo html con JavaScript
<script async src="https://www.googletagmanager.com/gtag/js?id=<key here>"></script> <script> const container = document.getElementById('app'); const key = container.getAttribute('secret-key'); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', key); </script>

Estoy tratando de acceder a la clave que necesito para agregar Google Analytics dentro de la etiqueta <script> . Pude hacerlo en el segundo bloque de código usando

 const container = document.getElementById('app'); const key = container.getAttribute('secret-key');

Quiero usar la misma clave para la primera cláusula <script> , pero no estoy seguro de poder hacer lo mismo para el <script> en línea. Quiero usar la misma const key para reemplazar <key here> .

¿Cómo puedo conseguir esto?

EDITAR

 <script> function appendGAScriptTag() { const configKeys = JSON.parse(container.getAttribute('data-bootstrap')).common.conf; const gaKey = configKeys['GOOGLE_ANALYTICS_KEY']; let gaScriptTag = document.createElement('script'); gaScriptTag.type = 'text/javascript'; gaScriptTag.async = true; gaScriptTag.src = `https://www.googletagmanager.com/gtag/js?id=${gaKey}`; document.getElementsByTagName('head')[0].appendChild(gaScriptTag); } appendGAScriptTag(); // <--- calling it here </script>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede crear dinámicamente la etiqueta <script> usando... ¡más JavaScript!

 function appendGAScriptTag() { var gaScriptTag = document.createElement('script'); gaScriptTag.type = 'text/javascript'; gaScriptTag.async = true; gaScriptTag.src = `https://www.googletagmanager.com/gtag/js?id=${document.getElementById('app').getAttribute('secret-key')}`; document.getElementsByTagName('head')[0].appendChild(gaScriptTag); }

Llama appendGAScriptTag() en tu código JavaScript preexistente cuando quieras inyectar el elemento generado en el DOM.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!