Solved

Insert JSP inside Javascript

Posted on 2004-08-31
8
1,211 Views
Last Modified: 2008-02-01
Hi,

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.  

Thanks.

<%
      String sRedirect = "http://www.yahoo.com";
%>
<html>
<head>
<title>Test</title>
</head>
<script>
document.write('JavaScript is turned on!');
function submitForm() {
  document.testForm.submit();
}
</script>
<body onLoad="submitForm()">
<noscript>
  <p>
   <b>
        JavaScript is turned off!
   <%
         System.out.println(" -- javascript is turned off ---- ");
      System.out.println(" -- redirect url = " + sRedirect);
      
      if(sRedirect!=null && sRedirect.length()>0)       
            response.sendRedirect(sRedirect);
      %>
   </b>
  </p>
</noscript>
<form name="testForm" action="<%=sRedirect%>" method="post">
</form>
</div>
</body>
</html>
0
Comment
Question by:radha77
8 Comments
 
LVL 35

Expert Comment

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

that will send you to the other site via the

          response.sendRedirect(sRedirect);

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...
0
 

Author Comment

by:radha77
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.

Thanks.
0
 
LVL 11

Expert Comment

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

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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:TimYates
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? :-)
0
 
LVL 6

Expert Comment

by:CodingExperts
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
0
 

Author Comment

by:radha77
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 -->
<noscript>
<p>JavaScript is turned off!</p>
<meta http-equiv="Refresh" content="0;url=<%=sRedirect%>">
</noscript>

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


0
 

Accepted Solution

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

CetusMOD
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need to view directory list in apache/tomcat 9 109
jersey maven project error 7 138
How to set default webapp for host 6 45
spring JDBC Template example error 26 207
There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

14 Experts available now in Live!

Get 1:1 Help Now