PDA

View Full Version : change src of script tag dynamically



rama1977
11-29-2006, 03:29 PM
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

shachi
11-29-2006, 04:40 PM
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.

rama1977
11-29-2006, 05:55 PM
Hi,

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

Twey
11-29-2006, 06:33 PM
<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.

rama1977
11-29-2006, 09:20 PM
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

Twey
11-29-2006, 10:40 PM
Whoops, my error.
<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>

rama1977
11-30-2006, 08:23 AM
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