1

i Want to import a given css or javascript file depending os some conditions, in my Servlet i have:


protected void doPost(...)
{
   if(condition)
   {
     //import some javascript or css file here
   }

}

I need this behavior since i have too many files to import and the files name may vary according to the condition. Is it possible?

3 Answers 3

4

Sort of, yes.

boolean condition = evaluateItSomehow();
request.setAttribute("condition", condition);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

Then in page.jsp using JSTL c:if:

<head>
    <c:if test="${condition}">
        <link rel="stylesheet" type="text/css" href="style.css">
        <script type="text/javascript" src="script.js"></script>
    </c:if>
    ...
</head>

Update: since you seem to have more than one files for this, you can even make it more flexible by just setting the desired filename suffix (or prefix, or even the entire name, what you like):

String suffix = evaluateItSomehow();
request.setAttribute("suffix", suffix);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

and

<head>
    <link rel="stylesheet" type="text/css" href="style_${suffix}.css">
    <script type="text/javascript" src="script_${suffix}.js"></script>
    ...
</head>

If you set suffix to for example "foo", this will load style_foo.css and script_foo.js. I think this gives enough new insights.

Sign up to request clarification or add additional context in comments.

1 Comment

hello :) is there any problem with the bwoser cache when using this method? if i want to load same page, on different conditions, using many kinds of css? wil the client browser cache the css?
1

Are you trying to insert Javascript and CSS into the dom? I would do that from the client side. I mean, it is possible to do it by explicitly writing out the code for <script...> or <link...>. A better way to do it is to send something back to the client that tells it to add a stylesheet or Javascript.

Then you can add it dynamically like so:

If this doesn't need to be done dynamically, then it is even easier. Simply set a flag and in your JSP or ASP, check the state of the flag. Within the conditional tag, you will add the code for your CSS and Javascript. However, I am assuming from your question that it is the former.

Comments

0
function loadjscssfile(filename, filetype) {
            if (filetype == "js") { //if filename is a external JavaScript file
               // alert('called');
                var fileref = document.createElement('script')
                fileref.setAttribute("type", "text/javascript")
                fileref.setAttribute("src", filename)
                alert('called');
            }
            else if (filetype == "css") { //if filename is an external CSS file
                var fileref = document.createElement("link")
                fileref.setAttribute("rel", "stylesheet")
                fileref.setAttribute("type", "text/css")
                fileref.setAttribute("href", filename)
            }
            if (typeof fileref != "undefined")
                document.getElementsByTagName("head")[0].appendChild(fileref)
        }

Call this javascript function to dynamically load the css and js file. Pass the complete file path with name in ‘filename’ argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.