最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论