簡単なテストとしてfunction test()をapp.jsの中で呼び出すのには、下記のような構成&scriptでは呼び出せないのですが
何処が間違っているのかがわかりません。ご教示頂けませんでしょうか?
html
1// index.html 2<!DOCTYPE html> 3<html lang="ja"> 4<head> 5 <meta charset="utf-8"> 6 <title>test</title> 7<style> 8#testDiv{ 9 width: 100px; 10 height: 100px; 11 background: #CCCCCC; 12} 13</style> 14</head> 15<body> 16<div id="testDiv">test</div> 17<script src="jquery-2.1.4.min.js"></script> 18<script type="text/javascript" src="js/app.js"></script> 19</body> 20</html> 21
javascript
1// js/app.js 2$(function(){ 3 var root; 4 var scripts = document.getElementsByTagName("script"); 5 var i = scripts.length; 6 while (i--) { 7 var match = scripts[i].src.match(/(^|.*\/)app\.js$/); 8 if (match) { 9 root = match[1]; 10 break; 11 } 12 } 13 var xhr = null; 14 if (window.XMLHttpRequest){ 15 xhr = new XMLHttpRequest(); 16 }else if(window.ActiveXObject){ 17 try{ 18 xhr = new ActiveXObject("Msxml2.XMLHTTP"); 19 }catch(e){ 20 xhr = new ActiveXObject("Microsoft.XMLHTTP"); 21 } 22 } 23 24 xhr.open("GET", root + "group.js", false); 25 xhr.send(""); 26 eval(xhr.responseText); 27 28 $('#testDiv').click(function(){ 29 test(); 30 }); 31 32});
javascript
1// js/group.js 2// js/app.jsの中で呼び出したいjsファイル 3$(function(){ 4 var defaults = { 5 'view': 'test' 6 }; 7 8 function test(){ 9 var x = console.log(defaults); 10 return x; 11 }; 12});
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。