Jump to content

javascript inside a style definition?


jwermont

Recommended Posts

Hi,I tried putting some javascript code inside a style sheet file so that I could code some element positioning differently, depending on which browser is being used. It didn't work.Here is what I put inside the style.css file:<script language="Javascript" type="text/javascript">switch (navigator.appName) { case "Microsoft Internet Explorer": document.write("p.nav { + height: 60px; + left: 0px; + ... + ... + }") break case "Netscape": document.write("p.nav { + ... Netscape definitions ... + }") break}</script> Then I just included that into my html file, but it caused the whole page to look like a huge mess.I put the <link rel ...> tag between the <head> and </head> tags.Any suggestions as to what I did wrong? Or is this simply not allowed? I'm not finding any mention of this in a Javascript book I'm using.Thanks,Joyce

Link to comment
Share on other sites

Take the javascript out of the CSS file, and create a new .JS file, with:

switch (navigator.appName) {case "Microsoft Internet Explorer":document.write("p.nav {+ height: 60px;+ left: 0px;+ ...+ ...+ }")breakcase "Netscape":document.write("p.nav {+ ... Netscape definitions ...+ }")break}

In. Upload that to your host, and use:<script src='http://link/myfile.js'></script>

Link to comment
Share on other sites

On another note, I havent fooled around with it much but, javascript can be used as a stylesheet... apparently in the same way css can. Like I said, I dont know much about it but it might be worth looking into?http://www.web-developer-india.com/web/html/ch13_04.htmlwell... after reading it a little, it appears it's a netscape-only thing...

Link to comment
Share on other sites

Blue wrote: > Take the javascript out of the CSS file, and create a new .JS file, with: > > switch (navigator.appName) { > case "Microsoft Internet Explorer": > document.write(...) > > ...etc... > > > In. Upload that to your host, and use: > > <script src='http://link/myfile.js'></script>OK, I tried this - created a file called style.js, into which I put only the style definitions that are different for each browser. I took those out of my main style.css file, but kept style.css for all the other definitions.Then, in the HTML file, I had two link statements:<link rel="stylesheet" href="style.css" type="text/css" media="all"><script src='http://link/style.js'></script>These were between <head> and </head>.However, the style definitions in the .js file did not get incorporated into the page in either IE or NN. (The other ones, in style.css, did show up correctly on the page). Was the above the correct setup, or did I misunderstand something you said?Thanks.

Link to comment
Share on other sites

What i did is I made two different CSS files, one with the code for Netscape and a second for IE, and put this in the html's <head>:

<!--[if IE]><link rel="stylesheet" href="iestyle.css" type="text/css" /><![endif]--><![if !IE]><link rel="stylesheet" href="nsstyle.css" type="text/css" /><![endif]>

dain

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...