最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - call external .js file in html - Stack Overflow

programmeradmin3浏览0评论

I have an array.js file and an index.html.

My array.js file looks like this:

function go(){
    var array = new Array();
    array[0] = "Red";
    array[1] = "Blue";
    array[3] = "Green";
    for (var i=0; i < array.length; i++){
        document.write("<li>" + array[i] + "<br />");
    }
}

My index.html file looks like this:

<html>
    <head>
        <script type="text/javascript" src="array.js"></script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>
        <script>
            go();
        </script>
    </body>
</html>

When I click the Display JS Array button on the HTML page, nothing happens. I want the array elements to be displayed after I click the button. It should look like:

  • Red
  • Blue
  • Green

I have an array.js file and an index.html.

My array.js file looks like this:

function go(){
    var array = new Array();
    array[0] = "Red";
    array[1] = "Blue";
    array[3] = "Green";
    for (var i=0; i < array.length; i++){
        document.write("<li>" + array[i] + "<br />");
    }
}

My index.html file looks like this:

<html>
    <head>
        <script type="text/javascript" src="array.js"></script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>
        <script>
            go();
        </script>
    </body>
</html>

When I click the Display JS Array button on the HTML page, nothing happens. I want the array elements to be displayed after I click the button. It should look like:

  • Red
  • Blue
  • Green
Share Improve this question edited Aug 7, 2012 at 13:34 Teun Zengerink 4,3935 gold badges32 silver badges32 bronze badges asked Aug 7, 2012 at 6:05 zmnyzmny 1192 gold badges2 silver badges8 bronze badges 1
  • Which browser are you working on where nothing happens ? Most probably your script array.js is not at root location where you are trying to access it. – yogi Commented Aug 7, 2012 at 6:11
Add a comment  | 

5 Answers 5

Reset to default 4

Your code is quite ok. And working fine. Yes you can use external JS or internal JS .

By using External JS: -

Test.html

<html>
    <head>
        <script type="text/javascript" src="arrayy.js"></script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>       
    </body>
</html>

arrayy.js

function go(){
    var array = new Array();
    array[0] = "Red";
    array[1] = "Blue";
    array[3] = "Green";
    for (var i=0; i < array.length; i++){
        document.write("<li>" + array[i] + "<br />");
    }
}

These above codes are running in Mozilla Firefox ,IE 8 and some other browser.

By Using Internal JS: -

 <html>
    <head>
        <script>
            function go(){
            var array = new Array();
            array[0] = "Red";
            array[1] = "Blue";
            array[3] = "Green";
            for (var i=0; i < array.length; i++){
                document.write("<li>" + array[i] + "<br />");
            }
            }        
        </script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>       
    </body>
</html>

You can change the function by taking the parent element of li elements as parameter.

function go(element){
    var array = new Array();
    array[0] = "Red";
    array[1] = "Blue";
    array[3] = "Green";

    var ul = document.createElement("ul");
    for (var i=0; i < array.length; i++) {
       var li = document.createElement("li");
       li.innerHtml = array[i];
       ul.appendChild(li);
    }
    body.insertAfter(ul, element); 
}


<input type="button" onclick="go(this)" value="Display JS Array"/>

replace this document.write("<li>" + array[i] + "<br />"); with document.write("<li>" + array[i] + "</li>");

and in your HTML file, remove

<script>
  go();
</script>

because already you calling go(); function on onclick. and in your array, array[2] will give undefined.

try this its working for me :

    <html>
    <head>
        <script type="text/javascript">
            function go(){
                var array = new Array();
                array[0] = "Red";
                array[1] = "Blue";
                array[2] = "Green";
                li = document.getElementById("list");
                li_arr = "";
                for (var i=0; i < array.length; i++){
                   li_arr += "<li>" + array[i] + "</li>";
                }
               li.innerHTML = li_arr;
            }
        </script>
    </head>
    <body>
        <input type="button" onclick="go()" value="Display JS Array"/>
        <ul id="list"></ul>
    </body>
</html>

It works just fine for me.

But as shreedhar said: You need to delete

 <script>
 go();
 </script>

this otherwise it will be displayed before you pressed the button. An since array[2] is undefined you need to filter that one from your array.

Suppose if you are using jquery dom ready function then make the modification on arrayy.js like this.

$(document).ready(function() {
    this.go = function(){
        var array = new Array();
        array[0] = "Red";
        array[1] = "Blue";
        array[3] = "Green";
        for (var i=0; i < array.length; i++){
           document.write("<li>" + array[i] + "<br />");
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论