-4

I'm learning Javascript (very very new to it). My question is How to connect to a url in Javascript.

Here is example of what I'm trying to do:

src="https://example.com/users/naughty_monkey"

var spank = new spank.MyMonkey(MyMonkeyIsNaughty);
spank.start();

How should this be done, so it can be saved as .js file?

Thank you all

Edit: I'm not trying to play a song, but asking how to structure the function properly so it would connect to the web and do something. Just to avoid the song problem.

  • 5
    What is your goal? Play a mp3 that you access remotely with `src` url? – Arnaud Moret Sep 27 '17 at 16:16
  • So you're trying to play a MP3 from the web by providing a direct link to it, in JavaScript? – ggg Sep 27 '17 at 16:16
  • 3
    Possible duplicate of [Playing audio with Javascript?](https://stackoverflow.com/questions/9419263/playing-audio-with-javascript) – Hajji Tarik Sep 27 '17 at 16:25
  • It was just an example with the audio. It might also be to display image, or download the image. I'm not sure if it should be 'src' or something else. – umpa_lumpa Sep 27 '17 at 16:38
  • Don't give a general example - try to accomplish a very specific task and ask a question about that. – takendarkk Sep 27 '17 at 16:49
  • @csm_dev it says what I'm trying to do... more or less. Visit the site, find a naughty monkey, and spank it :) – umpa_lumpa Sep 27 '17 at 16:55
  • I don't agree. I can't tell if you're trying to get a Json response from an API, or download an image to display it, or just download a file, or execute a function from some other website. I don't see a clear definition of what exactly you are trying to do, just "connect to some other URL" which could mean a lot of things. – takendarkk Sep 27 '17 at 16:58
  • @csm_dev that's why I said I'm very very new to Javascript. Let's say I'm trying to connect to a server/website, and execute function from the visited site. The MP3 was just general example. – umpa_lumpa Sep 27 '17 at 17:08

1 Answers1

0

What the OP is asking is vague, but I'm trying to answer it the way I understand it.

How can I use functionality of a 3rd party library hosted on another site? So basically, a webpage typically exists of HTML, Javascript and CSS. Currently we are just interested in Javascript and CSS.

So what we do is this: 1. Load the Script sources you would like to use! 2. Execute javascript that utilizes those loaded script sources

An example where we load the jQuery library: (Working codepen: https://codepen.io/iwantwin/pen/vemEXe)

<html>
  <head>
    <!--Import 3rd party library like this-->
    <script
  src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
  integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
  crossorigin="anonymous"></script>
    <!-- Execute it after that was loaded -->
    <script>
      //Use the library --> '$' is loaded here as an dependency, so we are using the library!
      //Use the library for the event handling
      $(document).ready(function(){
        //Use the library for the html binding
        $('#result').html('Jquery loaded');
      });

    </script>
  </head>
  <body>
    <div id="result">Loading jquery...</div>
  </body>
</html>

In jQuery, it's not as clear an example as what you use in your post, but that is really so vague, and just an example anyways, that I sticked to explaning using any 3rd party library to use :)

iwantwin
  • 130
  • 1
  • 7