SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    arr= []; will be arr=[112,184,234,543];

    js1.js
    Code:
     
    var ABC_x = "112";
    var ABC_y = "184";
    js2.js
    Code:
      
    var ABC_a = "234";
    var ABC_b = "543";
    arr= [];
    will be
    arr=[112,184,234,543];

    I tried this:
    Code:
      
    <script type="text/javascript" src="js1.js" ></script>
    
    <script type="text/javascript" src="js2.js" ></script>
    
    <script type="text/javascript">
    
    window.onload=function () {
    
    var arr = [];
    
    var el = document.getElementsByTagName('script');
    
    for(var i=0; i< el.length; i++)
    { 
    if(el[i].innerHTML.indexOf('ABC_') != -1)
    {
    arr.push(el[i].innerHTML.indexOf('ABC_')[i]) ;
    }
    }
    alert(arr);
    
    }
    </script>
    It did not that. How can I do? Thanks...

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript" src="js1.js" >hello</script>
    <script type="text/javascript" src="js2.js" >world</script>
    <script type="text/javascript">
    window.onload=function () {
    	
    	var el = document.getElementsByTagName('script');
    	
    	for(var i=0; i< el.length; i++)
    	{ 
    		alert(el[i].innerHTML)
    	
    	}
    	
    	arr = [ABC_x, ABC_y, ABC_a, ABC_b];
    	alert(arr)
    	
    }

  3. #3
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the DOM and/or innerHTML for this is silly. There's at least one better way.

    So you can't modify those two external scripts?

    Is the order of the numbers in the array important?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He. There is no need to do innerHTML on that, the variables in the external JavaScript files are accessible once then load.

    Code:
    window.onload = function() {
        var arr = [ABC_x , ABC_y, ABC_a, ABC_b];
    }
    Which 7stud points out I just noticed...

  5. #5
    do righteous deeds
    Join Date
    Aug 2006
    Location
    Türkiye Turkey تركيى
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks...
    Now I want to get the texts in the external JavaScript files. Message box will display the texts in the external JavaScript files. But it is not possible.
    Code:
    window.onload = function() {
        alert( display the texts in the external JavaScript files(js1.js)) ; // var ABC_x = "112"; var ABC_y = "184";
    }
    Last edited by muazzez; Mar 18, 2007 at 12:37.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean that you actually want to alert the content of the external JavaScript file? I don't know why you would like to do this, but I guess you could do it using Ajax and retrieving a text file with that JS code.

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    // If you define them like this it won't work, I dont know why, I thought it would
    //var ABC_x = "112";
    //var ABC_y = "184";
    //var ABC_a = "234";
    //var ABC_b = "543";
    
    // if you defined them this way it will work
    window["ABC_x"] = "112";
    window["ABC_y"] = "184";
    window["ABC_a"] = "234";
    window["ABC_b"] = "543";
    
    var arr = [];
    
    window.onload = function () {
    	for (var thingy in window) {
    		if (thingy.indexOf("ABC") == 0) { // if it starts with ABC
    			arr.push(window[thingy]);
    		}
    	}
    	alert(arr.join("\n"));
    }
    </script>
    </head>
    <body>
    </body>
    </html>

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jimfraser,

    What does your code do that the following doesn't do:

    arr = [ABC_x, ABC_y, ABC_a, ABC_b];

    Although the poster now says that is not what he wants to do. He wants the actual text:

    "var ABC_x = 112"

  9. #9
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser
    If you define them like this it won't work, I dont know why, I thought it would
    Hmm... that appears to be a bug in IE. It works fine in Firefox and Opera.

    The alerts display the values, but it appears that with the former declaration, the variable fails to show up in a for-in loop but the latter does.

    Code:
    var ABC_x = "112";
    window["ABC_y"] = "184";
    alert('ABC_x = '+window["ABC_x"]);
    alert('ABC_y = '+window["ABC_y"]);
    for (var thingy in window) {
      if(typeof(window[thingy])!='string') continue;
      if(!confirm(thingy)) break;
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  10. #10
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I read the original post I figured that the point was to take an unknown quantity of variables (in this case ABC_x et al) that are loaded separately (in a.js, b.js) and somehow make an array of their values, allowing for expansion later on (eg ABC_z = "555").

    I thought that using window["variable_name"] would work, but for some reason those variables didn't show up in my for...in loop, unless they were defined as I showed in my example.

    This code:
    arr = [ABC_x, ABC_y, ABC_a, ABC_b];
    sure it works fine, but not if ABC_z and ABC_c are added later.


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
  •