radha77
asked on
Insert JSP inside Javascript
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(sRed irect);
%>
</b>
</p>
</noscript>
<form name="testForm" action="<%=sRedirect%>" method="post">
</form>
</div>
</body>
</html>
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
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(sRed
%>
</b>
</p>
</noscript>
<form name="testForm" action="<%=sRedirect%>" method="post">
</form>
</div>
</body>
</html>
ASKER
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(sRe direct);" takes more time than js "window.location". How can I accomplish this? Pls let me know.
Thanks.
Thanks.
if(sRedirect!=null && sRedirect.length()>0)
response.sendRedirect(sRed irect);
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.
response.sendRedirect(sRed
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.
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? :-)
Maybe I'm missing the point though... could you explain the problem a bit more if that answer isn't good enough? :-)
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
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
ASKER
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=<%=sRedirec
</noscript>
<!--when javscript is enabled -->
<form name="testForm" action="<%=sRedirect%>" method="post">
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> response.sendRedirect(sRed
that will send you to the other site via the
response.sendRedirect(sRed
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...