• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Creating Dynamic Content > 14.1. Writing Dynamic Content During Page Loadin...

Writing Dynamic Content During Page Loading

NN 2, IE 3

Problem

You want to customize the content of the body based on client settings or cookie data, particularly in a backward-compatible way.

Solution

All scriptable browsers let you embed scripted document.write( ) statements anywhere in the body where you want customized content to appear. The following code displays a message tailored to the visitor’s operating system:

<html>
<head>
<script type="text/javascript">
function yourOS( ) {
    var ua = navigator.userAgent.toLowerCase( );
    if (ua.indexOf("win") != -1) {
        return "Windows";
    } else if (ua.indexOf("mac") != -1) {
        return "Macintosh";
    } else if (ua.indexOf("linux") != -1) {
        return "Linux";
    } else if (ua.indexOf("x11") != -1) {
        return "Unix";
    } else {
        return "Computers";
    }
}
</script>
...
<body>
<h1>Welcome to GiantCo Computers</h2>
<h2>We love 
<script type="text/javascript">document.write(yourOS( ))</script>
<noscript>Computers</noscript>
Users!</h2>
...
</body>
</html>

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint