sametch
05-11-2003, 10:22 AM
I am trying to design a website that changes the page style attributes depending upon the width of the browser page suing JavaScript.
The JavaScript creates in inline style using document.write.
Whenever I put the script into the head area of HTML (which Is where I would prefer it). The script fails to run.
I only seam to be able to get it to run in the body section.
Is it OK to define styles in the page body?
The code is as follows:
<html>
<head>
<title>page title</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function LeftStart(WebWidth)
{
var PositionStart=0;
var PageSize=0;
if (document.body.clientWidth != null)
{
PageSize=document.body.clientWidth;
}
else
{
if (window.innerWidth != null)
{
PageSize=document.innerWidth;
}
}
if (PageSize < WebWidth)
{
return 0;
}
else
{
return Math.round((PageSize - WebWidth)/2);
}
}
//-->
</script>
<noscript>
<style type="text/css">
<!--
.container{position: absolute; top: 15px; left: 0px}
-->
</style>
</noscript>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
document.writeln("<style type=\"text/css\">");
document.writeln(".container{position: absolute; top: 15px; left: "+LeftStart(720)+"px}");
document.writeln("</style>");
//alert (LeftStart(720));
//-->
</script>
....etc
The JavaScript creates in inline style using document.write.
Whenever I put the script into the head area of HTML (which Is where I would prefer it). The script fails to run.
I only seam to be able to get it to run in the body section.
Is it OK to define styles in the page body?
The code is as follows:
<html>
<head>
<title>page title</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function LeftStart(WebWidth)
{
var PositionStart=0;
var PageSize=0;
if (document.body.clientWidth != null)
{
PageSize=document.body.clientWidth;
}
else
{
if (window.innerWidth != null)
{
PageSize=document.innerWidth;
}
}
if (PageSize < WebWidth)
{
return 0;
}
else
{
return Math.round((PageSize - WebWidth)/2);
}
}
//-->
</script>
<noscript>
<style type="text/css">
<!--
.container{position: absolute; top: 15px; left: 0px}
-->
</style>
</noscript>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
document.writeln("<style type=\"text/css\">");
document.writeln(".container{position: absolute; top: 15px; left: "+LeftStart(720)+"px}");
document.writeln("</style>");
//alert (LeftStart(720));
//-->
</script>
....etc