JavaScript - Alternative To Document.write To Replace Image On Page?
Similar Tutorialsis there any alternative to document.write() which writes only in a specific part of document, does not overwrite whole document. and yeah, in this case the document.getElementById().innerHTML dosent work either, coz its in a loop, here's the code if you wanna see: Code: if (aa==1) { for (i=0;i<a.length;i++) { c = txt; b = a[i].childNodes[0].nodeValue; // a is already assigned someplace in a xml document. txt = (b==" T4 ")? "T4 <br />": ""; document.write(c); } } Note: in the above code, the code: Code: txt = (b==" T4 ")? "T4 <br />": ""; is same as (also can be written as ) the code : Code: if (b==" T4 ") { txt = "T4 <br />" ; } else { txt = "" ; } I am very new to JavaScript, however I already seem semi-stumped. The tutorial I am using, mentions that for tutorial example purposes they use document.write() however it suggests that I avoid using this as it will overwrite the whole HTML page. I know that I can use document.getElementById() for some things... however this begs my question: When do you use document.write(), when do you use document.getElementById() and is there also another method you use to change/write to the HTML Document using JavaScript? Otherwise, do you just use document.getElementById() quite a lot in JavaScript? Like I said, I'm very new to JavaScript, so it's all still very new to me. Cheers, Asher Hi! I want the result of a fileld to appear bellow the form and not in a new blank page (witch happens when you have document.write?). I guess it has something to do with "innerHTML"? Can anyone of you help me to make the result of this IF to appear bellow the form? if (document.Form.TypedInField.value == "The Cable Guy") { document.write("You just wrote a title of a nice movie") } I have the form and everything works perfect, except of that the result of what the user have wrote (beacuse of docuemnt.write) land in blank window. I would be very happy of any of you could solve this for me I have a series of videos which must be loaded dynamically on their pages which then get loaded into an iframe. I need to be able to load these videos using variables for the width and height. It's imperative that I use variables as the sizes of the movies will be determined by the user's resolution. Here's the code I'm currently using Code: <script type="text/javascript"> var dimW = screen.width; var dimH = screen.height; var w1 = dimW.toString(); var h1 = dimH.toString(); document.write('<OBJECT ID="Player" width="'+w1+'" height="'+h1+'" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" style=border:0px;">\n'); document.write('<PARAM name="autoStart" value="True">\n'); document.write('<PARAM name="uiMode" value="full">\n'); document.write('<PARAM name="volume" value="50">\n'); document.write('<PARAM name="mute" value="false">\n'); document.write('<PARAM name="URL" value="../../../videos/oracleChangeLocation_1.wmv">\n'); document.write('<embed src="../../../videos/oracleChangeLocation_1.wmv" width="'+w1+'" height="'+h1+'" autoStart="True" uiMode="full" volume="50" mute="false">\n'); document.write('</embed>\n'); document.write('</OBJECT>\n'); </script> I'm developing in xhtml so naturally, IE doesn't recognize the document.write statements and the page comes up blank in IE. It works flawlessly in FireFox (as expected). So, I need to replace the document.write statements with something else while still being able to pass variables for the movie objects height & width. I've tried this code: Code: <script type="text/javascript"> //alert("The beginning"); var dimW = window.innerWidth; var dimH = window.innerHeight; var w1 = dimW.toString(); var h1 = dimH.toString(); var theNewMovie = document.createElement('p'); var theTextOfTheMovie = document.createTextNode('<OBJECT ID="Player" width="'+w1+'" height="'+h1+'" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" style=border:0px;">\n<PARAM name="autoStart" value="True">\n<PARAM name="uiMode" value="full">\n<PARAM name="volume" value="50">\n<PARAM name="mute" value="false">\n<PARAM name="URL" value="../../../videos/oracleWelcome_1.wmv">\n<embed src="../../../videos/oracleWelcome_1.wmv" width="'+w1+'" height="'+h1+'" autoStart="True" uiMode="full" volume="50" mute="false">\n</embed>\n</OBJECT>\n'); theNewMovie.appendChild(theTextOfTheMovie); document.getElementById('mediaPlayer').appendChild(theNewMovie); </script> . I then have a <td> with the id of "mediaPlayer" but nothing is showing up. Most likely because I'm still fairly new to javascript and am probably forgetting or overlooking something. Can anyone give me any ideas, suggestions, or pointers as to what I am doing wrong? Thanks! M Filename: front.htm Supporting files: ads1.jpg - ads5.jpg, ads.js, fp.jpg, front.htm, logo.jpg, random.js, styles.css --> <title>The Ridgewood Herald Tribune</title> <link href="styles.css" rel="stylesheet" type="text/css" /> <script src="random.js" type="text/javascript"></script> <script src="ads.js" type="text/javascript"></script> </head> <body> <div id="links"> <h1>Contents</h1> <p class="section">Main</p> <p class="linksub"> <a href="#">Home</a><br /> <a href="#">Subscriptions</a><br /> <a href="#">Contact Us</a><br /> <a href="#">News Sources</a><br /><br /> </p> <p class="section">News</p> <p class="linksub"> <a href="#">Local</a><br /> <a href="#">National</a><br /> <a href="#">International</a><br /> </p> <p class="section">Sports</p> <p class="linksub"> <a href="#">Baseball</a><br /> <a href="#">Basketball</a><br /> <a href="#">Football</a><br /> <a href="#">Golf</a><br /> <a href="#">Hockey</a><br /> <a href="#">Miscellaneous</a><br /> </p> <p class="section">Opinion</p> <p class="linksub"> <a href="#">Editorials</a><br /> <a href="#">Columnists</a><br /> <a href="#">Letters</a><br /> </p> <p class="section">Classifieds</p> <p class="linksub"> <a href="#">Employment</a><br /> <a href="#">For Sale</a><br /> <a href="#">Personals</a><br /> <a href="#">Real Estate</a><br /> <a href="#">Wanted</a><br /> </p> <p class="section">Other</p> <p class="linksub"> <a href="#">Business</a><br /> <a href="#">Weather</a><br /> <a href="#">Entertainment</a> </p> </div> <div id="main"> <div id="ads"> <script type="text/javascript"> /* generate a random integer from 1 to 5 */ var rNumber = randInt(5); /* description of the random ad */ var rAd = adDescription(rNumber); /* URL of the random ad */ var rLink = adLink(rNumber); document.write('<a href="'+rLink+'">'); document.write(rLink+"'ads'"+rNumber+"'.jpg'" alt="'rAd'" />); document.write(adLink); document.write("</a>"); </script> </div> <div id="request"><a href="#">Contact us today to place your ad</a></div> <p id="logo"> <img src="logo.jpg" alt="Ridgewood Herald Tribune" /> </p> <p id="fp"> <img src="fp.jpg" alt="" /> </p> <h2>Park Opens</h2> <p id="intro">The <i>Adventure Island</i> theme park opened its doors on Monday near Ridgewood. The park, one of the biggest in New Jersey, drew large crowds, but the long lines didn't deter anyone. "I've been watching them put up the rides over the last year, it's really exciting to finally get inside the gates!" said Ridgewood resident, Denise Brooks.</p> <p class="cont"><a href="#">story continues on page 2...</a></p> <address id="footer"> <b>Ridgewood Herald Tribune</b> ° 10010 Atwood Ave. ° Ridgewood, NJ 07451<br /> Phone: (201)555-1101 ° Fax: (201)555-1102 </address> </div> </body> </html> So I have an HTML page with javascript that takes input values, computes some results, and then puts these results in a table. Works great. Now I realize some users might want to print just the results table. So I add a button that allows the user to request the table in a separate window; clicking the button invokes a script to re-create the table in a separate window. Works fine except: * In the status bar, the window appears to never finish loading * If you go to File -> Print then nothing seems to happen; however, if you close the generated window _then_ the print dialog appears; the window you want to print, however, is gone. Any suggestions? The code invoked by clicking the newly added button: [CODE] function grabResults() { var w = window.open("","ROIResults"+winCount++, "height=400,width=600,menubar=yes"); var d = w.document; d.write('<head>'); d.write('<link href="style.css" title="compact" rel="stylesheet" type="text/css" />'); d.write('<title>Return On Investment Calculations</title>'); d.write('</head>'); d.write('<body>'); d.write('<div id="logoholder">'); d.write(' <img src="TTFlogo2.gif" alt="Cannott display logo" "width="322" width="322" height="51"></p>'); d.write('</div>'); d.write('<div id="header">'); d.write(' <h2>Return on Investment Estimator for Classroom Training</h2>'); d.write('</div>'); d.write('<table rules="all" frame="box">'); d.write('<caption>Results - Estimated ROI</caption>'); d.write(' <tr><th>Course</th>'); d.write('<td class="center" id="courseName">'+cname+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Duration (Days)</th>'); d.write('<td class="center" id="courseDur">'+cdays+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Class Size</th>'); d.write('<td class="center" id="courseSize">'+csize+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Location</th>'); d.write('<td class="center" id="courseLoc">'+teach_loc+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Training Cost</th>'); d.write('<td class="justr" id="courseCost">'+TC+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Initial Value</th>'); d.write('<td class="justr" id="courseInitial">'+startValue+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Returned Value</th>'); d.write('<td class="justr" id="courseValue">'+RV+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Increase in Value</th>'); d.write('<td class="justr" id="courseIncr">'+Inc+'</td>'); d.write(' </tr>'); d.write(' <tr><th>Net ROI after costs</th>'); d.write('<td class="justr" id="courseROI">'+ROI+'</td>'); d.write(' </tr>'); d.write(' <tr><th>ROI Rate</th>'); d.write('<td class="justr" id="courseROIRate">'+ROIR.toFixed(2)+'%</td>'); d.write('</tr> '); */ d.write('</table>'); d.write('<p class="in3">(All amounts are in US Dollars.)</p>'); d.write('</body>'); d.write('</html>'); d.close(); return true; } [ICODE] I've written the following html, <html> <head> <script type="text/javascript"> function createpage(){ var first = document.getElementById('first').value var second = document.getElementById('second').value var third = document.getElementById('third').value var j = window.open('random'); var new_page = '<html><head><script type="text/javascript">function open_win() { window.open(first) window.open(second) window.open(third) } </script></head><body onLoad = "open_win()"></body></html>'; j.document.write(new_page); j.document.close(); } </script> </head> <body> <form action=""> <p> First Link: <input type="text" id="first" name="first"/> Second Link: <input type="text" id="second" name="second"/> Third Link: <input type="text" id="third" name="third"/> <input type=BUTTON value="Submit" name="mySubmit" onClick="createpage();return false;"> </p> </form> </body> </html> With the intention to eventually implement it into a website that "mashes" several URLs together into a tinyurl that, upon being opened, creates several tabs of the content the creator wanted in the single URL. Does that make sense? In order to best do this I figured a page with a random character string (I haven't yet implemented this part, for now 'random' holds the place of the new page/link) would have to be created that would launch all the tabs and then close itself. Here's what it should do... 1) User enter 3 URLs into three forms: First Link: <input type="text" id="first" name="first"/> Second Link: <input type="text" id="second" name="second"/> Third Link: <input type="text" id="third" name="third"/> 2) User clicks Submit button that calls "createpage()" function that's declared in the <head>er. The first part of this function declares the links that were inputted as variables first, second, and, third. <script type="text/javascript"> function createpage(){ var first = document.getElementById('first').value var second = document.getElementById('second').value var third = document.getElementById('third').value 3) "createpage()" function then declares a new window named 'random' to variable j (I have random.html stored in the same folder as this project. It is empty.) var j = window.open('random') 4) Next "createpage()" creates a variable that stores a full page's html code that includes a script that launches each link individually with window.open(). var new_page = '<html><head><script type="text/javascript">function open_win() { window.open(first) window.open(second) window.open(third) } </script></head><body onLoad = "open_win()"></body></html>'; 5) "createpage()" writes the code (stored in variable 'new_page') to document 'random' with j.document.write(new_page); 6) "createpage()" closes the document. I'm working on a website that will basically embed a widget/frame sent by a handler into a user's current page. The user basically adds a script tag to where they would like the HTML to be. The script tag has their settings and is basically a document.write that calls all the code that we want displayed. So here's my problem. We have a map that we need to add in a specific section, and to get the map we have to call another script tag. So we end up having a script tag (map) embedded in another script tag (the code for the widget/frame) or we end up having to document.write inside a document.write. Now this works just fine and as expected in Firefox, Safari, and Chrome. However, Internet Explorer and Opera wait until the first document.write is completely finished before calling the embedded one. Of course the problem with this, is that it takes the map out of the document's flow and just appends it to the bottom left of the page. Since the rest of the page has already been called, there's no way to move the interior "map" script. Any ideas? Basically just trying to figure out how (if even possible) to render an embedded script tag in Internet Explorer and be able to place it properly. I've tried everything that I can think of, including AJAX and Google's unescape script. Any suggestions, I'd greatly appreciate it. Or even if you've encountered a similar problem, and know that it just isn't possible in IE or Opera, that would be fine too. Thanks in advance! My code is here and it works ... However, I would like my dynamic table to show on the same page as my body and not on a new blank page. I have created a DIV and try playing around with the document.getElementById('monTab').innerHTML but it's not working out for me ... What am i missing ? Regards, Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD><TITLE>new Script - Javascript Cours 11</TITLE> <META content="text/html"; charset="UTF-8" http-equiv="content-type"> <SCRIPT type="text/javascript"> function createTable(){ var Etudiant = new Array(Number(prompt("How many Students will you put in ?",""))); document.write("<table border=\"1\">"); for (var i=0; i<Etudiant.length; i++) { Etudiant[i] = window.prompt("S'il vous plait entrez le nom d'un etudiant " + (i+1) + ".","") alert("Nice to see you "+Etudiant[i]); document.write("<td>"+Etudiant[i]+"</td>"); j = parseInt(prompt("Combien de notes voulez vous calculez ?")); for (h=0;h<j;h++){ notes[h] = parseInt(prompt("S'il vous plait entrez la "+(h+1)+" note de "+Etudiant[i])); document.write("<td>"+notes[h]+"</td>"); } document.write("<tr>"); } document.write("</tr>"); document.write("</table>"); document.getElementById('monTab').innerHTML=Etudiant; } </script> <BODY> <H1>Combien de note voulez vous cumulez ?</H1> <br> <br> <input type="button" name="btnSubmit" value="TRY IT" onclick="createTable()"> <div id="monTab" size="10"> Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ...Content should come here ... </div> </BODY> </HTML> Hi i have a function (i wont write it out coz it is too long!) but after i have done the main calculations within this function i wont to display some of the variables in a table. i have writen: document.write( <table border="1"> <tr><td> Value 1 </td><td> document.write(a); //here * </td></tr> </table> ); were it says "here *" i wont to write within the table variable a (which i have defined properly in the function). However, as i have already written document.write it actually displays the text "document.write(a);" instead of the value for var a. Help asap please as it in for school soon! Thanks in advanced :D What is an alternative to document.write? I know I could use <p> in html </p> I would like to use JS though. I hope you can understand my question. I am 14yo and this is not for homework. I just wanna try and learn JS. I read document.write can cause me problems. But all I ever see is: <script type="text/javascript"> document.write("any words"); </script> what alternate could I use? Hello, I'm new to Javascript. I know how to make a script that when you click on a button it changes a variable. I also know that with document.write I can express this variable but I do not know how to refresh document.write! For example, if I have a var car =3 and when I click on a button car++ (or increases by 1 so it becomes 4 then 5 and so forth depending how many times you click the button) and I write document.write (car), it only shows the 3 and no matter how many times I click on the button, the 3 doesn't change. How can I get the document.write to show the updated variable? Thanks! So I want to have a button that when I click it will change this variable and that number will change live on the page. Hello everyone, I am new I found this site while searching for help with my problem. I'm new to javascript and having a very hard time with the document.write. Can anyone tell me why I'm getting this error? Error: missing ) after argument list Source File: file:///G:/CINS%20157/tutorial.12/case1/clist.htm Line: 79, Column: 58 Source Code: document.write("<th id='sumTitle' colspan='2'>" Summary "</th>"); Then there is an arrow pointing between the S and u in Summary. Even if you could just tell me where to look for help with the answer that would be greatly appreciated. If it helps this is the code it is attached to: <script type="text/javascript"> document.write("<table border='1' cellspacing='1'>"); document.write("<tr>"); document.write("<th id='sumTitle' colspan='2'>" Summary "</th>"); document.write("</tr>"); document.write("<tr><th>" Contributors "</th>"); document.write("<td>"+amount.length+ "</td></tr>"); document.write("<tr>") document.write("<th>"); document.write(Amount); document.write("</th>"); document.write("<td>"+amountTotal()+"</td>"); document.write("</tr>"); document.write("</table>"); </script> Thanks in advance for any help you can give me. I really appreciate it. I Am trying to use the following script to generate a url dynamically and I can get the link working just fine with plain txt in the anchor area but when I try to add an image - I am running into trouble. Any help would be much appreciated! what is wrong with the following? <script>document.write ("<a href=\"http://contact."+ domain +"/ws/eBayISAPI.dll?ShowCoreAskSellerQuestion&requested="+ userid +"&iid="+ itemid +"&frm=284&redirect=0&SSPageName=PageAskSellerQuestion_VI\"><img src=\"http://www.thebluedot.net/images/layout/contact_us.gif\" style=\"border:none;\"></a>");</script> Please help me out. I have been tearing my hair out over this basic script. I am complete newbie to javascript so please be easy on me. Thanks for any help in advance. ------------------------ The Blue Dot pays top dollar for your used cell phones. 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. Hello, I have the following code, which I plan on using the datepicker with, however the code currently hangs up on the script references, as to my knowlege it looks ok, and I can't seem to find anything wrong with it: Code: <html> <head> <script> function open_cal() { var str_html = "" + "<!DOCTYPE html>" + "<html lang='en'>" + "<head>" + "<meta charset='utf-8'>" + "<title>CALENDAR</title>" + "<link href=jq/jquery-ui.css rel=stylesheet type=text/css/>" + "<script src=jq/jquery.min.js type=text/javascript></script>" + "<script src=jq/jquery-ui.min.js type=text/javascript></script>" + "</head>" + "<body>" + "<p>Hello test</p>" + "</body>" + "</html>" var j = window.open("","CALENDAR","width=200,height=250,status=no,resizable=yes,top=200,left=200") j.document.write(str_html); j.document.close(); } </script> </head> <body> <input onclick="open_cal()" type="button" value="Open" name="B1"> </body> </html> I need your help. Any ideas? Thanks very much in advance, J 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! What other methods can I use to insert code into my HTML pages from an external file, that will be able to be picked up by search engines and will show entirely if you do a "View Source" on my page? I've been using document.write functions in my personal home page for doing things like maintaining nav. bars, footers, etc. However, the HTML that's written using document.write is not picked up by search engines being that its browser-side instead of server-side and if you view the source of the page, all it shows is the call to my JS function that does the document.writes. Hello again, I've continued to study and work on JavaScript. I've had some problems with the document.write() function. When I try to do a math problem, the whole content of the page clears out and only displays the solution. For example: Code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled</title> <script type="text/javascript"> function jahava() { var h = 1 var r = 4 var t = 5 document.write(h += r += t) } </script> </head> <body> <button OnClick="jahava()">Click Me!</button> </body> </html> So If I put other content, like the button I was displaying (which I wanted to stay visible), the function would make them all disappear. I would again appreciate any help towards this matter. Sincerely, Taro B. PS; This is not a calculator related problem, just in-general. |