Solved

How to assign JSP Variable to JavaScript

Posted on 2007-11-16
9
5,945 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
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 18

Expert Comment

by:Morcalavin
Comment Utility
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
Comment Utility
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
Comment Utility

function sortResults(sortOption) {

    <% if(request.getAttribute("id")!=null){

             String pId = (String)request.getAttribute("id"); 

%>

       var proId= '<%=pId%>';

  <%

    }%>

    }

Open in new window

0
 

Author Comment

by:knaren_1975
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
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
Comment Utility
function sortResults(sortOption) {
  var proId= '<%= (request.getAttribute("id")!=null)?(String)request.getAttribute("id"):"" %>';
}
0
 
LVL 11

Accepted Solution

by:
Manish earned 500 total points
Comment Utility
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
Comment Utility
THx
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Did you try mine?
0

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

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…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

763 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

5 Experts available now in Live!

Get 1:1 Help Now