English 中文(简体)
无法在javascript中获取已申报的变量
原标题:Cannot get declared variables in javascript

审议一个javascript file script.js which contained the Code>alert(theVar);。

现在,我把js案与文件等联系起来。

<script type="text/javascript">
    var theVar=  a alert ;
</script>
<script type="text/javascript" src="script.js"></script> //Contains alert(theVar);

可变的<代码>“Var使用,一经通知。 罚款。

何时

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
     var theVar=  a alert ;
 </script>

使用时,即没有发出警报。

我知道这个问题是因为装满小 file后宣布的变数。

但是否有任何办法使文件任何地方宣布的变数?

最佳回答

www.un.org/spanish/ga/president do

window.onload = function() { alert ( theVar ) }

或者说,你喜欢的图书馆很少准备就绪,因此,它在某个事件之后,而不是立即援引呼吁。

虽然这确实取决于您迄今未具体说明的何种功能(<代码>.js)。

问题回答

重要的一点是,守则按适当的顺序取到executed。 在文件满负荷之前,您应推迟对<代码>alert(theVar)的号召。 例如,请在<代码>window的标的上附上<>代码>。

值得注意的是,用外部文件*.js文档并不影响守则的操作方式。

简单的解决办法:

http://code>script.js inclusion to the final row of the one/code>。 这样,就可以使用文件任何时间宣布的变量。

技术解决办法:

script.js 上,在作任何评价之前,请上window.onload。 其结果与较为简单的解决办法相同,但允许你将文字标签放在头上(或在这个问题上的任何地点)。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.