Jump to content


Photo

Dynamic change of page title from AJAX called page


  • Please log in to reply
2 replies to this topic

#1 gamesmstr

gamesmstr

    Advanced Member

  • Members
  • PipPipPip
  • 140 posts

Posted 20 February 2014 - 11:59 PM

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.



#2 gristoi

gristoi

    Advanced Member

  • Members
  • PipPipPip
  • 841 posts

Posted 21 February 2014 - 05:22 AM

Firstly it is 2014 you should have no need to build raw ( and outdated ) ajax calls like this. Consider switching to a modern JQuery method for your ajax calls


To err is human... to really foul up requires the root password

#3 gamesmstr

gamesmstr

    Advanced Member

  • Members
  • PipPipPip
  • 140 posts

Posted 21 February 2014 - 06:43 AM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com