简体   繁体   English

无法在Jade中将Node.js包含wysiwyg.js脚本

[英]Cannot include wysiwyg.js script in Jade with Node.js

While trying to create a rich text editor, I cannot seem to add the js script for the buttons and form. 在尝试创建富文本编辑器时,我似乎无法为按钮和表单添加js脚本。 Right now, most of the functions are inlined in the jade pages, but that is quite unelegant. 现在,大多数功能都内嵌在翡翠页面中,但这很不明确。

I have a layout.jade file: 我有一个layout.jade文件:

doctype html
html
    head
body
    header
    section.content
    block content

And the create.jade script, where I want the rich text editor: 还有create.jade脚本,我想要富文本编辑器:

extends ../layout

block content
    script(type='text/javascript', src='./css/js/wiz.js')
    form(method="post", action=post ? '/post/edit/' + post.id : '/post/create', onload="iFrameOn()")
        ul
         p
         input#title(name="title", placeholder="Title", value=post ? post.title : '', autocomplete='off')
         p
         #wysiwyg_cp(style="padding:8px; width:700px")
             input(type="button", onClick="iBold();", value="B")
             input(type="button", onClick="richTextField.document.execCommand(\"underline\",false,null);", value="U")
         p
         textarea#blogPostBody(style="display:none", name="blogPostBody", placeholder="Blog post text", rows="20", cols="50")
         iframe#richTextField(name="richTextField", contentEditable="true", onLoad="richTextField.document.designMode = 'On';")
                if(post)
                    | #{post.body}
                else
                    | &#09
         p
         input(onClick="javascript:submit_form();", type="button", name="myBtn", value="Save")

The structure of the project looks like: 该项目的结构如下:

Proj
- css
 -- js
  --- wiz.js
- middleware
- node_modules
- public
- routes
- views
 -- post
  --- create.jade
 -- layout.jade
 -- home.jade
 -- login.jade
- app.js
- package.json

When trying to open the create section of my blog, I get the following error message, as can be seen in the image below: 尝试打开博客的create部分时,收到以下错误消息,如下图所示:

Uncaught SyntaxError: Unexpected token < 

See chrome stack: http://i.stack.imgur.com/JyHs2.png 请参阅Chrome堆栈: http//i.stack.imgur.com/JyHs2.png

The wiz.js file looks like this: wiz.js文件如下所示:

function iFrameOn() { richTextField.document.designMode = 'On'; }

function iBold() { richTextField.document.execCommand("bold",false,null); }

function iUnderline(){ richTextField.document.execCommand("underline",false,null); }

function iItalic(){ richTextField.document.execCommand("italic",false,null); }

function iImage() {
    var imgSrc = prompt("EnterImageLocation", '');
    if(imgSrc!=null) {
        richTextField.document.execCommand("insertimage",false,imgSrc);
    }
}

function submit_form() {
    var theForm = document.getElementById("myForm");
    theForm.elements("myTextArea").value = window.frames['richTextField'].document.body.innerHTML;
    theForm.submit();
}

I have also tried adding 我也尝试添加

 app.set('view options', {locals:{scripts:['wiz.js']}});

and/or adding 和/或添加

app.use(express.static("./css/js"));

In the app.js file or in the middleware. 在app.js文件或中间件中。 But these did not help. 但是这些没有帮助。 The problem seem to be in the "create.jade" script, when referencing the 'text/javascript' script, because the same error was obtained even when referencing a non-existant js file. 引用“ text / javascript”脚本时,问题似乎出在“ create.jade”脚本中,因为即使引用了不存在的js文件,也会获得相同的错误。

Could anyone have any idea what the problem could be? 谁能知道可能是什么问题?

[EDIT] SOLUTION that was implemented: [编辑]已实施的解决方案:

script
    include ./../../css/js/wiz.js

以下代码段有效:

script include ./../../css/js/wiz.js

Express's static middleware will by default mount the files to the root. Express的静态中间件默认情况下会将文件安装到根目录。 So, if you include the middleware as above, the wiz.js file will be accessible at "/wiz.js" when your server's running -- this is the path you should put in the src attribute of your script tag in the jade file, not the local path (since local files are not accessible to the client unless they are served). 因此,如果您包含上述中间件,则在服务器运行时,可通过“ /wiz.js”访问wiz.js文件-这是应该在jade文件中的script标签的src属性中添加的路径,而不是本地路径(因为除非提供本地文件,否则客户端无法访问本地文件)。 If you want to change the mountpath (so, for example, that the file will be accessible on "/js/wiz.js" , rather than the root), you can add it as a first argument, like below: 如果要更改安装路径(例如,可以在“ /js/wiz.js”而不是根目录下访问该文件),则可以将其添加为第一个参数,如下所示:

var localDirectoryPath = "./css/js";
// Or if you happened to want an absolute path
// (though it doesn't make a difference here):
// var localDirectoryPath = require("path").join(__dirname, "css", "js");

app.use("/js", express.static(localDirectoryPath));

The temporary solution that you've found (using Jade's include function) is merely loading the file's contents on the server (when Jade compiles the template) and inserting the code in between script tags on your page, which works but is certainly not ideal, since it will slow down the initial loading of your HTML, especially as the wiz.js file gets bigger. 您发现的临时解决方案(使用Jade的include函数)只是将文件的内容加载到服务器上(当Jade编译模板时),然后将代码插入页面上的脚本标签之间,这虽然可行,但当然并不理想,因为它会减慢HTML的初始加载速度,尤其是wiz.js文件变大时。

And I won't even ask why your javascript directory is inside your css directory! 而且我什至不会问为什么您的javascript目录位于您的css目录中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM