回答編集履歴

2

phpタグを書き忘れていたので修正

2017/05/02 06:56

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -6,9 +6,11 @@
6
6
 
7
7
  #例
8
8
 
9
+ **js.php**
10
+
9
11
  ```PHP
10
12
 
11
- # js.php
13
+ <?php
12
14
 
13
15
  header('Content-Type', 'text/javascript'); # このphpのレスポンスはtext/javascriptであることをブラウザに教える
14
16
 
@@ -20,6 +22,8 @@
20
22
 
21
23
  ```
22
24
 
25
+ **index.html**
26
+
23
27
  ```html
24
28
 
25
29
  <script src="js.php" type="text/javascript"></script>

1

誤植修正、コメント追加、htmlについて追記

2017/05/02 06:56

投稿

masaya_ohashi
masaya_ohashi

スコア9206

test CHANGED
@@ -12,10 +12,18 @@
12
12
 
13
13
  header('Content-Type', 'text/javascript'); # このphpのレスポンスはtext/javascriptであることをブラウザに教える
14
14
 
15
- %message = "Hello world!";
15
+ $message = "Hello world!"; # phpの変数
16
16
 
17
- $js = "alert(\"$message\");";
17
+ $js = "alert(\"$message\");"; # phpの変数を使ってjavascriptのコードを生成
18
18
 
19
- echo $js; # レスポンスとしてJavascriptのコードを返す
19
+ echo $js; # レスポンスとしてjavascriptのコードを返す
20
20
 
21
21
  ```
22
+
23
+ ```html
24
+
25
+ <script src="js.php" type="text/javascript"></script>
26
+
27
+ <!--読み込まれたらalert("Hello world!");が実行される-->
28
+
29
+ ```