admin管理员组

文章数量:1392007

I am trying to call another function in a different HTML file would I do something like this:

 <script type = "text/javascript" src = "otherfile.html"></script>

This is the function getting called

        function rankedScores(music, pattern) { 
            var scoresArray = urlScores(music, pattern);  

            function swap(a, b) {
                var temp = scoresArray[a];
                scoresArray[a] = scoresArray[b];
                scoresArray[b] = temp;
            }

            for(var i = 0; i < scoresArray.length; i++) {
                for(var x = 0; x < scoresArray.length - 1; x++) {

                    if (scoresArray[x].score > scoresArray[x + 1].score) {
                        swap(x, x + 1);
                    }
                }
            }
            generateResults(scoresArray)
        }

This is how im calling it:

       rankedScores(albums, document.main.search.value);

I am trying to call another function in a different HTML file would I do something like this:

 <script type = "text/javascript" src = "otherfile.html"></script>

This is the function getting called

        function rankedScores(music, pattern) { 
            var scoresArray = urlScores(music, pattern);  

            function swap(a, b) {
                var temp = scoresArray[a];
                scoresArray[a] = scoresArray[b];
                scoresArray[b] = temp;
            }

            for(var i = 0; i < scoresArray.length; i++) {
                for(var x = 0; x < scoresArray.length - 1; x++) {

                    if (scoresArray[x].score > scoresArray[x + 1].score) {
                        swap(x, x + 1);
                    }
                }
            }
            generateResults(scoresArray)
        }

This is how im calling it:

       rankedScores(albums, document.main.search.value);
Share Improve this question edited Dec 8, 2013 at 20:27 Pavlo 45k14 gold badges83 silver badges114 bronze badges asked Dec 8, 2013 at 20:24 user2964960user2964960 291 silver badge9 bronze badges 2
  • 3 Move it to a JS file. – SLaks Commented Dec 8, 2013 at 20:26
  • If one of the pages is in an iframe, you can call its functions from the parent window. – Anderson Green Commented Feb 27, 2022 at 12:48
Add a ment  | 

1 Answer 1

Reset to default 4

You cannot do this. You should move it into a js file and then include that js file in your template:

<script type='text/javascript' src='some_other_file.js'></script>

本文标签: javascriptcall function in another HTML fileStack Overflow