简体   繁体   中英

Include .js file within an .ejs file

I am attempting to include a js file within my ejs file and it's not working. I'm not sure what is wrong with my syntax.

Heres my create player form:

<% // create a list of javascript files to have the header load %>
<% var js_include = ['create_player.js']; %>
<% include header %>

<!-- Note that this is a post -->
<form id="create_player" action="<%= action %>">
<table id="login">
    <tr><td>Player Name:</td>
        <td><input type="text" id="Player_Name" name="Player_Name" /></td>
    </tr>
    <tr><td>Position:</td>
        <td><input type="text" id="Position" name="Position" /></td>
    </tr>
    <tr><td>College:</td>
        <td><input type="text" id="College" name="College" /></td>
    </tr>
    <tr><td>High School:</td>
        <td><input type="text" id="High_School" name="High_School" /></td>
    </tr>
    <tr><td>City:</td>
        <td><input type="text" id="City" name="City" /></td>
    </tr>
    <tr><td>Player Number:</td>
        <td><input type="text" id="Player_Number" name="Player_Number" /></td>
    </tr>
    <tr><td>Height:</td>
        <td><input type="text" id="Height" name="Height" /></td>
    </tr>
    <tr><td>Weight:</td>
        <td><input type="text" id="Weight" name="Weight" /></td>
    </tr>
    <tr><td>Goals:</td>
        <td><input type="text" id="Goals" name="Goals" /></td>
    </tr>
    <tr><td>Assists:</td>
        <td><input type="text" id="Assists" name="Assists" /></td>
    </tr>
    <tr><td>Points:</td>
        <td><input type="text" id="Points" name="Points" /></td>
    </tr>
</table>

<input type="button" id="createPlayerBtn" value="Create" />
</form>

<div id="output" style="margin:10px;display:none"></div>

<% include footer %>

The problem is on the line:

<% var js_include = ['create_player.js']; %>

Thanks in advance

If 'create_player.js' is a Js script and its not related with the business logic, I mean, if Its not a controller you can use script tag.

If you are using express you can put the .js file in public javascripts and use the following

<script type="text/javascript" src="javascripts/*.js"></script>

If you file is related to the business logic, for example create a player or something like that you have to use it in server and call it in a route, if this is the case you should check the express documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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