0

I have the following code:

<script type="text/javascript">
    var ops = {
        slideshow: {
            slices: 20,
            boxCols: 11,
            boxRows: 6,
            animSpeed: 750,
            pauseTime: 6000
        },
        tweets: {
            username: 'stigmahost',
            count: 6,
            refresh_interval: 120,
            auto_join_text_default: 'I said,',
            auto_join_text_ed: "I",
            auto_join_text_ing: "I am",
            auto_join_text_reply: "I replied to",
            auto_join_text_url: "I was looking at",
            just_now: 'Now',
            X_seconds_ago: '{x} seconds ago',
            about_a_minute_ago: 'About a minute ago',
            about_X_minutes_ago: 'About {x} minutes ago',
            about_an_hour_ago: 'About an hour ago',
            about_X_hours_ago: 'About {x} hours ago',
            about_a_day_ago: 'About a day ago',
            about_X_days_ago: 'About {x} ημέρες ago'
        },
        map:
        {
            latitude: 37.966667,
            longitude: 23.716667,
            zoom: 8
        }
    }
</script>

and immediately after that code I have that code:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=MyKey&sensor=true"> </script>
<script type="text/javascript" src="js/js.js"></script>
<script type="text/javascript" src="js/jquery.tweet.js"></script>

Then from within the js.js file I try to use the ops variable but with no any luck. Do you have any idea why I cannot use the variable ops into the js.js.

Into js file I have use that code in order to determine the type of the ops variable:

console.dir(ops);

and in my Console I get the undefined result.

Have I did anything wrong ?


I did some progress. The js.js file is like that:

jQuery(document).ready(
    function()
    {
        console.dir(typeof ops);
        ...
        File code here
        ...
    }
);

The above code return in my console "undefined"

Now I have try that:

console.dir(typeof ops);

jQuery(document).ready(
    function()
    {
        console.dir(typeof ops);
        ...
        File code here
        ...
    }
);

and return in my Console, "Object" for the first try and "undefined" for the second typeof.

Can that help ?

1
  • 1
    i tryed the code (without the googleapis) and it seems to work fine, can you show us how you trying to access ops? i'm using chrome. Commented May 22, 2012 at 10:48

1 Answer 1

2
$.getScript("js.js", function() {
    //declare ops-variable in here and use it
});

Without jquery:

var Loader = function () {

}
Loader.prototype = {
require: function (scripts, callback) {
    this.loadCount      = 0;
    this.totalRequired  = scripts.length;
    this.callback       = callback;

    for (var i = 0; i < scripts.length; i++) {
        this.writeScript(scripts[i]);
    }
},
loaded: function (evt) {
    this.loadCount++;

    if (this.loadCount == this.totalRequired && typeof this.callback == 'function') this.callback.call();
},
writeScript: function (src) {
    var self = this;
    var s = document.createElement('script');
    s.type = "text/javascript";
    s.async = true;
    s.src = src;
    s.addEventListener('load', function (e) { self.loaded(e); }, false);
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(s);
}
}

 var l = new Loader();
l.require([
"js.js"
function() {
    // do same stuff in this callback
});
Sign up to request clarification or add additional context in comments.

6 Comments

I cannot use that, because the jQuery library is include it into js.js file for speed optimization issue. Your answer upvoted from me because of the info ;) Thanks ...
@MerianosNikos I added a non jquery async solution. Maybe it can help
Do you know, what's wrong with the external variables in jQuery(document).read(.... ?
@MerianosNikos Is jquery loaded when you try to use your document ready function?
@MerianosNikos have you tried replacing it with $(window).load(function() { }); ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.