Results 1 to 7 of 7

Thread: change src of script tag dynamically

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

    Default change src of script tag dynamically

    Hi,

    I want to change the path value of src attribute of <script> TAG dynamically so that different data gets displyed.

    I have the following code

    <script language="Javascript"
    var today = new Date();
    var time = today.getTime();
    if (time%2 == 0)
    {
    src="http://www.indiaglitz.com/channels/hindi/rss/news_js.asp";
    }
    else if (time%3 == 0)
    {
    src="http://www.indiaglitz.com/channels/hindi/rss/news_js.asp";
    }
    else if (time%5 == 0)
    {
    src="http://www.indiaglitz.com/channels/telugu/rss/news_js.asp";
    }
    else if (time%7 == 0)
    {
    src="http://www.indiaglitz.com/channels/malayalam/rss/news_js.asp";
    }
    else
    {
    src="http://www.indiaglitz.com/channels/kannada/rss/news_js.asp"
    }
    ></script>

    I am substituting src value dynamically.But it does not change after it is loaded for first time.I think once the data got displayed inorder to do display the new content i have to reload the page.But i do not want to refresh the whole page only the portion where this data gets displayed.

    Kindly let me know how i can do it.

    Thanks

  2. #2
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    wow, wow, wow ... what are you doing dude?? It's completely *NOT* HTML *NOR* javascript. You can't do that.

    There's another way of doing this.

  3. #3
    Join Date
    Aug 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    Kindly let me know how to do it.Any help will be appreciated.Thnx for replying.

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

    Default

    Code:
    <script type="text/javascript">
      var scripts = {
        2 : "http://www.indiaglitz.com/channels/hindi/rss/news_js.asp",
        3 : "http://www.indiaglitz.com/channels/hindi/rss/news_js.asp",
        5 : "http://www.indiaglitz.com/channels/telugu/rss/news_js.asp",
        7 : "http://www.indiaglitz.com/channels/malayalam/rss/news_js.asp",
        default : "http://www.indiaglitz.com/channels/kannada/rss/news_js.asp"
      };
    
      function getCurrent() {
        var time = (new Date()).getTime();
        for(var i in scripts)
          if(time % i === 0)
            return scripts[i];
      }
    
      function addScript(url) {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = url;
        document.getElementsByTagName("head")[0].appendChild(s);
      }
    
      addScript(getCurrent());
    </script>
    This will work... however, the difficulty comes in undoing what was done by the previous script. Perhaps it writes into a set area; in this case, you could clear that area.
    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!

  5. #5
    Join Date
    Aug 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    I am getting a javascript error inside the scripts array.it complains that default cannot be accepted.It shouod also be a number or string.How to rectify that.

    Thanks

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

    Default

    Whoops, my error.
    Code:
    <script type="text/javascript">
      var scripts = {
        2 : "http://www.indiaglitz.com/channels/hindi/rss/news_js.asp",
        3 : "http://www.indiaglitz.com/channels/hindi/rss/news_js.asp",
        5 : "http://www.indiaglitz.com/channels/telugu/rss/news_js.asp",
        7 : "http://www.indiaglitz.com/channels/malayalam/rss/news_js.asp",
        "default" : "http://www.indiaglitz.com/channels/kannada/rss/news_js.asp"
      };
    
      function getCurrent() {
        var time = (new Date()).getTime();
        for(var i in scripts)
          if(time % i === 0)
            return scripts[i];
        return scripts["default"];
      }
    
      function addScript(url) {
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.src = url;
        document.getElementsByTagName("head")[0].appendChild(s);
      }
    
      addScript(getCurrent());
    </script>
    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!

  7. #7
    Join Date
    Aug 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    i have attached the zip file desi.zip.It contains a html file.When the html file is opebed in browser, u can see a section in the bottom riht portion.A section called movie link.

    That is the section which has to dynamically change.After a certain period the link will display the specified contents of the url.That is where i have copied the script u have given me.But the link does not change.

    I have copied the script just after heading "MOVIEW LINK".

    KINDLY LET ME KNOW why it is not working.

    Thanks

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
  •