I have the following code within the tag of my page:
<script>
function LogInOut()
{
// Get the current login status
alert("executing LogInOut");
$loginStatus = "<?php echo $_SESSION['login']; ?>";
if ($loginStatus == "true")
{
<?php
echo "<br />script function";
$_SESSION['login'] = "false";
session_destroy();
?>
document.getElementById("loginState").innerHTML = "login";
}
else
{
window.location = 'login.php';
}
}
</script>
I find that the php code executes when the page loads. The function (for debugging) is NEVER called yet the php code executes while none of the rest of the script executes! Can anyone clarify why this could be happening?
thank you,
Shimon
Classic case of mixing Javascript, a client side language, with PHP, a server side language. They run at two different locations and that being said this will never be possible.
PHP runs before javascript and if your trying to mix it with javascript, use it to echo dynamic data. eg:
var logged_in = <?=($_SESSION['login'] ? 'true' : 'false')?> ;
Javascript runs within the browser and after PHP, do not use php code thinking it will run inside of the browser
You will need something to call the function
In your case, call it when page is ready.
$(document).ready(function(){
LogInOut();
});
Or using a console for modern browser type in:
LogInOut();
Related
I'm on a Java EE project and I need to use the value of a JS var on my Java code but my java code is execute before my JS (logic)
But I need the value of a JS var to execute my java and I think it will work if I execute my js on server-side but I don't know how to do this ...
My code is something like this :
JS :
function(){
var url = "an url of 20.000 char that I can't pass in GET and who is automatically generated by google chart API"
}
Java :
<%
String urlChart = "value of my "url" var in js";
session.setAttribute("urlChart", urlChart);
%>
But I don't know how tu put the value of my JS var un my java code. Can you help me ?
Somebody said me that I have to use AJAX but I don't know how to use it.
That is not possible in my understanding. you must switch to some other alternate solutions. you can use CGI or PHP or AJAX.
http://www.w3schools.com/ajax/
You can check this similiar question: how to send a string to a servlet from javascript using xmlhttprequest
If this won't help, then you need to provide more details because i'm not sure if i understand your problem.
I have implemented a simple browser in java which load a html page(I wrote it), I want to call my javascript function, in my java my class!
I try the following code, but it didn't execute the script!
browser.execute("<script type='text/javascript'> "
+"loadMap("+lat+","+lng+");"
+" </script>");
Try
browser.execute("loadMap("+lat+","+lng+");");
You don't need to create a script tag to execute the script. Just execute the actual JavaScript code. Refer to this sample.
basically my problem is when I want to mix javascript with java code since I did not take the variable (var nombreRodamiento javascript) when I put the "<%" to start putting java code.
please note the bold line, which is what the compiler does not like.
<script type="text/javaScript">
function moveToRightOrLeft(side) {
var listLeft = document.getElementById('selectLeft');
var listRight = document.getElementById('selectRight');
if (side == 1) {//izquierda
if (listLeft.options.length == 0) {
alert('Ya aprobaste todos los items');
return false;
} else {
var rodamientoElegido = listLeft.options.selectedIndex;
var nombreRodamiento = listLeft.options[rodamientoElegido].text;
move(listRight, listLeft.options[rodamientoElegido].value,
listLeft.options[rodamientoElegido].text);
listLeft.remove(rodamientoElegido);
<%
**String nombreRodamiento = '%> nombreRodamiento;<%'**
for (int i=0;i<listaItems.size();i++){
if (listaItems.get(i).equals(nombreRodamiento))
listaItems.remove(i);
}
%>
if (listLeft.options.length > 0) {
listLeft.options[0].selected = true;
}
}
}
}
</script>
Regards
Assuming that this is all inside of a JSP. The java code (scriptlet, everything inside the <% %> tags) will execute server side, and the javascript will execute client side (in the user's browser). Yet you seem to be assigning a java variable the value of a javascript variable, nombreRodamiento. That is not going to work. The javascript is just text, with no values, execution context, etc, whenever the scriplet is being evaluated.
Java strings require double quotes, and you're missing a semicolon, which Java will not automatically insert.
Assuming this is a part of jsp file java code and js code executes separately. first java code get execute on server side and then the javascript code and that is on client side. Infact the java scriplet in jsp renderes the js code to be executed later on client which in this case is a browser.
Hence one cannot assign javascript variable value to a java variable but the reverse is possible.T
Edit:
I can give you the steps as I dont know your server side implementation.
render the page. You must be rendering the list by some type of array or equivaletn object on server side. save both right and left side element on session.
when the list box do some element exchange. exucute your js code.
you have to update the same on server side so send the selected element index and side information to server side using ajax.
update the server side list objects in the session accordingly. Update db if needed.
From next time render this list box suing this objects on ths server side sessions.
Hope this helps.
var result = null;
function setSendButton(userInput){
var clicked=userInput;
result = "<%=mb.myMethod(clicked)%>";
}
where myMethod is a java method called through using jsp tags. it is defined as:
public boolean myMethod(String isClicked){
if(isClicked.equals("true")){
return true;
}else{
return false;
}
}
for some reason stepping i got a JSP compilation error that compiles the code where var clicked value of "true" is not passed and clicked becomes a string during JSP compilation like so: mb.myMethod(clicked) instead of mb.myMethod("true")
It can't work like that.
The Java code in JSP is translated and compiled on the server side before sending to the client's browser. The javascript variable is available only after the JSP is translated and compiled to become a HTML file and sent to the client browser. At that time, The mb.myMethod is already already executed on the server side.
In short, you can passed java code to js assignment but not the other way around.
Wouldn't this work?
var result = null;
function setSendButton(){
result = "<%=mb.myMethod(true)%>";
}
I have an ASP.NET page which loads a Java Applet. I am first checking that the client computer has the ability to run the applet (Java version etc) by giving the tag an ID of "AppletID" - then I call a function of this applet which always returns "true".
So, the following line of code: var isRunning = document.getElementById('AppletID').appletRunning() will return "true" if the method "appletRunning" in the applet is called successfully (indicating that the client can load the applet correctly).
This was always working until recently. However, lately, Firefox browsers give me an intermettent dialog box showing the "You do not have the required minimum version of Java..." message. (Other times it correctly detects that the applet can be loaded). The applet then proceeds to load correctly, but the dialog should not be shown in the first place. I wonder why this is happening - it possibly could be that document.getElementById('AppletID') is being null when it is checked, thus leading to the "catch" part of the "checkAppletRunning" method? IE is always OK and never returns this dialog box.
Below is the code of the .aspx page.
<script type="text/javascript" language="javascript">
window.onload = function()
{
var appletCheck = checkAppletRunning();
if (appletCheck == 1)
{
alert("Server is down...please try again later.");
window.location.href = "Default.htm";
}
else if (appletCheck == 2)
{
alert("You do not have the required minimum version of Java enabled or installed. Java must be enabled or downloaded from http://www.java.com");
window.location.href = "Default.htm";
}
}
function checkAppletRunning()
{
var OK = 0;
var serverDown = 1;
var appletNotSupported = 2;
try
{
var isRunning = document.getElementById('AppletID').appletRunning();
if (isRunning)
{
return OK;
}
else
{
return appletNotSupported;
}
}
catch (e)
{
return appletNotSupported;
}
}
</script>
I would appreciate any help in this matter,
Thanks in advance,
Timothy
may be that the applet has not finished starting up.
try returning different result for an exception (or log the exception) to get a better idea why its failing.
A simpler approach is to rely on the fact that if Java is not available then the APPLET tag is not interpreted.
<HTML><HEAD></HEAD><BODY>
<APPLET CODE="MyApplet.class"
NAME="myApplet"
HEIGHT=400 WIDTH=400>
Oups! You don't have JAVA enabled, click here.
</APPLET>
</BODY></HTML>