jquery - how to set a variable for external javascript file after the js file is defined? - Stack Overflow

I'm using jQuery Mobile Ajax navigation feature, And I need to change a variable that is defined i

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?

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?

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Sep 11, 2012 at 23:03 Mahdi GhiasiMahdi Ghiasi 15.3k19 gold badges77 silver badges121 bronze badges 8
  • It depends. variable = 'value'; could work – zerkms Commented Sep 11, 2012 at 23:06
  • @zerkms I tried it, but didn't work... (Still getting undefined error) – Mahdi Ghiasi 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. – zerkms Commented Sep 11, 2012 at 23:08
  • Question updated. What other information should I put on the question? – Mahdi Ghiasi Commented Sep 11, 2012 at 23:10
  • If it's out of scope, it's out of scope, period! – adeneo Commented Sep 11, 2012 at 23:10
 |  Show 3 more ments

2 Answers 2

Reset to default 4

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

<script src="http://www.example./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.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745545157a4632305.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信