Jump to content

Javascript in head


pcurran
 Share

Recommended Posts

I would like some advice on whether the following is valid (legal in, say HTML 4.01 Transitional), and reasonably browser-independent. It appears to be valid according to the DTD (e.g. Tidy doesn't object), and it works on the browsers I have tested (several version of Firefox and IE), but it still feels dodgy, and I would like some opinions before I rely on it.I want to write Javascript code in the HEAD section of an HTML file to generate links to script files and CSS files. For example, something like the following:

<script type="text/javascript">   document.write("<script type='text/javascript' src='myjs.js'><\/script>");   document.write("<link rel='stylesheet' type='text/css' href='mystyle.css'>");</script>

(Obviously in the real code, the logic is more complicated, with the file names derived from external data, such as the current date.)Anyone see any problem doing this?Thanks for your comments.

Link to comment
Share on other sites

<script type="text/javascript">   document.write("<script type='text/javascript' src='myjs.js'><\/script>");   document.write("<link rel='stylesheet' type='text/css' href='mystyle.css'>");</script>

What's this? You don't have to use a JavaScript code to import another JS and your CSS.You do this between <head> and </head>:<script type="text/javascript" src="myjs.js"></script>and<link rel="stylesheet" type="text/css" href="mystyle.css">

Link to comment
Share on other sites

What's this? You don't have to use a JavaScript code to import another JS and your CSS.
But you do if you don't know the filename that you want until runtime. Normally you'd use server-side code to do this:
<script type="text/javascript" src="<%=ScriptFileName%>"></script>

But if you aren't using server-side technologies, you have to rely on javascript. J Winey, I'm pretty sure it works, but, like you, I haven't tested it extensively.

Link to comment
Share on other sites

if the user has js turned off none of the linking happens.I would define a default (perhaps in <noscript></noscript> in case this happens
Thanks. In this case the default CSS will work adequately. I'm putting in some work-arounds for unused Javascript, but there is only so much that can be done.
But you do if you don't know the filename that you want until runtime. Normally you'd use server-side code to do this:
<script type="text/javascript" src="<%=ScriptFileName%>"></script>

But if you aren't using server-side technologies, you have to rely on javascript. J Winey, I'm pretty sure it works, but, like you, I haven't tested it extensively.

Yes, that is the situation here.Thanks - I'm going ahead. Everything seems to work fine in the browsers I've tested, which covers the ones most important to me.
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
 Share

×
×
  • Create New...