-2

I'm trying to link a basic javascript file from one web server to another the same way that cloud-based jquery files work. However, when I link it like their files as seen below the alert stored in the external file doesn't trigger. I know this isn't the best practice to do this, however, for the small task it is required. Any suggestions would be great.

Link

<script src="http://example.com/javascriptfile.js"></script>

JS File

(function() {
  alert("Matt it worked bud");
})
Matt Hammond
  • 765
  • 1
  • 7
  • 25

2 Answers2

1

Just execute it. Like this:

(function() {
  alert("Matt it worked bud");
})();

Reading this may help: https://developer.mozilla.org/pt-BR/docs/Glossario/IIFE

As we have discussed in the chat, this question is relevant to your problem: Safari 9 disallowed running of insecure content?

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • The alert still isn't being executed and when I click on the script src in the editor unlike the other files, mine opens and new tab not the code. – Matt Hammond Jul 17 '18 at 21:21
  • Sorry but I didn't understand what you mean by "when I click on the script src in the editor unlike the other files, mine opens and new tab not the code". Can you reformulate please ? – Nelson Teixeira Jul 17 '18 at 21:31
  • Not to sure how to explain it but basiclly the url is being taken as a webpage but not a file – Matt Hammond Jul 17 '18 at 21:33
  • 1
    I meant inspect element, sorry! And Safari – Matt Hammond Jul 17 '18 at 21:35
  • That's because it's a url and it's not in your own filesystem. But when it opens in the other tab, can you see the javascript code ? If you can't that means you're using the wrong url. I mean that the file for js isn't in the location you think it is, or there's some feature of http server disallowing the access to the file. – Nelson Teixeira Jul 17 '18 at 21:38
  • The link is and if you click it, it goes to the js code. But never get executed by more another website. However with other external files work for example, . Is it something to do something with my server? – Matt Hammond Jul 17 '18 at 21:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176188/discussion-between-nelson-teixeira-and-matt-hammond). – Nelson Teixeira Jul 17 '18 at 21:44
1

The function isn't be triggered because you haven't called it.

<script>
 (function() {
  alert("Matt it worked bud");
 })();
</script>

You may also want to look into Cross-Origin Resource Sharing (CORS).