This is a PHP script. The same script in classic asp can be found here - http://www.sitepoint.com/forums/show...68#post5163068
What do you people think of this method?


It's just an example written from a question here http://www.sitepoint.com/forums/show...nt-Replacement No animation though, which was one of the requests, but works with or without javascript.


The live method can work with ASP and PHP. I'm trying to do it in .NET but having problems.


It's better if the CSS and JavaScript were in their seperate files, but I just wanted to show how it was done in one page.


Copy and save it in a default folder that runs php and it should work the same with or without javascript.


If you have dynamic data then you will need to add a nocache header. Adding this will also make it live if you use a setTimeout or setInterval.


PHP Code:
<?php
//----is Live----//
$live=False;
if((isset(
$_GET["live"]))&&(($_GET["live"]==="1")||($_GET["live"]==="2"))){
    
$live=(int)$_GET["live"];
}
//----write function----//
function write($s){
    global 
$live;
    if(
$live){
        echo 
str_replace("\"","\\\"",str_replace("\r\n","",$s));
    }else{
        echo 
$s;
    }
}
//----product id (pid)----//

$pid=0;
if(isset(
$_GET["pid"])){
    
$pid=(int)$_GET["pid"];
}
//----page headers----//
if($live){
    
header("ContentType: application/x-javascript");
} else {
    echo 
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\\r\n\t\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n";
    echo 
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n";
    echo 
"<head>\r\n";
    echo 
"<title>".$title."</title>\r\n";
    echo 
"<style type=\"text/css\">\r\n";
        echo 
"img{width:200px;height:150px}\r\n";
        echo 
"#sidecontent{display:block;width:200px;height:400px;background:gold;float:left;}\r\n";
        echo 
"#maincontent{display:block;width:700px;height:400px;background:gold;float:left;}\r\n";
        echo 
"#others img{width:100px;height:75px;}\r\n";
    echo 
"</style>\r\n";
    echo 
"<script type=\"text/javascript\">\r\n";
    echo 
"function changeScript(s){";
        echo 
"var o=document.getElementsByTagName(\"script\")[0];";
        echo 
"var n=document.createElement('script');";
        echo 
"n.setAttribute('type', 'text/javascript');";
        echo 
"n.setAttribute('src',s);";
        echo 
"o.parentNode.replaceChild(n,o);";
    echo 
"};";
    echo 
"function init(){";
        echo 
"a=document.getElementsByTagName(\"a\");";
        echo 
"for(i=0;i<a.length;i++){";
            echo 
"a[i].onclick=function(){if(this.href.indexOf(\"?\")>-1) changeScript(this.href+\"&live=1\"); else changeScript(this.href+\"?live=1\"); return false;}";
        echo 
"}\r\n";
    echo 
"}\r\n";
    echo 
"window.onload=function(){init();}\r\n";
    echo 
"</script>\r\n";
    echo 
"</head>\r\n";
    echo 
"<body>\r\n";
    echo 
"<div id=\"sidecontent\"><a href=\"\" title=\"Show All\">Static side Content</a></div>\r\n";
}
if(
$live){
    echo 
"document.getElementById(\"maincontent\").innerHTML=\"";
} else {
    echo 
"<div id=\"maincontent\">\r\n";
}
if(
$pid===0){
    for(
$i=5;$i<11;$i++){
        
write("<a href=\"?pid=".$i."\" title=\"Product #\"".$i."\"><img src=\"\" alt=\"Egypt".$i.".jpg\" /></a>\r\n");
    }
    
write("</div>\r\n");
} else {
    
//----get product info from db or text file----//
    
write("<img src=\"\" alt=\"Egypt".$pid.".jpg\" />\r\n");
    
write("<div id=\"info\">\r\n");
    
write("<h1>Product Number ".$pid."</h1>\r\n");
    
write("<p>This is the info for product number ".$pid."</p>\r\n");
    
write("</div>\r\n");
    
write("<div id=\"others\">\r\n");
    for(
$i=5;$i<11;$i++){
        if(
$i!==pid){
            
write("<a href=\"?pid=".$i."\" title=\"Product #".$i."\"><img src=\"\" alt=\"Egypt".$i.".jpg\" /></a>\r\n");
        }
    }
    
write("</div>\r\n");
}
if(
$live){
    echo 
"\";init();";
} else {
    echo 
"</div>\r\n";
}
?>