2017-06-21 83 views
-2

,所以我想有路由文件傳遞的需要將會收錄頁面的JavaScript文件的列表,並有EJS構築頭部以包括這些腳本節點EJS JavaScript動態負載

我的路線文件

router.get('/:name', function(req,res, next) { 
    res.render('templates/experiment', { 
    name:req.params.name, 
    extraScripts:experiments[req.params.name].extraScripts 
    }) 
}); 

我experiment.ejs

<head> 
    <title><%= name %></title> 
    <script src="../javascripts/p5/p5.js"></script> 
    <script src="../javascripts/p5/addons/p5.dom.js"></script> 
    <script src="../javascripts/p5/addons/p5.sound.js"></script> 
    <% for(var i = 0; i < extraScripts.length; i++) { %> 
    <script scr=<%= "../javascripts/" + extraScripts[i]%> ></script> 
    <% }%> 
    <script src=<%= "../javascripts/experiments/"+name + "/sketch.js"%>></script> 
    <style> body {padding: 0; margin: 0;} </style> 
</head> 

現在,當我加載網頁,我可以看到腳本標記得很好,但它沒有做額外的腳本的實際負荷。

<head> 
    <title>snake</title> 
    <script src="../javascripts/p5/p5.js"></script> 
    <script src="../javascripts/p5/addons/p5.dom.js"></script> 
    <script src="../javascripts/p5/addons/p5.sound.js"></script> 

    <script scr=../javascripts/library/colors.js ></script> 

    <script src=../javascripts/experiments/snake/sketch.js></script> 
    <style> body {padding: 0; margin: 0;} </style> 
</head> 

colors.js文件沒有加載我缺少的東西,sketch.js是動態的,它加載得很好。

回答

0

。在你的代碼 使用SRC到位SCR的錯字。

還嘗試用雙引號中的src路徑如下

<head> 
    <title><%= name %></title> 
    <script src="../javascripts/p5/p5.js"></script> 
    <script src="../javascripts/p5/addons/p5.dom.js"></script> 
    <script src="../javascripts/p5/addons/p5.sound.js"></script> 
    <% for(var i = 0; i < extraScripts.length; i++) { %> 
    <script src="<%= "../javascripts/" + extraScripts[i]%>" ></script> 
    <% }%> 
    <script src="<%= "../javascripts/experiments/"+name + "/sketch.js"%>"></script> 
    <style> body {padding: 0; margin: 0;} </style> 
</head> 
+0

哇通常當我輸入了這樣我趕上那些愚蠢的錯別字問題,謝謝 – Bill