javascript - 'Node' is undefined. How can we define in application level? -
i facing problem variable scope. if define in 1 js file , how can use in js file (here mean if want use 1 variable in entire application)? can do?
example: in our ie (ie-8 , lower versions of ie) facing problem node types. using constants in many javascript files example: node.element_node giving problems few browsers( 'node' undefined) have replace constant "1" in files. instead of replacing in places (so many files), can define in our starting html file
example below in our starting html file (i.e. index.html) , can use node.element_node in other javascript files? impact in other javascript files ever use node.element_node:
<script type="text/javascript"> if (!window.node){ node = { element_node : 1, attribute_node : 2, text_node : 3, cdata_section_node : 4, entity_reference_node : 5, entity_node : 6, processing_instruction_node : 7, comment_node : 8, document_node : 9, document_type_node : 10, document_fragment_node : 11, notation_node : 12 }; }
create file put javascript code.
// your-script.js if (!window.node){ node = { element_node : 1, attribute_node : 2, text_node : 3, cdata_section_node : 4, entity_reference_node : 5, entity_node : 6, processing_instruction_node : 7, comment_node : 8, document_node : 9, document_type_node : 10, document_fragment_node : 11, notation_node : 12}; } }
then, add line on every page of site :
<script src="your-script.js"></script>
the best put line before </body>
(closing tag). execute javascript after else has been loaded. of course, can still place line want.
note : here, file called "your-script.js", can name want.
this include code in pages. accessible everywhere.
if still have issues solution, wait scripts loaded before executing code :
window.onload = function () { // javascript code goes here }
Comments
Post a Comment