<script language="javascript" >
function addSelection(){
var tst="hello";
sessionStorage.setItem("test", tst);
<%
String pastst=request.getParameter("tst");
session.setAttribute("tst1",pastst);
%>
addSelection1();
}
function addSelection1(){
var outtst = '<%= session.getAttribute("tst1") %>';
<%
out.print("session"+session.getAttribute("tst1"));
%>
var tstout= session.getAttribute("tstout");
alert(tstout);
}
</script>
alert(tstout); works properly, but the code inside jsp prints blank.
I don't know what is the purpose behind this. Anyway few issues found in your code.
First. You cannot get the javascript variable value like this,
var tst="hello"; //Setting the value in javascript
sessionStorage.setItem("test", tst);
<%
String pastst=request.getParameter("tst"); //Expecting the value will be returned by java request
session.setAttribute("tst1",pastst);
%>
Second. Following line will provide you JS error, You cannot assign like this var tstout= session.getAttribute("tstout");
You need to change into, var tstout= '<% session.getAttribute("tstout"); %>'
Its wrong assumption javascript will print the session value in the following line,
<%
out.print("session"+session.getAttribute("tst1"));
%>
Finally, you need to start learning JSTL and EL avoid using scriptlets.
This is the way some where it is working.
<script type="text/javascript" >
function addSelection(){
var tst="hello";
sessionStorage.setItem("test", tst);
<%
String pastst="hello";
session.setAttribute("tst1",pastst);
%>
addSelection1();
}
function addSelection1(){
var outtst = '<%= session.getAttribute("tst1") %>';
alert('<%
out.print("session : "+session.getAttribute("tst1"));
%>');
alert(outtst);
}
</script>
Hope it helps you.
Related
Whenever I attempt to set a variable within a script to be equal to an array rendered using ejs I get 'Invalid are unexpected token'.
Here is the snippet:
<script type="text/javascript">
var list = <%= events.slice(0) %>;
</script>
Originally I had it set to:
<script type="text/javascript">
var list = <%= events %>;
</script>
But I receive the same error. I was pretty certain you could render an ejs file that contains script tags and set the ejs variable to a variable within the script tag. Am I wrong?
Events is an array of event models for a calendar.
this is what you are looking for:
<script type="text/javascript">
var list = <%-JSON.stringify(events)%>;
</script>
You don't need to add script tags. Just put your code in html
Here is a plain example
index.ejs
<script>
//just call the events variable
for(var i = 0; i < events.length; i++){
var myEvent = events[i];
//handle the event
}
//then use the variables outside the script tag after you are done with all the handling
</script>
<ul>
<li><%=myDefinedVariable%></li>
<% var name = "Jake" %>
<li><%=name%></li>
<%for(var i = 0; i < 10; i++){%>
<li><%=name%></li> // puts li in the ul 10 times
<%}%>
</ul>
I have a jsp page that looks like the following:
What the intent is, is to get the current environment via a system variable and then generate the correct url for that environment.
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%# taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<%
String sysEnv = "";
if(System.getProperty("env.name").equals("test")) {
sysEnv = "test";
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
<head>
<%# include file="/includes/common.jsp" %>
<script type="text/javascript" src="<c:out value="${requestScope.scriptsLocation}"/>/scripts/general.js"></script>
<c:if test="${requestScope.isFeed == true}">
<jsp:include page="/includes/someInclude.jsp"/>
<script src="http://www.myothersite.com/feed/d/some.js" type="text/javascript"></script>
<script type="text/javascript">
var environ = <%=sysEnv%>;
if(environ == 'test'){
var theUrl = 'http://mywebsite.com?isFeed=true&env=TEST';
} else {
var theUrl = 'http://mywebsite.com?isFeed=true';
}
...
</script>
...
I think I'm supposed to have the tags-logic import to do the looping that I'm trying to achieve in Java, but this is not working.
Since you're already in a JSP why not just do (roughly) this:
<script>
var url = 'http://mywebsite.com?isFeed=true';
if ('<%= System.getProperty("env.name") %>' === 'test') {
url += '&env=TEST';
}
// etc.
Personally I'd move this logic out of the view and create a request-scoped attribute in a filter, as part of your Struts 1 request processor, etc. since scriptlets are almost always bad. It also gives you the opportunity to set it at runtime.
In addition, the URL generation should also likely be moved out of the view layer.
I am wondering if i can get the session value and put as a parameter to a control i have.
This is the code I currently have:
<script type="text/javascript">
var workspacepath = '<%= Session["workspacepath"].ToString() %>';
var workspacename = '<%= Session["workspacename"].ToString() %>';
</script>
<iz:FileManager ID="FileManager1" runat="server" Height="550" Width="1016px" FileViewMode="Details"
ClientOpenItemFunction="funcinvisiblebutton1">
<RootDirectories>
<iz:RootDirectory DirectoryPath="~/" Text="Workspace Etteplan" />
</RootDirectories>
</iz:FileManager>
</div>
I want the session value in DirectoryPath="~/".
You can set attribute value :
document.getElementsByTagName("iz:RootDirectory")[0].setAttribute("DirectoryPath",workspacepath);
I have to use some inline code in my asp.net application.
F.e. i have following inline code:
<% FlashRenderer.Render(); %>
That will return me following markup:
<div id="flashGame" style="color:red"></div>
<script type="text/javascript" language="javascript">
var flash = document.getElementById('flashGame');
...
</script>
</div>
I need to assign a result of FlashRenderer.Render() to javascript variable, then using jquery
append that markup to some parent div. I try follwing:
<script type="text/javascript">
var swfString = '<%= FlashRenderer.Render() %>';
$("swf").append(swfString);
</script>
And it fails, because result string from inline inclusion has both single and double quotes, so when i use single quotes to wrap '<%= FlashRenderer.Render() %>' i always have a syntax error in browser console.
I try to replace "'" with "\'" but error happens before i got string created.
Here is screenshot what i have after try to use JavaScriptSerializer().Serialize
You can use a JSON serializer, which will give you a Javascript-safe string, including the quotes, and it will handle the line breaks as well:
--JSON.NET--:
var x = <%= JsonConvert.SerializeObject(FlashRenderer.Render()) %>;
or
--System.Web.Script.Serialization--:
var x = <%= new JavaScriptSerializer().Serialize(FlashRenderer.Render()) %>;
This will render something like:
var x = "<div id=\"flashGame\" style=\"color:red\"></div>\r\n <script type=\"text/javascript\" ...etc... </div>";
Instead of trying to place it in a string, just have it render hidden on the page and move it:
<div style="display:none">
<%= FlashRenderer.Render() %>
</div>
<script type="text/javascript">
$("swf").append($("#flashGame"));
</script>
Hacky solution if you can't modify the C# code. Output the function into a textarea then get the value of the textarea with jQuery.
<textarea id="swfref" style="display:none;">
<% FlashRenderer.Render(); %>
</textarea>
<script type="text/javascript">
var swfString = $("#swfref").val();
$("swf").append(swfString);
</script>
Well i have 2 aspx
page1 you are going to have amm two buttons or it dontmind but the goal is...
when user press button1 in page1.aspx, session["x"] will be equals to "ok"
if users press button2 in page1.aspx, session["x"] will be eauls to null;
so after press any button user will be redirected to page2.aspx.
i want to execute a javascript function if session is equals to "ok" and not to do it
if session is null, someidea? when page2.aspx is open javascript function will be done automatically, if it is null, javascript function will not be nothing or it will not be executed. some idea? thanks guys!
it worked so...
the real code was it..
<% if (Session[#"codigodiagrama"] != null){%>
<script type="text/javascript">
window.onload= function () {
alert('with code');
}
</script>
<% } %>
<% if (Session[#"codigodiagrama"] == null){%>
<script type="text/javascript">
window.onload= function () {
alert('null');
}
</script>
<% } %>
now i want to pass the session["codigodiagrama"] to the function
if it has code to pass the String (codigodiagrama)
i refer how to pass this session to this windows.onload =function(pass code here)
In your page2 aspx you could have something like below...
<% if(Session[#"x"] == #"ok") { %>
<script type="text/javascript">
SomeFunction();
</script>
<% } %>
EDIT
In response to additional information in question...
You can achieve your goal like so..
<% if (Session[#"codigodiagrama"] != null){%>
<script type="text/javascript">
window.onload = function () {
alert('with code <%= Session[#"codigodiagrama"] %>');
};
</script>
<% } %>
Alternative:
in the page2 aspx in the code behind:
declare Public sessionX as string
in the page_init event set sessionX = session("x")
use it in the javascript function
var x
x = '<%=sessionX%>';
if (x.toString() == 'ok') {
alert('hello');}