...

View Full Version : Problem of using two external js file...



lovebearhk
08-20-2008, 07:55 PM
I would like to write a script to read "http://abcd.js", then get the length of the variable "counter" inside the file, if it greater then i, output all the messages in "http://abcd.js", else, output all the messages in "http://efgh.js"

Can anyone suggest how can I link up these script? Thanks!


<SCRIPT type="text/javascript" language="javascript" SRC="http://abcd.js"></SCRIPT>
<SCRIPT type="text/javascript" language="javascript">
var i = 1;
if (counter.length > 1){
while (i < counter.length) {
var msg= counter[i++];
document.write(msg);
}
}
</SCRIPT>

else

<SCRIPT type="text/javascript" language="javascript" SRC="http://efgh.js"></SCRIPT>
<SCRIPT type="text/javascript" language="javascript">
var i = 1;
while (i < counter.length) {
var msg= counter[i++];
document.write(msg);
}
</SCRIPT>

Philip M
08-20-2008, 09:19 PM
Not sure that I fully understand.

I take it that abcd.js contains an array of messages, but of course the number of messages or the variable "counter" is a constant as read in from the file.

You can make the two arrays of messages message1() and message2().
Then output either one depending on the value of counter.

But if counter is to change from session to session, then you need a cookie to capture its value.

lovebearhk
08-20-2008, 09:25 PM
yes, http://abcd.js and http://efgh.js contain an array of message. "counter" is the name of the array in each file. I have successfully get the values in this two file. But dont know how to link up the "IF ELSE"

lovebearhk
08-25-2008, 09:42 PM
Thanks! I get another solution.:)
Another problem for this issue...

I found that the following script works only if I have defined the variable "counter". What should I do if "counter" is the number of messages as read in from "http://abcd.js"?


<SCRIPT type="text/javascript" language="javascript">
var counter = 5;
var i = 6;
if(counter > i) {
document.write('<scr'+'ipt type="text/javascr'+'ipt" src="http://abcd.js"></scr'+'ipt>');
}else{
document.write('<scr'+'ipt type="text/javascr'+'ipt" src="http://efgh.js"></scr'+'ipt>');
}
</SCRIPT>
<script type="text/javascript">
var i = 6;
while (i < counter.length) {
var msg= counter[i++];
document.write(msg);
}
</script>

Philip M
08-25-2008, 10:00 PM
if the variable counter exists and contains the number of mesages, simply delete the var in:-

var counter = 5;

That var makes counter a (new) local variable only available to that function.

It is perfectly possible (but obviously undesirable) to have two variables with exactly the same name, one global and one local.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum