Solved

How to assign JSP Variable to JavaScript

Posted on 2007-11-16
9
6,022 Views
Last Modified: 2012-08-13
Hi
i have a function like below
    function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id");
       var proId= '<%=pId%>';
      }%>
    }
i am not able to assign the pid value to proId it is showing null..where as i am getting the id value
please let me whethe i am properly assign this jsp value in java script variable
0
Comment
Question by:knaren_1975
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20299014
I don't know much about jsp, but I would think this would be proper:

 function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id");
       }
    %>
       var proId= '<%=pId%>';
    }

Open in new window

0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 20299022
Or this, depending on which part is javascript and which is jsp
function sortResults(sortOption) {
    if(request.getAttribute("id")!=null){
    <%
             String pId = (String)request.getAttribute("id");
    %>
       var proId= '<%=pId%>';
    }
    }

Open in new window

0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20299048

function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id"); 
%>
       var proId= '<%=pId%>';
  <%
    }%>
    }

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:knaren_1975
ID: 20299156
it's not working...

i am getting the request.getAttribute("id") value but i am not able to assign this value into javascript varible

i but some debug statement then i can see the value coming into this.
when i add  the code var proId= '<%=pId%>'; my jsp page itself is not opening just saying  null in this Jsp

Regards
Naren
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20299181
did u try
function sortResults(sortOption) {
    <% if(request.getAttribute("id")!=null){
             String pId = (String)request.getAttribute("id"); 
%>
       var proId= '<%=pId%>';
  <%
    }%>
    }

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20299223
function sortResults(sortOption) {
  var proId= '<%= (request.getAttribute("id")!=null)?(String)request.getAttribute("id"):"" %>';
}
0
 
LVL 11

Accepted Solution

by:
Manish earned 500 total points
ID: 20317713
Just some modification in given code.
function sortResults(sortOption) {

    <%  String pId="";
    if(request.getAttribute("id")!=null){
              pId = (String)request.getAttribute("id");
%>
       var proId= '<%=pId%>';
  <%  
   }   %>
    }
0
 

Author Closing Comment

by:knaren_1975
ID: 31409571
THx
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20712577
Did you try mine?
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

717 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