Javascript: Global variable within function
Is there a way to set a global Javascript function within HTML.
I can only pass it as a function like
PostComment(this.content)
So i can only access it inside the PostComment function. Is there a way to acc开发者_StackOverflowess it in another function?
Just create the function as normal in your <script> tags like this
function postComment(content){
//process content
}
it will be placed in the global namespace. Such practice is actually frowned against as it pollutes the global namespace. It is best practice to namespace your functions like
myNamesSpace.postComments(this.content) I assume the this here refers to an enclosing object, myNamespace in this case
You can always attach stuff to the global object, which is usually window in a browser.
window.myGlobalVar = ...;
加载中,请稍侯......
精彩评论