JavaScript - Problem With Creating Canvas With Document.createelement
Similar TutorialsHi all, I am attempting to create an element (to be added later to the document DOM) using createElement. My test case is just: document.createElement("<p>Hello World</p>"); My error console shows the following error: Error: uncaught exception: [Exception... "String contains an invalid character" code: "5" nsresult: "0x80530005 (NS_ERROR_DOM_INVALID_CHARACTER_ERR)" location: "http://192.168.1.10/projects/test/public_html/js/test.js Line: 10"] Any ideas? Hello everyone. I'm having a little problem with creating a element in internet explorer. The element that I'm creating is a div with a few style attributes to to it. The div is suppose to cover the whole page almost like a black transparent window on the page. However it is not. The messed up part about this is that it works fine in Firefox, google chrome and I'm sure other browsers (though haven't tested) and when I go to apply the div in my actual code as javascript is suppose to when you tell it create it, it works fine. No problems whatsoever... I'm not to sure what I'm doing wrong here and it would be great if I could get some help with this. Thanks Code: function showPhotoUpload() { var overlay = document.createElement("div"); overlay.setAttribute("style","z-index:3; background:#111111; width:100%; height:100%; position:absolute; top:0; opacity:0.5; filter:alpha(opacity:50);"); document.body.appendChild(overlay).lastChild; } So i 've reviewed my work and can't see what i've done wrong. here is the html <code><html> <head> <title>Javascript Practic</title> <script type="text/javascript" src="Practice.js"> </script> </head> <body> write something here <div id="here"> blue here</div> </body> </html></code> and here is the javascript: <code> window.onload = tryInnerHTML; function tryAlert(){ alert("just trying it out"); } function tryInnerHTML(){ var p = document.createElement('p'); here = document.getElementById("here"); here.style.color = "blue"; p.innerHTML("<b>This is Dynamic</b>"); here.appendChild(p); } </code> firebug is saying the error is p.createElement('p'); but for the life of me I can't guess why any help would be very appreciative. Evening all. Here's a snippet: Code: canvases[i] = document.createElement('canvas'); canvases[i].style.width = '3px'; canvases[i].style.height = '100%'; canvases[i].getContext("2D"); canvases[i].fillRect(1,0,1,table_height); I'm trying to dynamically create a canvas element. Later in the script it gets attached to the DOM, and this works fine if the last two lines are missing. I end up with a canvas element that is 3px wide and 100% of the containing div tall. However, the last two lines make it crash, with the error report 'canvases[i].fillRect is not a function'. If I change the last two lines to Code: var drawing = canvases[i].getContext("2D"); drawing.fillRect(1,0,1,table_height); it says 'drawing is null'. Something seems to be going wrong with the getContext bit, because setting the height and width work fine. Any ideas? It behaves the same if I append the canvas element to the DOM before trying to do the drawing as well. This is in FF4.0.1 with a xhtml1-strict.dtd doctype and valid html. Thanks, Chris Hello guys! I need your help! I want to create a rectangle with a line in the middle with the canvas object. The problem is that the line in the middle gets bigger than the rest of the rectangle. How do I solve this? I believe it has something to do with shadows. If so, how do I turn them off. My code is: <html> <head> <title></title> </head> <body> <canvas id='chart' width='400' height='200'> <script type="text/javascript"> var canvas = document.getElementById('chart'); var ctx = canvas.getContext('2d'); ctx.lineWidth = 10; ctx.strokeRect(0, 0, 400, 200); ctx.moveTo(0, 100); ctx.lineTo(400, 100); ctx.stroke(); </script> </body> </html> Hello all; I am trying to load four images into a canvas using an array. The images load fine and all, however they are resizing weird. I haven't specified what I want them to resize to, as the actual image are already the sizes I want. However when loaded into the canvas they are sizing differently then their actual sizes. Even when I specify their actual size in the code, they still resize weirdly. Here is my code: Code: <body> <canvas id="worldscreen" class="map"></canvas><br /> <script type="text/javascript"> function loadImages(arrow, call) { var images = {}; var loaded = 0; var num = 0; for (var src in arrow) { num++; } for (var src in arrow) { images[src] = new Image(); images[src].onload = function(){ if (++loaded >= num) { call(images); } }; images[src].src = arrow[src]; } } window.onload = function(images) { var canvas = document.getElementById("worldscreen").getContext("2d"); var arrow = { top: "sideQUEST/images/arrow-top.png", right: "sideQUEST/images/arrow-right.png", bottom: "sideQUEST/images/arrow-bottom.png", left: "sideQUEST/images/arrow-left.png", }; loadImages(arrow, function(images) { canvas.drawImage(images.top, 100, 18); canvas.drawImage(images.right, 215, 36); canvas.drawImage(images.bottom, 100, 115); canvas.drawImage(images.left, 36, 36); }); }; </script> </body> You can see the code in action he http://myrmidon16.x10.mx/test.php. This is very frustrating and if you have any suggestions it would be greatly appreciated. Thank you. I'm trying to simulate a textfield on a canvas; that is when clicking on the text box a cursor appears and blink. My problem here is when I create more than one object, the event listener will work only on the last object created. Here is my code: Javascript Code: var canvas; var ctx; var MousePosX; var MousePosY; var Cursor_Width = 1; var Cursor_Height = 21; var Height = 27; var LapseTime = 800; function init(CanvasName) { canvas = document.getElementById(CanvasName); ctx = canvas.getContext("2d"); } /* Create Textfiled */ function Textfield(x, y, w) { Textfield.prototype.x = x; Textfield.prototype.y = y; Textfield.prototype.w = w; Textfield.prototype.Cursor_PosX = x + 3; Textfield.prototype.Cursor_PosY = y + 3; // draw rectangle ctx.beginPath(); ctx.rect(this.x, this.y, this.w, Height); ctx.closePath(); ctx.stroke(); // listen for mouse click event canvas.addEventListener("mousedown", function(evt) { // calculate mouse coordination on canvas MousePosX = evt.clientX - canvas.offsetLeft; MousePosY = evt.clientY - canvas.offsetTop; // if mouse coordination is within texfield display cursor if((MousePosX > Textfield.prototype.x && MousePosX < (Textfield.prototype.x + Textfield.prototype.w)) && (MousePosY > Textfield.prototype.y && MousePosY < (Textfield.prototype.y + Height))) { Textfield.prototype.cursor(); } }); } /* Cursor for textfield */ Textfield.prototype.cursor = function() { // alert("good"); ctx.beginPath(); ctx.strokeStyle = "black"; ctx.rect(this.Cursor_PosX, this.Cursor_PosY, Cursor_Width, Cursor_Height); ctx.closePath(); ctx.stroke(); setTimeout("Textfield.prototype.animate_cursor()", LapseTime); }; /* Change color of cursor to white to make blinking effect */ Textfield.prototype.animate_cursor = function() { ctx.strokeStyle = "white"; ctx.stroke(); setTimeout("Textfield.prototype.cursor()", LapseTime); }; HTML Code: <!DOCTYPE HTML> <html> <head> <style> #MyCanvas { border: 1px solid #000000; } </style> <script src="widgets1.js"></script> <script> window.onload = function () { init('MyCanvas'); var tf1 = new Textfield(10.5, 10.5, 150); var tf2 = new Textfield(10.5, 50.5, 150); }; </script> </head> <body> <br /><br /><br /> <center> <canvas id="MyCanvas" width="700" height="500"> </canvas> </center> </body> </html> What i want to achieve here is have my event listener on any textfield created on the canvas. I created a drawEllipse function for a Canvas library I'm making, and it works fine in Safari, Firefox, and Chrome, but in Opera, the rotation doesn't work. The function is as follows: Code: ctx.drawEllipse = function (fill, x, y, rad1, rad2, r) { ctx.save(); if (r) { ctx.translate(x, y); ctx.rotate(r); ctx.translate(-x, -y); } ctx.scale(rad1, rad2); ctx.beginPath(); ctx.arc()(x / rad1, y / rad2, 1); ctx[fill](); ctx.restore(); }; When I add rotation to the ellipse, in Opera, it doesn't rotate and just gets bigger. I have no idea why. Can anyone suggest anything? Thanks, Julian Hello, I'm trying to create a new link element using JS but using the following code: Code: writeCredits : function () { if (this.credits) return; this.credits = hs.createElement('a', { href: hs.creditsHref, target: hs.creditsTarget, className: 'highslide-credits', innerHTML: hs.lang.creditsText, title: hs.lang.creditsTitle }); this.createOverlay({ overlayId: this.credits, position: this.creditsPosition || 'top left', hsId: 'credits' }); }, It has the target but can I add anything to the createElement to make it open in a new window? Thanks for the help. This opens up in my current Hi all, A few quick questions: Is there a difference between these two lines of code: var e = document.createElement('img'); -and- var e = new Image(); Both seem do do the same thing and act the same way... which makes me wonder why the second one even exists. There isn't a corresponding "new Div();" function, for example.... Second question: If I create an element to use temporarily and do not ever insert it into the DOM, do I need to delete or destroy it afterwards? Will it use memory (a memory leak) if I don't get rid of it after using? Thanks! -- Roger I have a problem that I have been told to fix. The code that is there work perfectly in Firefox, but throws errors in IE and will not work. Code: var newtbody = document.createElement("tbody"); newtbody.id = "ROW"+newkey; document.getElementById(trElement).parentNode.insertBefore(newtbody,document.getElementById(trElement)); newtbody.innerHTML = gcp; gcp has the HTML for inside the tbody tag. The error I get in IE is the "Unknown runtime error". I have tried some of the "solutions" that I have seen online but none have worked. Can anyone help? Please delete, someone had a retard moment
I'm trying to check to see if a dynamically created element exists BEFORE creating another element of the same exact type...but my if statement at the beginning is not working and another element is just created. Code: function writeElement(id) { if(document.getElementsByTagName('transElement')[0]) { var id = id; killElement(); writeElement(id); } var id = id; var transElement = document.createElement('transElement'); var solidElement = document.createElement('solidElement'); var closeElement = document.createElement('closeElement'); //transElement.id = "transElement"; transElement.setAttribute('id', 'transElement'); solidElement.setAttribute('id', 'solidElement'); closeElement.setAttribute('id', 'closeElement'); closeElement.setAttribute('onClick', 'killElement()'); closeElement.innerHTML = "Close"; var content = "inject_poem.php?id=" + id; getdata(content, 'solidElement'); document.body.appendChild(transElement); document.body.appendChild(solidElement); document.body.appendChild(closeElement); } Does anyone know what is incorrect about my if statement? document.write("<table><tr><td><a href='#' onMouseOver='MM_swapImage('changeMe','','images/calNum/calNum4g.jpg',1);'><img id='changeMe' name='changeMe' src='images/calNum/calNum1.jpg' border='0'/></a></td></tr></table>"); basically the one thing I am know is a problem or at least where the problem starts is when the ","(comma) is added in the parathesis for the params of MM_swapImage. Whats the correct way to write those commas? Hey I'm having a problem with this line: Code: var val1 = document.sform.sel1.value; sform is the name of a form that the data is being put onto. Problem is I want to change that name to verifypage.php When I replace sform with the verifypage.php I believe (I have no clues on JS) that the . if the extension of verifypage.php is screwing the code up. It's a dynamic drop down list that I want to display the results on another page. Thanks! Hello everyone. As lame as this may sound, I can't for the life of me get the document by the id. All looks fine to me, but I get the following error that just simply doesn't make sense. Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0) Timestamp: Fri, 15 Jan 2010 01:37:06 UTC Message: Object required Line: 7 Char: 4 Code: 0 Here's the code... Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <script type="text/javascript"> function doSomething() { var elem = document.getElementById('doFade'); alert(elem.src); } window.onload = doSomething(); </script> </head> <body> <img src="/images/aero.jpg" id="doFade" width="300" height="200" /> </body> </html> Hi all, I'm working on an internal site for my company and I have a snippet of code that works 100% fine indepentantly but when I try to use it inside a document.write I can't get it to work. I'm pretty sure it's down to the structure of it but I'm inexperienced with JS so I'm not sure how do correctly structure it. Here is the code I want inside of a document.write(): Code: <a href='javascript: void(0);' style="text-decoration:none; font-size:14px;" onclick=" document.getElementById('my_hidden_div_id_1').innerHTML = '<iframe src=\'{file:link}\' width=\'880\' height=\'500\'></iframe>' $('#my_hidden_div_id_1').koverlay({title: '{file:filename}'}); $('.css_koverlay').css({'background-color':'#9c183a'}); ; "> Pop-Up</a> Could anyone help me correctly insert it inside document.write() so that it outputs without errors? Thanks! here's my javascript code
Code: <script type="text/javascript"> function sel(id){ document.getElementById(id).className="selected"; var getEls = document.getElementById("countrytabs").getElementsByTagName("a"); var getAgn = getEls; for(var i=0; i<getEls.length; i++) { getEls[i].onclick=function() { for (var x=0; x<getAgn.length; x++) { getEls[i].className=getAgn[x].className.replace("selected", ""); } } } } </script> <style> a:hover {color: #FFFF00;} .selected{color: #FFFF00; text-decoration: none; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; line-height: 15px; background-color: #ACAAAB; border: 0px none #ACAAAB; height: 20px; width: 125px; text-align: left;} a{color: #333333; text-decoration: none; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; line-height: 15px; background-color: #ACAAAB; border: 0px none #ACAAAB; height: 20px; width: 125px; text-align: left;} </style> here's my html code Code: <a href="#" class="selected" id="one" onclick="sel('one')"> Bharati Axa </a><br /> <a href="#" id="two" onclick="sel('two')"> BPCL</a> <br /> <a href="#" id="three" onclick="sel('three')"> Hypercity </a><br /> <a href="#" id="four" onclick="sel('four')"> Idea </a> the problem is when i select a link the first time. it works fine. but when i select another link the next time the script does not work. Basically it works only once when page is loaded and then its not working at all. Its not showing any errors either. don't know whats happing.? Can someone please help me its urgent? Any help will be really gratefull! Code: onload=function() {progress1();} var repeat=0; function progress1() { document.getElementById('clib').style.display="inline"; if (repeat<9) { repeat=repeat+1; setTimeout('progress1()',1000); old = document.getElementById('cliba').innerHML; document.getElementById('cliba').innerHML=old+'.'; } else { progress2(); } } var repeata=0; function progress2() { document.getElementById('wlib').style.display="inline"; if (repeata<23) { repeata=repeata+1; setTimeout('progress2()',1000); old = document.getElementById('wliba').innerHML; document.getElementById('wliba').innerHML=old+'.'; } else { progress3(); } } var repeatb=0; function progress3() { document.getElementById('dlib').style.display="inline"; if (repeatb<17) { repeatb=repeatb+1; setTimeout('progress3()',1000); old = document.getElementById('dliba').innerHML; document.getElementById('dliba').innerHML=old+'.'; } else { progress4(); } } var repeatc=0; function progress4() { document.getElementById('slib').style.display="inline"; if (repeatc<4) { repeatc=repeatc+1; setTimeout('progress4()',1000); old = document.getElementById('sliba').innerHML; document.getElementById('sliba').innerHML=old+'.'; } else { progress5(); } } var repeatd=0; function progress5() { document.getElementById('vlib').style.display="inline"; if (repeatd<17) { repeatd=repeatd+1; setTimeout('progress5()',1000); old = document.getElementById('vliba').innerHML; document.getElementById('vliba').innerHML=old+'.'; } else { progress6(); } } var repeate=0; function progress6() { document.getElementById('xlib').style.display="inline"; if (repeate<33) { repeate=repeate+1; setTimeout('progress6()',1000); old = document.getElementById('xliba').innerHML; document.getElementById('xliba').innerHML=old+'.'; } else { pyws(); } } var repeatf=0; function pyws() { document.getElementById('pyws').style.display="inline"; if (repeatf<7) { repeatf=repeat+1; setTimeout('pyws()',1000); old = document.getElementById('pywsa').innerHML; document.getElementById('pywsa').innerHML=old+'.'; } } HI.....idk why it is not working......when I load site, it will start counting, but dots after that .lib files.....look: http://ikoos.tk/load_os/ ......if you don't know what I mean....try to send me a PM. Thank you. document.write("<script type='text/javascript' src='http://www.website.com/javscript.js'></script>"); For some reason this will not output correctly. It ouputs "); Which is the last three characters. |