Skip to content Skip to sidebar Skip to footer

Accesing Variables Declared In Javascript In A Different Html File

I have two files screen.html and db_fun.js.I have declared a variable at just the beginning as follows: db_fun.js var name = 'abc'; now i tried to access this variable in the s

Solution 1:

add this to the head

<scripttype="text/javascript"src="db_fun.js"></script>

Solution 2:

I think that you should move

<scriptsrc="db_fun.js"type="text/javascript" />

In the head of the page. I.e. before document.write. Because in the moment where you are trying to print 'name', the variable is still not created. Also I think that it is not a very good practice to use document.write to add content to your page. Try using jQuery or some other library. For example:

$(document).ready(function() { 
   $("p").html(name);
});

Solution 3:

That's because you are inserting your JavaScript in the page footer. You have to firstly load your javascript file into your web site (in the head section for example) and then use your variable.

Post a Comment for "Accesing Variables Declared In Javascript In A Different Html File"