如何在Javascript中连接到URL

时间:2017-09-27 16:14:42

标签: javascript

我正在学习Javascript(非常新的)。我的问题是如何连接到Javascript中的URL。

这是我尝试做的例子:

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

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

如何做到这一点,所以它可以保存为.js文件?

谢谢大家

编辑:我没有尝试播放歌曲,而是询问如何正确构建功能,以便连接到网络并执行某些操作。只是为了避免歌曲问题。

1 个答案:

答案 0 :(得分:0)

OP所要求的是模糊的,但我试图按照我理解的方式回答它。

如何使用托管在其他网站上的第三方图书馆的功能? 基本上,网页通常存在HTML,Javascript和CSS。目前我们只对Javascript和CSS感兴趣。

所以我们这样做是: 1.加载您想要使用的脚本源! 2.执行利用那些加载的脚本源的javascript

我们加载jQuery库的示例:(工作代码: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>

在jQuery中,它并不像你在帖子中使用的那样清楚,但这真的很模糊,而且只是一个例子,我坚持使用任何第三方库来解释: )