Results 1 to 2 of 2

Thread: JavaScript question...

  1. #1
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaScript question...

    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(){
    }
    </script>

    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!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    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:
    Code:
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •