Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created an ASP.Net application. I am using a javascript which is written in a separate file. I am using Var myvariableName ={} in javascript file.

I have included this file in MasterPage and accessing myvariableName in my aspx page.

This is working fine in Google Chrome, however, in IE 8 an unhandled exception is thrown as

myvariableName is undefined.

the error shows as; 0x800a1391 - Microsoft JScript runtime error: 'Common' is undefined

where Common is my javascript variable.

Please assist me in resolving this issue.

share|improve this question
 
Please check the rendered HTML and see the scope of calling –  Murali Nov 20 at 14:00
 
In view source I can see that Js file is loaded. –  Vijay Balkawade Nov 20 at 14:03
 
Var should be var... lowercase, but I don't think that's actually your problem, as you said it works in Chrome. Sounds like something is blocking your JS in IE8. Do you have any browser sniffing js or conditional comments, which may be trying to execute code in IE that doesn't execute in Chrome? –  Phillip Wills Nov 20 at 14:09
 
Sorry my mistake, it is var (lowercase). No I don't have any browser sniffing js –  Vijay Balkawade Nov 20 at 14:12
 
@VijayBalkawade are there any errors in the console? –  Grundy Nov 20 at 14:33
show 4 more comments

This question has an open bounty worth +50 reputation from Vijay Balkawade ending tomorrow.

Looking for an answer drawing from credible and/or official sources.

5 Answers

You are probably getting this error due to a missing semicolon. Change the code to this:

var myvariableName = {};
share|improve this answer
 
still it is showing the same error. –  Vijay Balkawade Nov 27 at 10:19
1  
I can see that you have updated your question. You now say that "Common" is your javascript variable. It will be helpful if you include more of your code in your question. –  Fresh Nov 27 at 12:30
add comment

You're probably accessing the variable before your external script is executed.

Be sure to access your variable as soon as the document is fully loaded (i.e. $(document).ready(function(){...}); if you use jQuery) or try to find out the real execution order with some alert (that shouldn't be browser-depentent, by the way!).

share|improve this answer
add comment

In your asp page, where you access your custom variable, wrap your code with:

var myvariableName = {};

window.onload = function(){

  // your code here where you're accessing the variable

};
share|improve this answer
add comment

If your code is already in a document.load or $(document).ready(function) you can always handle the variable before acessing it via

if (typeof myvariableName !== "undefined") {
    // do stuff
}

Some times in IE shit happens and window.load gets screwed specially when async calls are in place.

share|improve this answer
add comment

Try wrapping your code inside an IIFE (Immediately Invoked Function Expression). After you set the variable make it a property of the window so that it is global in scope.

(function() {   
    var common = 'this is a global variable.';
    window.common = common;  
}) ();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.