How to use scriptlet inside javascript

后端 未结 4 1678
半阙折子戏
半阙折子戏 2020-11-30 15:23

Can someone test this example and share the results? http://timothypowell.net/blog/?p=23
When I do:

var myVar = \'<% request.getContextPath(); %>\'         


        
4条回答
  •  忘掉有多难
    2020-11-30 15:44

    You cannot run scriptlet inside javascript by giving it normal .js extension. However you can give your .js the file extension of .jsp and then and link directly to it as:

    
    

    and now you can use jsp within your javascript like:

    var someMessage = "${someMessage}"
    var anotherMessage = "${anotherMessage}"/>"
    

    This action is completely valid because what determines whether a file is a javascript file or not is what MIME media type. You can set MIME from JSP using:

    <%@ page contentType="text/javascript" %>
    

提交回复
热议问题