0

I have this script that is from a third party such as

<script id="snippet" src="https//something.com/dsd/something.js?key=35345" />

Im trying to use window.onload but I can't figure out how

I've searched StackOverflow and I've seen here where they recommend doing something like

 <script type="text/javascript">
    function codeAddress() {
        alert('ok');
    }
    window.onload = codeAddress;
    </script>

how can I put my script in a function to do this?

I also found this page that shows how to add it and they recommend doing

window.addEventListener('load', nameOfFunction);

but I dont have a function name. Ideally I would like do to something like

window.AddEventListener('load', "https://https//something.com/dsd/something.js?key=35345");
user2903379
  • 393
  • 3
  • 19

1 Answers1

0

It will be something such as:

 window.addEventListener('load', (event) => {
  const el = document.createElement('script');
  el.setAttribute('src','https://localhost/1.js');
  document.body.append(el);
  // call other function from 1.js in this example
});
Gal Gur-Arie
  • 202
  • 1
  • 3
  • I dont want to add another js file though because I already have this code in a partial. Is there another way? – user2903379 Apr 19 '21 at 14:51
  • It seems that your question is not clear enough. I understood from your question that you wish to use external JavaScript file when the page is loaded and call a function from it. this is what my example is showing. What exactly are you're trying to achieve? – Gal Gur-Arie Apr 19 '21 at 16:42