search

Home  >  Q&A  >  body text

Correct use of JavaScript to write multiple meta tags

<p>Hello, I am very new to JS. I would like to ask about my code, how can I write it correctly?</p> <pre class="brush:php;toolbar:false;">function impScript(scriptURL){ let headTitle = document.querySelector('head'); let setProperty = document.createElement('script'); setProperty.setAttribute('href',scriptURL); headTitle.appendChild(setProperty); } const metaScript = ['ar.js','ar2.js'] impScript(metaScript);</pre> <p>The code works, but the result is 2script href="ar.js,ar2.js"></script></p> <p>I want to do this</p> <pre class="brush:php;toolbar:false;"><script href="ar.js"></script> <script href="ar2.js"></script</pre> <p>What should I do? Your answer will be helpful, thank you. </p>
P粉432906880P粉432906880470 days ago515

reply all(1)I'll reply

  • P粉517814372

    P粉5178143722023-08-02 12:53:03

    You need to loop through the array and create tags one by one:

    metaScript.forEach(s => impScript(s));

    reply
    0
  • Cancelreply