-1

I'm using jQuery Mobile Ajax navigation feature, And I need to change a variable that is defined inside external js file, So I can't put my definition before I load that js file...

So, How to change value of a variable for external javascript file after the js file is defined?

(That external js file includes events)


So this question is not duplicate of that question.


Update

My JS File contains events, something like this: $(document).on('mousemove','#main',function() { /*something*/} );

And I need that variable. Is it possible to pass variable to that?

I have tried simply changing that variable i = 5;, but I'm getting undefined error.


Update 2

The external JS file is something for some pages that are almost same, but a little different, just one or two parameters.

And I simply want to pass the parameters to that JS file. Is it possible? How?

8
  • It depends. variable = 'value'; could work Commented Sep 11, 2012 at 23:06
  • @zerkms I tried it, but didn't work... (Still getting undefined error) Commented Sep 11, 2012 at 23:07
  • that's why I said "it depends", because it really depends on what and how you do in that external file. Commented Sep 11, 2012 at 23:08
  • Question updated. What other information should I put on the question? Commented Sep 11, 2012 at 23:10
  • If it's out of scope, it's out of scope, period! Commented Sep 11, 2012 at 23:10

2 Answers 2

4

Let's assume http://www.example.com/external.js defines variable foo, which you want to change.

<script src="http://www.example.com/external.js"></script>
<script type="text/javascript">
    foo = "my new value";
</script>

This assumes that external.js defined foo in the global scope. If it's defined in an anonymous function or similar, you won't be able to change the value.

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

1 Comment

If it's defined in an anonymous function or similar, you won't be able to change the value. Thanks for it!
1

Depending on what you're doing, you can just set the variable and it'll work. Example:

// JS file
blah = "Hello";
function doSomething() {
    alert(blah);
}

// HTML file
blah = "I'm a fish";
doSomething(); // alerts "I'm a fish";

Alternatively, pass the variable as an argument to relevant functions instead of using global variables.

Comments

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.