Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow 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"

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.

  • #3
    New Coder
    Join Date
    Aug 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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"

  • #4
    New Coder
    Join Date
    Aug 2008
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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"?

    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>

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,300
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    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.


  •  

    Posting Permissions

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