0

i have 2 pages, Login page and Contents page

--Login.php I have a div with a message 'hi'

--Contents.php I have a logout button, clicking on which calls a javascript function logout()

--Javascript function

function logout(){
window.location='logout.php';
}

--logout.php

header('Location:Login.php');
exit;

After clicking on logout button in Contents.php page it switches successfully to Login.php page but i am not able to update the div in Login.php page

Anubhaw
  • 5,978
  • 1
  • 29
  • 38
user475685
  • 8,041
  • 7
  • 26
  • 24
  • We need more info on how you're trying to update the div currently – rcapote May 30 '11 at 08:28
  • i am not able to update the div currently. In the javascript function i.e. after window.location='logout.php' i am using document.getElementById('messageid').innerHTML='new message'; – user475685 May 30 '11 at 08:43
  • are you trying to update the login.php page from a script in the contents.php page? once you've done that first redirect, to the logout.php page, everything else is pretty much ignored – Beno May 30 '11 at 09:04
  • When i logout , i am switching to the login page and i am not able to update the div in the login page using document.getElementById. Is there any way by which i can do it? – user475685 May 30 '11 at 09:05

2 Answers2

2

JavaScript runs on the page it appears on.

When you send the browser to another page (e.g. by setting location), you are going to a new page and any scripts running on the current page stop.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It isn't clear what "it" actually is. Presumably you can know whatever condition it is on the server and do the work there. – Quentin May 30 '11 at 09:01
  • When i logout , i am switching to the login page and i am not able to update the div in the login page using document.getElementById. Is there any way by which i can do it? – user475685 May 30 '11 at 09:03
  • not from the script in the contents.php page, in any case. @Quentin is right. I'm not sure we can help without getting more details about your setup – Beno May 30 '11 at 09:06
  • Not from the page you were on. You need to step back and solve the problem instead of trying to get a particular technical implementation to work. You haven't said what that problem is though. – Quentin May 30 '11 at 09:07
0

what if you did this:

on your logout.php page:

header('Location:Login.php?logout=true');
exit;

then on your login page: (using How can I get query string values in JavaScript?)

<script type="text/javascript">
    if (getParameterByName("logout") == "true") {
        document.getElementById("messageid").innerHTML = "Your message";
    }

    function getParameterByName( name )
    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }

</script>
Community
  • 1
  • 1
Beno
  • 4,633
  • 1
  • 27
  • 26