View Full Version : JavaScript question...

08-09-2006, 05:15 PM
I am new to JavaScript and wanted to know if there is a way to assign a variable a certian value and then include that variable within a <script src="..."> link reference.

Here is an example of what I am trying to do:
The following script takes a name of a city and truncates it to 5 characters.

<script type="text/javascript">
var actualcity = 'the name of a city';
var trunccity = (actualcity.substring(0,5));
onload = function(){

Now... I would like to take the variable trunccity and include it within a script src tag. The purpose is to get a local city feed from some weather site:
<script src='http://weatherurl/ [HERE IS WHERE I WOULD INSERT THE TRUNCCITY VARIABLE ] ?/111111'></script>

Any help would be greatly appreciated!

08-09-2006, 10:16 PM
document.write('<script src="http://weatherurl/' + trunccity + '?/111111" type="text/javascript"></script>');You can also use DOM methods to add the script to the head:
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "http://weatherurl/" + trunccity + "?/111111";
document.getElementsByTagName("head").appendChild(s);However, although this considered a "cleaner" method, it can cause problems for some scripts.