Jump to content

Dynamic change of page title from AJAX called page


gamesmstr

Recommended Posts

I have a game website.  I want to be able to change the page title to alert players when a new news item affects them.  I routinely use refreshing AJAX calls to refresh stats etc.  These AJAX calls refer to a seperate php file.  I have tried everything I can think of, but the solution eludes me.

 

 

Here is the AJAX call (NO I am not interested in Jquery)

function createXMLHttpRequest() {
  if (typeof XMLHttpRequest != 'undefined') { 
    return new XMLHttpRequest(); 
  } 
  try { 
    return new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) {
    try { 
      return new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (e) {}
  }
  return false; 
}
function refresh_stats() {
  var xmlHttp3 = createXMLHttpRequest();
  params = '';
  xmlHttp3.open("POST","ajax/statsajax.php", true);
  xmlHttp3.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp3.onreadystatechange = function() {
    if(xmlHttp3.readyState == 4 && xmlHttp3.status == 200) {
      var brokenstring = xmlHttp3.responseText.split("-@[-");
      if ( brokenstring[0] == 'stats' ) { 
        document.getElementById("statbox").innerHTML = brokenstring[1];
      }
    }
  }
  xmlHttp3.send(params);
  statupdate=setTimeout("refresh_stats()", 60000);
}

Here is a snippet of the PHP code that I am working with

if ($timecheck1['id'] > $player['news']) {
  echo("<i><font color=red> * News! *</font></i>");
  ?> <script type="text/javascript">document.title = "*News!* Immortalix";</script> <?php
} else {?> <script type="text/javascript">document.title = "Immortalix";</script> <?php }

I am assuming that it is not working because it is being called from a different document.  Any suggestions would be most helpful.

Link to comment
Share on other sites

Please See above.  As I stated before, I am NOT interested in jQuery.  The AJAX works fine and has for years.  I'm not going to go recode my site after 6 years just to say I have jQuery.  What I am adding is just a little perk and is more related to how to change the page title from inside an ajax called php file than the javascript itself.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.