Insert JSP inside Javascript

Posted on 2004-08-31
Last Modified: 2008-02-01

I need to determine if the browser supports javascript or not and redirect the user appropriately.  If the user does not support js, I will have to use "response.sendRedirect" in jsp, if not I can use the form action tag.  I want to know if the following code works.  Can anyone confirm if I am doing the right thing.  


      String sRedirect = "";
document.write('JavaScript is turned on!');
function submitForm() {
<body onLoad="submitForm()">
        JavaScript is turned off!
         System.out.println(" -- javascript is turned off ---- ");
      System.out.println(" -- redirect url = " + sRedirect);
      if(sRedirect!=null && sRedirect.length()>0)       
<form name="testForm" action="<%=sRedirect%>" method="post">
Question by:radha77
LVL 35

Expert Comment

ID: 11944847
>      if(sRedirect!=null && sRedirect.length()>0)      
>           response.sendRedirect(sRedirect);

that will send you to the other site via the


in every case (whether you have javascript or not), as the JSP will be executed at the server (before the client gets the page), and you will go to the redirect site before the javascript has a chance to function...

Author Comment

ID: 11945256
Thanks for your response.  Well what I am actually trying to achieve is to reduce the time it takes to go to the other page.  I have seen more than once that "response.sendRedirect(sRedirect);" takes more time than js "window.location".  How can I accomplish this?  Pls let me know.

LVL 11

Expert Comment

ID: 11948580
    if(sRedirect!=null && sRedirect.length()>0)      

This code will execute every time you access this jsp and not depends on whether browser support javascript or not. since javascript will execute after jsp code.
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

LVL 35

Expert Comment

ID: 11949985
I'd just use response.sendRedirect as that is guaranteed to work in every case...

Maybe I'm missing the point though...  could you explain the problem a bit more if that answer isn't good enough? :-)

Expert Comment

ID: 11949992
Hi radha
I would suggest you to display the message "Javascript Not enabled.Either enable JavaScript or Alternatively click on the link"  with a link for an alternate url for the user .

Coding Experts

Author Comment

ID: 11969672

Thanks all for your responses.  I bascially want to avoid response.sendRedirect as it take more time to redirect.  Well I have used meta refresh tags with noscript and it serves the purpose.

<!--When javscript is disabled -->
<p>JavaScript is turned off!</p>
<meta http-equiv="Refresh" content="0;url=<%=sRedirect%>">

<!--when javscript is enabled -->
<form name="testForm" action="<%=sRedirect%>" method="post">


Accepted Solution

CetusMOD earned 0 total points
ID: 12524860
PAQed, with points refunded (50)

Community Support Moderator

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now