Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onload issue?

Status
Not open for further replies.

BrotherArnold

Technical User
Jul 12, 2002
16
GB
Hi,

The following test code is meant to select a css depending on screen width but it doesn't work - it writes the LINK tag to a new document. However, if I delete the function line and remove the onload bit, it works. Why is that? If I'm going to be adding more functions can I get by with this code set within the script tag but outside a function.If not what do I need to do to get this to work inside a function. As I'm sure you can gather I'm new to Javascript so any help would be much appreciated.

<HTML><HEAD><TITLE>test</TITLE>
<SCRIPT LANGUAGE=JavaScript>
function init()
{
if (screen.Width < 801)
document.write('<link rel="stylesheet" type="text/css" href="lowres.css" / >\n')
else
document.write('<link rel="stylesheet" type="text/css" href="hires.css" / >\n')
}
</SCRIPT>
</head>
<body onload="init();"><p>hi there, how's it going?</body>
</html>

Cheers
Brother Arnold
 
because document.write() replaces the document's contents if used after the page loads.



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Try calling the function "inline" as it loads - since you want to display the formatting AS the page loads, not AFTER it has loaded:
Code:
<script type="text/javascript">
function init() {
    if (screen.Width < 801)
        document.write('<link rel="stylesheet" type="text/css" href="lowres.css" / >\n')
    else 
        document.write('<link rel="stylesheet" type="text/css" href="hires.css" / >\n')
}
init();
</script>
</head>
<body>
<p>hi there, how's it going?</p>
</body>
</html>

I've noticed that you have used XHTML formatting in your JavaScript, but you forgot to close off your paragraph tag in the body. [thumbsup2]

Hope this helps.

Pete.


Web Developer & Aptrix / IBM Lotus Workplace Web Content Management (ILWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top