0

I keep reading that it is possible to load javascript files, and then use those functions in other files. Unfortunately I can't get this to work. Here is my current path:

        <script src="js/bootstrap.min.js"></script>
        <script src="js/twitter_crypt.js"></script>
        <script src="js/scripts.js"></script>
        <script src="http://platform.twitter.com/widgets.js"></script>
        <script src="js/interactive.js"></script>

I am using functions from the twitter_crypt.js file in my interactive.js file (last in path).

When I console.log(nameOfFunction) in interactive.js which is in twitter_crypt.jsit tells me that function is not defined.

I ran into this posts but I am not looking to use something like require.js .

I am just trying to call functions from the other files. I assume the must be available since I am using jquery, bootstra.min.js and other files that are being loaded.

I believe I have everything in order as well which is what this post talks about.

//Twitter_Crypt JS file:

function decoder(key, encodedMessage){
    var coded = "";
    var ch;
    var index;
    var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for(var i = 0; i < encodedMessage.length; i++){
        ch = encodedMessage.charAt(i);
        index = key.indexOf(ch);
        if(index === -1){
            coded = coded + ch;
        } else {
            coded = coded + alphabet.charAt(index);
        }
    }
    return coded;

}

InteractivJS File:

$('#decodeSubmit').on("click", function(ev){
        if($("#encryptText").val() === ""){
            alert("Please Enter a Decrypted Tweet");
        } else {
        ev.preventDefault();
        var decodedTweet = decoder("zipsunxmogdkfabrhcylvqewtj", $("#encryptText").val());
        $("#decode_form").fadeOut("fast");
            $("#decode").fadeIn("fast");
            $('#decode_progress').progressbar({
                display_text: 'center', 
                use_percentage: false, 
                refresh_speed: 10,
                done: function($this) { 
                    $(".modal-body").append(decodedTweet).css('color', 'black');
                    $("#decodeNewTweet").fadeIn("slow");
                    $(".modal").modal("show");
                }
            });
        }

    });

When I put the function decoder inside the interactiveJS file it works fine. When it is in the twitter_crypt.js file I keep getting a decoder is not defined.

Originally I had both scripts wrapped in a document ready, which helped but didn't solve everything. Why isn't my decoder function in global scope though? It isn't wrapped in any other functions?

Do I have to do something like window.decoder =function(){}

Community
  • 1
  • 1
HelloWorld
  • 10,529
  • 10
  • 31
  • 50
  • 4
    Then either the function isn't defined, or it isn't in the global scope. We can't see the code in your scripts to tell. – Quentin Sep 25 '14 at 19:04
  • Yeah, whatever functions you're using need to be exported into the global scope from that script. – Tom A Sep 25 '14 at 19:10
  • I did find one problem -- `I assume`. Don't assume. Find out. Copy the script from the file into your page as if it were a standard script instead of a linked script. See if it alters the result. – Joel Etherton Sep 25 '14 at 19:10
  • Hey guys, thanks for the suggestions, I added some of the code in both of my files, these scripts don't work together, I believe they are both in global scope as well and loading in the proper order. – HelloWorld Sep 25 '14 at 19:17
  • Perhaps try running stuff in the Firebug console, e.g. `decoder("zipsunxmogdkfabrhcylvqewtj", "hello"` – Owlvark Sep 25 '14 at 19:38
  • What happens when you try window.decoder = decoder in the file where decoder is implemented? Try window.decoder in the console after having done that. – Jeremy D Sep 25 '14 at 20:25

0 Answers0