1

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>&nbsp;&nbsp;&nbsp; </div>
</div>
</body>
</html>

but i am unable to call the function

5
  • 4
    Open you html in browser. Right mause click - View source code, click on testjs.js. If it open, please see in Error Javascript Console. Commented 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. Commented May 18, 2012 at 9:03
  • What is the error you are getting? It works fine for me. Commented May 18, 2012 at 9:04
  • 1
    jsbin.com/udacan/edit#javascript,html,live Working, do you have both files in same directory ? Commented May 18, 2012 at 9:05
  • sorry i am late actually all the files are in the same path Commented May 18, 2012 at 9:32

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

all the file are in the same path but code is not working can u please tell me why it is happening
run your page then right click ->view source then check if your testjs.js file is loaded or not. check for the error there
How can i check if the file is loaded or not if we right click and view source then it will show the following code 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>.....
Load the site into chrome or firefox with firebug installed. Click F12 and activate the net tab, hit reload and see the 404 on the JS file
1

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>&nbsp;&nbsp;&nbsp;         
      </div>
    </div>
  </body>
</html>

4 Comments

actually its working fine for me if we write the js function in the same html file but it is not working if i place the code in external js file. please help me thanks
How to add css class on this div
dynDiv.className = "cssClass"
I know it works when the js is inline, I just suggest that once you figure out what is wrong, that you switch to jQuery in the code too
0

I tested with external js file.It works fine even on IE6. This kind of problem mostly like" File path is wrong".

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.