ReformSchooler
01-22-2004, 09:41 AM
I am trying to write a JavaScript that needs only to work with IE. It will change the CSS styles used on my pages when my site is viewed on a browser window less than a given size. I need it because IE doesn't support the CSS-2 value "min-width."
Here is the relevant html and code . . . you will immediately see my problem:
<head>
<link rel="stylesheet" type="text/css" href="layout.css" />
<!--[if IE]>
<script type="text/JavaScript">
var minlimit = 700 // The value (in pixels) below which the new stylesheet is needed.
window.onload = minWidth;
window.onresize = minWidth;
function minWidth() {
if(document.body.offsetWidth <= minlimit)
{
document.write('<link rel="stylesheet" type="text/css" title="ie hack" href="ie-no-min-width-hack.css" />');
}
else return false;
}
//
</script>
<![endif]-->
</head>
Note my use of IE's conditional statements to hide my JavaScript from all but IE browsers ().
The problem, is, of course that when the MinWidth function is called, document.write returns only the string between the single quotation marks. What I want it to do is write the string to the page.
This must be a very easy problem to solve, but while I have a pretty good grasp of Perl, the only JavaScript I know is what I've learned in the last day or so.
Any suggestions on how to modify my JavaScript to ADD
<link rel="stylesheet" type="text/css" title="ie hack" href="ie-no-min-width-hack.css" /> to my page rather than have it the only thing returned when my function is called?
Thanks for your help!
-- Chris
Here is the relevant html and code . . . you will immediately see my problem:
<head>
<link rel="stylesheet" type="text/css" href="layout.css" />
<!--[if IE]>
<script type="text/JavaScript">
var minlimit = 700 // The value (in pixels) below which the new stylesheet is needed.
window.onload = minWidth;
window.onresize = minWidth;
function minWidth() {
if(document.body.offsetWidth <= minlimit)
{
document.write('<link rel="stylesheet" type="text/css" title="ie hack" href="ie-no-min-width-hack.css" />');
}
else return false;
}
//
</script>
<![endif]-->
</head>
Note my use of IE's conditional statements to hide my JavaScript from all but IE browsers ().
The problem, is, of course that when the MinWidth function is called, document.write returns only the string between the single quotation marks. What I want it to do is write the string to the page.
This must be a very easy problem to solve, but while I have a pretty good grasp of Perl, the only JavaScript I know is what I've learned in the last day or so.
Any suggestions on how to modify my JavaScript to ADD
<link rel="stylesheet" type="text/css" title="ie hack" href="ie-no-min-width-hack.css" /> to my page rather than have it the only thing returned when my function is called?
Thanks for your help!
-- Chris