/    Sign up×
Community /Pin to ProfileBookmark

Problem of using two external js file…

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

[url]http://abcd.js[/url] and [url]http://efgh.js[/url] 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”

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

[CODE]<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>[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@avitardotnetAug 23.2008 — You can just load each external file conditionally. A good alternative is to use ajax with json if you need to flip them back and forth after the page is loaded.

[CODE]<script type="text/javascript">
<![CDATA[

var i = 1;

if(counter.length > 1) {
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>');
}
while (i < counter.length) {
var msg= counter[i++];
document.write(msg);
}
]]>
</script>[/CODE]
Copy linkTweet thisAlerts:
@lovebearhkauthorAug 25.2008 — Thanks very much! But I found that it 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"?

[CODE]<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>[/CODE]
Copy linkTweet thisAlerts:
@konithomimoAug 25.2008 — parse the querystring for the value you need:

&lt;SCRIPT type="text/javascript" language="javascript"&gt;
function getCounter(){
query = window.location.search.substring(1);
guery_split = query.split("&amp;");
for (i=0;i&lt;query_split.length;i++) {
value_split = query_split[i].split("=");
if (value_split[0] == counter)
return value_split[1];
}
return(-1);
}

var counter = getCounter();
if(counter==-1)
counter = 5;

var i = 6;
if(counter &gt; i)
document.write('&lt;scr'+'ipt type="text/javascr'+'ipt" src="http://abcd.js"&gt;&lt;/scr'+'ipt&gt;');
else
document.write('&lt;scr'+'ipt type="text/javascr'+'ipt" src="http://efgh.js"&gt;&lt;/scr'+'ipt&gt;');
&lt;/SCRIPT&gt;
×

Success!

Help @lovebearhk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...