0

Q.js file

Q = {};

Q.stringFile = [];
Q.file = "CSS.txt";

Q.getData = function(Q.file){
    $.get(Q.file, function(data){
        var str = data;
        Q.stringFile = str.split("\n"); 

        return Q.stringFile;
    });
}

a.html file

<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>   
    <script type="text/javascript" src="Q.js"></script>
    <script type="text/javascript">
        var d = Q.getData(Q.file);
        alert(d);
     </script>

</head>

<body>

</body>
</html>

alert doesn't output!

Errors: Q is not defined ; unexpected token .

How do i fix this??

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
DrStrangeLove
  • 11,227
  • 16
  • 59
  • 72

5 Answers5

2

As I said in my comment, you cannot return data from an Ajax call, as the Ajax call is asynchronous. You have to make your function accept a callback, like:

Q.getData = function(file, callback){
    $.get(file, function(data){
        var stringFile = data.split("\n"); 
        callback(stringFile);
    });
};

and then call it with:

Q.getData(Q.file, function(d) {
    alert(d);
});

Regarding the errors: You have a syntax error in this line

Q.getData = function(Q.file)

Q.file is not valid here. The browser cannot parse and process the file and so Q will not be defined.


I have the impression, you should first read some tutorial before you go on.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

Your problem is at:

Q.getData = function(Q.file) {

The part after function( is a formal parameter list and can only contain valid identifiers. They can't contain '.' characters.

RobG
  • 142,382
  • 31
  • 172
  • 209
1

I think the problem is with the method definition:

Q.getData = function() {
    var that = this;
    $.get(that.file, function(data){
        var str = data;
        that.stringFile = str.split("\n"); 

        // below return has no purpose in an async request
        // return Q.stringFile;

        alert(that.stringFile);
    });
}

After running Q.getData(); your Q.stringFile will contain your data;

Also because this function does not return any value, you have to put your alert in the callback.

aorcsik
  • 15,271
  • 5
  • 39
  • 49
1

Your:

Q.getData = function(Q.file) {

Is not valid, that's where you define named arguments not where you pass them.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

You can't use an object property as function parameter: Q.getData = function(Q.file){. In this case there is no need for a parameter at all because of closure you can read Q.file from inside the getData function.

The reason that your alert is returning undefined is that you are making a asynchronous AJAX call it doesn't return anything so d is undefined. If you want to return something from an AJAX call you need to make a synchronous call.

A better solution however would be to use a success handler:

var Q = {}; // Without the var it is an implied global.  In this case you look like you want a global, but it's still good form to explicitly define it in the global namespace.

Q.stringFile = [];
Q.file = "CSS.txt";

Q.getData = function() {
    $.get(Q.file, function(data) {
        var str = data;
        Q.stringFile = str.split("\n"); 

        return Q.stringFile;
    }).success(function (d) { // This fires once the data has arrived
        alert(d);
    });
}; // you should have a ; here
Useless Code
  • 12,123
  • 5
  • 35
  • 40