Click here to Skip to main content
15,902,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to learn javascript and html5, and I have created this file.
But, the external js file dont draw rectangle on my canvas.. if i move it to html page, it works fine, whats the problem? can anybody pointout please..


HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type='text/javascript' src = '/js/main.js'>
</script>
<canvas id="myCanvas" width = "1300" height = "800" style="border: 1px solid black">Your browser does not support 

the HTML5 canvas tag.</canvas>
<script>
var c=document.getElementById('myCanvas');
drawRectangle(c);
</script>
</body>
</html>


here is the js file in folder js...
Main.js

JavaScript
function drawRectangle(c)
{ 
var ctx=c.getContext('2d');
ctx.fillStyle='#FF0000';
ctx.fillRect(0,0,80,100);
}
Posted
Comments
enhzflep 14-Apr-14 8:07am    
What does your javascript console say? My guess is that it shows an error - i.e file_not_found.

Unless your html file exists in the web-server root directory, it won't work - since you've got a '/' as the first character of the source.

Consider the following example:

My server's root folder on this machine is located at:

c:\xampp\htdocs

I put html/php files inside a folder named enhzflep, and images/scripts inside two separate folders: c:\xampp\htdocs\enhzflep\img and c:\xampp\htdocs\enhzflep\js

So, I would have C:\xampp\htdocs\enhzflep\index.html and c:\xampp\htdocs\enhzflep\js\Main.js

If I used your code as-is, it would actually tell the browser to ask for c:\xampp\htdocs\js\Main.js

Notice that we're no longer in the enhzflep folder any more? It's the first slash in the JS source that did that.

To get to the file I actually want (c:\xampp\htdocs\enhzflep\script\Main.js) I would change '/js/main.js' to 'js/main.js'

It's probably also worth getting in the habit of using the consistant capitalization. If your file is named 'Main.js', you should ask for that, rather than 'main.js'.

the error is
Quote:
GET http://127.0.0.1:8020/js/main.js 404 (Not Found) index.html:7
Uncaught ReferenceError: drawRectangle is not defined


change your source code to
<script type='text/javascript' src = 'js/main.js'>
 
Share this answer
 
Everything is looking fine. Remove unnecessary space before and after src attribute and try:

<script type="text/javascript" src="/js/main.js">
</script>
 
Share this answer
 
v2
Hi Yesudasan,
I tried your code.It is working properly.

XML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/main.js"></script>
    <script>
        var c = document.getElementById('myCanvas');
        drawRectangle(c);
    </script>
</head>
<body>
    <canvas id="myCanvas" width="1300" height="800" style="border: 1px solid black">Your browser does not support
 the HTML5 canvas tag.</canvas>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900