I have one js file and one html file hare is the code of
testjs.js
function DynamicDiv() {
alert('enter');
var dynDiv = document.createElement("div");
dynDiv.id = "divDyna";
dynDiv.innerHTML = "Created using JavaScript";
dynDiv.style.height = "20px";
dynDiv.style.width = "300px";
dynDiv.style.backgroundColor = 'gray';
document.body.appendChild(dynDiv);
}
and my html code is below
<html>
<head>
<TITLE>Test Page</TITLE>
<script type="text/javascript" src="testjs.js" language="javascript"></script>
<script type="text/javascript" src="jquery-1.4.2.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
//"Global" variable accessible to all
//$(document).ready(DynamicDiv());
</script>
</head>
<body onLoad="DynamicDiv()">
<input id="Button1" type="button" value="Using JS" onclick="" />
<div id="Layer1" style="position: absolute; width: 100%; height: 17px; z-index: 1; left: 0px; top: 3px;">
<div align="right"><a href="http://www.uspto.gov/main/patents.htm">PATENTS</a> </div>
</div>
</body>
</html>
but i am unable to call the function
-
4Open you html in browser. Right mause click - View source code, click on testjs.js. If it open, please see in Error Javascript Console.Eugene Trofimenko– Eugene Trofimenko2012年05月18日 09:02:40 +00:00Commented May 18, 2012 at 9:02
-
Have you checked if the path to testjs.js is correct? It needs to be relative to the html file for a standalone html page or you need to put in the correct static url if you are serving it with a static server.tapan– tapan2012年05月18日 09:03:31 +00:00Commented May 18, 2012 at 9:03
-
What is the error you are getting? It works fine for me.Jayan Kuttagupthan– Jayan Kuttagupthan2012年05月18日 09:04:14 +00:00Commented May 18, 2012 at 9:04
-
1jsbin.com/udacan/edit#javascript,html,live Working, do you have both files in same directory ?Riz– Riz2012年05月18日 09:05:24 +00:00Commented May 18, 2012 at 9:05
-
sorry i am late actually all the files are in the same pathDwarika Nath– Dwarika Nath2012年05月18日 09:32:58 +00:00Commented May 18, 2012 at 9:32
4 Answers 4
well your code is woking well with me, may be the path that you have provided to your testjs.js file might not be accurate but its workin fine with me. try checking the path to your js file
5 Comments
Another suggestion, since you load jQuery, load the latest and change the script to use it too
<html>
<head>
<TITLE>Test Page</TITLE>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" language="javascript"></script>
<script type="text/javascript" language="javascript">
function DynamicDiv() { // to be externalised
// alert('enter');
$('<div/>', {
id: 'divDyna',
width:300,
height:20,
css:{border:'1px solid black', 'background-color':'grey'},
html: 'Created using <i>JavaScript</i>'
}).appendTo('body')
}
</script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
DynamicDiv();
});
</script>
</head>
<body>
<input id="Button1" type="button" value="Using JS" onclick="" />
<div id="Layer1" style="position: absolute; width: 100%; height: 17px; z-index: 1; left: 0px; top: 3px;">
<div align="right"><a href="http://www.uspto.gov/main/patents.htm">PATENTS</a>
</div>
</div>
</body>
</html>
4 Comments
I tested with external js file.It works fine even on IE6. This kind of problem mostly like" File path is wrong".
Comments
Your code is working well with me, maybe your internet browser is not compatible or the path that you have provided to your testjs.js file might not be correct. Check the path to your JavaScript file.