iT邦幫忙

0

script 微調的問題確認

請教大家
今天在逛網頁的時候看到下列的資訊
參照此網址:https://developers.google.com/speed/docs/insights/BlockingJS
下列語法:

<html>
  <head>
    <script type="text/javascript" src="small.js"></script>
  </head>
  <body>
    <div>
      Hello, world!
    </div>
  </body>
</html>

要如何改成下列的方式呢?

<html>
  <head>
    <script type="text/javascript">
      /* contents of a small JavaScript file */
    </script>
  </head>
  <body>
    <div>
      Hello, world!
    </div>
  </body>
</html>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
tzuchin
iT邦新手 5 級 ‧ 2016-10-10 12:03:35

我想您可能沒有看仔細Google的範例XD

原文片段:

如果外部指令碼很小,您可以將指令碼直接置入到 HTML 文件中。以這種方式嵌入小型檔案,瀏覽器就能繼續轉譯網頁。舉例來說,如果 HTML 程式碼的內容如下:

<html>
  <head>
    <script type="text/javascript" src="small.js"></script>
  </head>
  <body>
    <div>
      Hello, world!
    </div>
  </body>
</html>

資源 small.js 則如下所示:
/* contents of a small JavaScript file */
您即可像下方這樣嵌入指令碼:

<html>
  <head>
    <script type="text/javascript">
      /* contents of a small JavaScript file */
    </script>
  </head>
  <body>
    <div>
      Hello, world!
    </div>
  </body>
</html>

將 small.js 嵌入 HTML 文件,可消除對其的外部要求。

  1. 文中有說到,如果外部指令碼很小的時候建議這樣做。
  2. 就如他給的範例,假設現在small.js的內容如下
alert("abc");
console.log("def");

像這樣只有做一點點事情,
就不需要有一個small.js檔案,
直接把他寫進去html內就可以,
也就是:

<html>
  <head>
    <script type="text/javascript">
      alert("abc");
      console.log("def");
    </script>
  </head>
  <body>
    <div>
      Hello, world!
    </div>
  </body>
</html>
ektrontek iT邦研究生 1 級 ‧ 2016-10-10 21:02:17 檢舉

了解
感謝,那如果想把一些SCRIPT及CSS檔案放置在同一台LINUX主機上,這些檔案的權限開放建議怎麼開放呢?(讀寫刪的權限部分)

我要發表回答

立即登入回答