I have a condition in php based on some session variables. If condition passes, I want to call a Javascript function which is updating my current webpage. Is it possible to place such a call to Javascript function independent of a button or a link or the window load function?
5
-
If it's not going to be called on the window load function when would you like it called?Cory Danielson– Cory Danielson2011-12-06 19:25:58 +00:00Commented Dec 6, 2011 at 19:25
-
Why not call it from the process that changes the session variable(s)?David Thomas– David Thomas2011-12-06 19:27:58 +00:00Commented Dec 6, 2011 at 19:27
-
actually the flow is somthing like 1. enter login details (on home page) -> 2. navigation to login.php to authenticate -> 3. come back to home page with error/success -> 4. based on authentication result modify the home page. I don't want to display a page inbetween and want user to believe there is only one page.Mandar Joshi– Mandar Joshi2011-12-06 19:31:59 +00:00Commented Dec 6, 2011 at 19:31
-
1If I understand this well, you want javascript to read session variables. Javascript can't. But it can read cookies (explained here). So why not to copy relevant session to cookies?Jan Turoň– Jan Turoň2011-12-06 19:38:18 +00:00Commented Dec 6, 2011 at 19:38
-
Please clarify if this check can be executed whenever the page is reloaded, or needs to to be more re-active, kind of like Stackoverflow when a new answer is posted while your editing your own answer. I think that is a key point to providing a proper answer to this question.Zoidberg– Zoidberg2011-12-06 21:25:11 +00:00Commented Dec 6, 2011 at 21:25
Add a comment
|
2 Answers
If I am understanding you correctly, just use php to output the javascript function. It will fire once, if you need.
<?php
if ($_SESSION['condition']=="foo") {
echo "<script>","/n";
echo "doThisNow();","\n";
echo "</script>","\n";
}
?>
Or, you could echo out the session variables as javascript variables in the same way, and then do the conditional check in javascript.
5 Comments
Mandar Joshi
Cymbals, i tried and it works if i write the javascript code there itself. but when i try to call an existing function from an existing javascript source it does not work. for example below code is not working<?php if ($_SESSION['Error']=='true') {$_SESSION['Error']='false' ?> <script type="text/javascript" src="login.js"> updateLoginContent(); </script> <?php } ?>
Pateman
@Mandar, put
<script type="text/javascript" src="login.js"> </script> in the HEAD section of your document and change the condition to <script type="text/javascript"updateLoginContent();</script>Mandar Joshi
Solved now :) <?php if ($_SESSION['Error']=='true') {$_SESSION['Error']='false' ?> <script> updateLoginContent(); </script> <?php } ?> Thanks Cymbals.
Mandar Joshi
Pateman, yes was a little bit confused but corrected in the above comment. Thanks for your suggestion.
Cymbals
You're welcome, Mandar! Please mark this as the correct answer if this works for you.
As proposed in another answer you can output a Javascript call whenever the page is loaded.
However, if your event cannot wait until the page is reloaded, then I suggest you setup some javascript to poll a page every X number of seconds (using AJAX) and do something when the values change.
3 Comments
nrabinowitz
I like this answer, assuming it meets the OP's needs, though I might rephrase as "This is possible", since your proposed solution is perfectly reasonable if the session variables are changing arbitrarily outside the context of the page (like SO's async "One new answer has been posted" functionality).
Cymbals
As an fyi, AJAX won't load the session variables by design, unfortunately. It treats it as a separate instance. Hopefully this prevents self-inflicted head trauma.
Zoidberg
That is not true at all. An ajax request, in the server's eyes, is no different than a request.