1

I have JS file hosted at https://cdn.rawgit.com/DarkPotatoKing/darkpotatoking.github.io/master/latest_version.js

When I run the following script on the browser console

jQuery.getScript("https://cdn.rawgit.com/DarkPotatoKing/darkpotatoking.github.io/master/latest_version.js")
	.done(function() {
		alert("done");
	})
	.fail(function() {
		alert("fail");
});

I get a popup that says "done", but when I run

version.number()

the following error message is returned

ReferenceError: version is not define

Why is "version" not defined if the script is supposedly loaded properly?

2
  • from latest_version.js: SyntaxError: missing } after property list Commented Dec 6, 2016 at 15:06
  • I see, got it, it works now when I changed the script Commented Dec 6, 2016 at 15:13

2 Answers 2

1

Your script at has a syntax error, so it cannot be parsed. Correct your script, and version.number() will come:

var version = {
    number: function() { return "1.1"; }; <==Syntax error here
}

Of course you must call version.number() from inside .done(function () {...})

Sign up to request clarification or add additional context in comments.

Comments

0

EDIT

Ok i solved it for you, you need to change that uploaded file to this:

var version = {
    number: function() { return "1.1"; }
}

you have a ; to much in that code.

And this is your new javscript/jquery code:

$.getScript( "https://cdn.rawgit.com/DarkPotatoKing/darkpotatoking.github.io/master/latest_version.js" )
.done(function( script, textStatus ) {
    console.log(version.number);
})
.fail(function( jqxhr, settings, exception ) {
    console.log("nope");
});


OLD

This is because your code is not in sync.

if you run version.number() inside the .done it must work.

2 Comments

I replaced 'alert("done")' with 'vs = version.number()', but when I access vs after that it says vs is undefined. What can I do to access the string returned by version.number() outside the getScript() call?
@DarkPotatoKing you need to stay inside the .done. Why? Little explanation: you request a file, that needs to download so javascript does not wait for that shit and runs the other code and when the file that was requested is downloaded. It wil run the .done code. So when you are outside the .done and ask for vs = version.number() it will say its undefined because the file is not loaded yet.

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.