Monday 15 March 2010

java - Interaction between JSP and External javascript files -


I'm a newbie for external javascript files (* .js) Actually my JSP is ready but my manager wants to add graphics to me.

Then I got some * .js files but I do not know how to interact between them and my JSP page.

I want to pass data from jsp file to an external .js file. Is there any mechanism to do this?

For example: -

Demo. JSP

  out.print (request.getAttribute ("name"));   

Now I want to use / pass / set in the main.js file how to do this?

  & lt; Script type = "text / javascript" & gt; Var myJavascriptVariable = & lt;% = request.getParameter ("name")%> // or .getAttribute ("name") & lt; / Script & gt;   

This can do the trick, it will create a global variable which is the main one. Can be reached in JS. When you receive the GET parameter, you can only use JS:

  var prmstr = window.location.search.substr (1); Var prmarr = prmstr.split ("& amp;"); Var Params = {}; {Var tmparr = prmarr [i] .split ("=") for (Var i = 0; i & lt; prmarr.length; i ++); Param [TMPR [0]] = TMPR [1]; }   

Now you can use the parameter via js:

  params ['name']   < P> Personally I'll use AJAX (like with the help of JQuery) to get data for my Javascript files, you   

No comments:

Post a Comment