JavaScript - Creeping Line Scenario
I've started studying JavaScript recently. The following simple scenario from the Russian textbook cannot be implemented on my PC.
Code: <HTML> <HEAD> <TITLE>Creeping line example</TITLE> <SCRIPT LANGUAGE="JavaScript"> var msg = "Взгляните, как ровно движется строка! Вставляет?"; spacer = "... ..."; pos = 0; function ScrollMessage() { window.status = msg.substring(pos, msg.length) + spacer + msg.substring(0,pos); pos++; if (pos > msg.length) pos = 0; window.setTimeout("ScrollMessage()",80); } ScrollMessage(); </SCRIPT> </HEAD> <BODY> <h1>Creeping line example</h1> Have a look at the creeping line in the lower part of your browser window. </BODY> </HTML> I just typed the script from the book char by char. I use Firefox browser, v.3.5.7. The error console does not display any mistakes. Please, explain me why creeping line does not appear in the browser window. Similar TutorialsI am trying to implement a javascript on a page and the error that I get is Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /fullsize/image.php on line 131 On line 131 the code is Code: echo"<img id='thepic' onClick="scaleImg('thepic')" onLoad="scaleImg('thepic')" src='$image'>"; sub: Please help me to get all characters from every line from a word-wrapped div using javascript. Detail: Need to get all the characters for first line, second line, third line,... separately from the word-wrapped div Thanks for your reply. hi, I am tring to display some texts in a textbox using \n to create a new line but it does not work. PHP Code: <script> document.getElementById("txtbox1").value=" this is my firstline \n This is my second line \n this is my third line" </script> <input type="text" id="txtbox1"> thanks How do I get new line to work in javascript. Everytime I try to use it, it never works. There should be a new line with 5 * instead of having 17 * on one line. Code: <html> <body> <script type ="text/javascript"> var x=0; for (x=0;x<=12;x++) { document.writeln("*"); } document.writeln("\n"); for (x=0;x<=5;x++){ document.writeln("*"); } </script> </body> <html> the HTML code: Code: <ul id="ul1"> <li>1111</li> <li>2222</li> </ul> <div id="div1"></div> the javascript code: Code: var oDiv=document.getElementById('div1'); oDiv.style.left=getPos(this).left+this.offsetWidth+'px'; oDiv.style.top=getPos(this).top+'px'; function getPos(obj){ var aPos={left: null, top: null}; while(obj) { aPos.left+=obj.offsetLeft; aPos.top+=obj.offsetTop; obj=obj.offsetParent; } return aPos; } why it write the while loop,and what's effection of the while loop obj=obj.offsetParent; what's this line meaning? Hi, I'm creating a new email in javascript via outlook. When i try entering a line break "\n", "\r", "\r\r", etc, Outlook just ignores it completely. I've tried using "%0D%0A" and "fromCharCode(10)" as well but it doesnt seem to read it as a line break. Is there any way to code it so that a line break or a carriage return appears on outlook? I know you can add data to array wiht the push() method but can you add a whole new line as in a = new Array a [1] [1,2,3] so it would create a new line a [2] [4,5,6]; The syntax to the array i think is wrong but you get the question. Hi, I'm trying to do an assignment, and have copied a piece of code to learn while I do, if you see what I mean. Problem is, I'm having real trouble with one of the lines of code. If someone could tell me basically what it does, I'd be very greatful! Here's the code: if (height[i] == -(array[i].height)) The - before (array[i].height) is really confusing me! Thanks in advance! Is there any way to have a multiline text string in javascript? For example strname="line one of string line two of string"; I get an error that the string is not terminated. The reason I want to do this is from the following code: Code: <textarea name="comments" id="comments" cols="90" rows="8" ></textarea> <?php if (isset($_REQUEST["registered"])) { $end_of_line=chr(10); $carriage_return=chr(13); $step1=str_replace($carriage_return,"",$comments); $step2=str_replace($end_of_line,"<br>",$step1); echo "<script type='text/javascript'>\n"; echo "intermediate='",$step2,"';\n"; echo "comment_value=",'intermediate.replace(/<br>/g,"\n")',";\n"; echo "document.getElementById('comments').value=comment_value;</script>\n"; }?> I an getting the value to set a textarea to from the server. It may have newline characters. If there is a way to have a multiline string, then I could skip all the steps and just echo one javascriopt command. Can this be done? CraigRS I have these line of javascript: Code: $(document).ready(function () { /*$('#first').focus(); $(this).keyup(function (e) { if (e.keyCode == 13) { generate(); } });*/ }); function generate() { if($("input[name='name']").val().length > 1) { fi = $("input[name='name']").val().substring(0,1).toUpperCase(); var sl = ["Cool "]; var sl2 = ["Hand "]; var sl3 = ["Luke",]; { var i = Math.floor(Math.random() * sl.length); var j = Math.floor(Math.random() * sl2.length); var k = Math.floor(Math.random() * sl3.length); var nick = sl[i] + sl2[j] + sl3[k]; } { $("#answer").text("Your Name Is: " +nick); $("#answer").fadeIn("slow"); } ; } } I would like for it to return: Your Name Is: Cool Hand Luke I want the "Cool Hand Luke" to be on a separate line but I don't know how to put a linebreak in the .text string. Is this something somebody could tell me how to do? Where in the line: Code: $("#answer").text("Your Name Is: " +nick); Do I put (<br>) Hello: First post here on CS ... I am wondering why my code will not write a new line or line break. This is fine: Code: <script> document.write("Hello and welcome to javascript!"); </script> But when I try to put a message on two lines, it does not work. The book I'm reading and several websites have stated the below codes will work, but neither do: Code: <script> document.write("Hello and welcome to javascript! \n"); document.write("It is a tricky language to learn!") </script> Code: <script> document.writeln("Hello and welcome to javascript!"); document.writeln("It is a tricky language to learn!") </script> Am I missing something? I have a piece of code that randomly picks a line from a preset list and displays it whenever the page is loaded. I'm using it to generate random quotes on my pages. At the moment, there seems to be a good chance that when the page loads no quote appears at all, just a blank space. Anyone know why this could be happening? the code I am using is: Code: <script type="text/javascript"> q0= "<img src=\"images/quotes/01.png\" />"; q1= "<img src=\"images/quotes/02.png\" />"; q2= "<img src=\"images/quotes/03.png\" />"; q3= "<img src=\"images/quotes/04.png\" />"; q4= "<img src=\"images/quotes/05.png\" />"; q5= "<img src=\"images/quotes/06.png\" />"; q6= "<img src=\"images/quotes/07.png\" />"; q7= "<img src=\"images/quotes/08.png\" />"; q8= "<img src=\"images/quotes/09.png\" />"; q9= "<img src=\"images/quotes/10.png\" />"; q10= "<img src=\"images/quotes/11.png\" />"; q11= "<img src=\"images/quotes/12.png\" />"; var ran_unrounded=Math.random()*11; var ran_number=Math.round(ran_unrounded); document.write(eval("q"+ran_number)); </script> I've searched and searched. It seems the sages, and experienced programmers think var dashes = "------------"; is the easiest way. Certainly there must be some simple (or not as simple) way of printing a line of dashes in javascript without declaring a variable as "----------" (that's just sloppy) I don't mean to be rude but this should be easy. Right? Or must I resort to a loop (probably an integer count) that ends at a declared number like 10 or 20? Thanks, And I really think this thread will come in useful to many others (in future searches) who have had just as hard a time as finding this as I. Thanks again. Hi everyone I am trying to complete some code involving a while loop. It's actually part of an assignment. Although the code is outputting ALMOST correctly there is one thing bugging me. Here is the code: Code: playersScore = rollDie(); document.write(' Sco ' + playersScore); playersPosition = playersScore + playersPosition; document.write(', Squa ' + playersPosition); indexOfNumber = findIndexOf(playersPosition, specialSquaresArray); if (indexOfNumber != -1) { document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber] + '<BR>'); playersPosition = connectedSquaresArray[indexOfNumber]; indexOfNumber = -1; while(playersPosition<80) { playersScore = rollDie() document.write(' Sco ' + playersScore) playersPosition = playersPosition + playersScore document.write(', Squa ' + playersPosition) indexOfNumber = findIndexOf(playersPosition, specialSquaresArray) if(indexOfNumber != -1) { document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber]); playersPosition = connectedSquaresArray[indexOfNumber]; } document.write('<BR>'); Now, from what I can see I think it's correct. But if if(indexOfNumber != -1) is not executed I get the following: Sco 2, Squa 2 Sco 4, Squa 6 OK Sco 4, Squa 6 should be on a new line below Sco 2, Squa 2 but no matter where I add the line break, it just will not do it. Can somebody please point me in the right direction as I am wasting so much time on this silly little problem Many thanks in advance EDIT: Ok after further fiddling with the code I entered document.write('<BR>'); in the required place and removed + '<BR>' from document.write(', Ladder to Squa ' + connectedSquaresArray[indexOfNumber] + '<BR>'); This has fixed the problem so I'm happy now. Sorry if I wasted anybodies time. I can't believe I have spent over 7 hours trying to figure this out! I have a slideshow that goes through pictures once you click on the Play button and doesn't stop until you press on the Stop button. How can I make it so that it automatically plays once the website loads without having to click on the Play button? Heres the code: Code: <div class="tabs png"><span class="tabs-inner"><a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a></span></div> <div class="slider-indent-button"> <a href="#" onClick='$("div.tabs").tabs().play();'>Play</a> <a href="#" onClick='$("div.tabs").tabs().stop();'>Stop</a> </div> All help is much appreciated!!! Hi ! I am trying to plot a line using the variables that I have derived in this "rectangular to polar" conversion program. I am just new to javascript and to programming generally. But with the help of sample codes here, I have managed to deal with some easy problems. I have read that drawing in javascript isn't so easy. Could you give me some hints on how to draw the following variable in polar coordinates. Do I need to use another programming language aside from javascript ? Thank you very much !!! Code: <html> <head> </head> <body> <script language="javascript" type="text/javascript"> function PolarConvert(){ num_1=Number(document.addition.entry_1.value); num_2=Number(document.addition.entry_2.value); valNum1=Math.pow(num_1,2); valNum2=Math.pow(num_2,2); valNum3=Math.sqrt(valNum1 + valNum2); valNum4=Math.atan(num_2/num_1); valNum5=(valNum4*180)/Math.PI; document.addition.endVal.value=valNum3; document.addition.endVal2.value=valNum5; } </script> <form name="addition"> <input type="text" name="entry_1">+ j <input type="text" name="entry_2"> = <input type="text" name="endVal"> angle <input type="text" name="endVal2"><br> <input type="button" value="Convert to polar" onclick="PolarConvert()"> <input type="reset" value="Clear"> </form> </body> </html> How do I change the <br /> tags in the follow code to javascript code that performs the same function. I need new lines where you see the <br /> tags and I can't use <br /> since my javascript is within the header section of my HTML file and the W3C validator doesn't like it. Therefore, how do I change the following code to pure javascript with no <br />'s. I tried using \n and \r\n but maybe I didn't use this right as it didn't work. Please help, I am new to javascript txt="Name: "+name+"Sex: "+sex+"<br />Ethnicity: "+ethnicity+"<br />Region: "+region+"<br />Profession: "+profession+"<br />Membership_status: "+membership_status+"<br />Seeking: "+seeking+"<br />Education: "+education+"<br />Body Type: "+body_type+"<br />Eye Colour: "+eye_colour+"<br />Smoker: "+smoker+"<br />Drinker: "+drinker ; I have this Javascript and I am wanting to create a line break between the tweet and the time it was posted, here is the javascript, I am guessing I need to insert the <BR> tag somewhere but just not sure where? Thanks function twitterCallback2(twitters) { var statusHTML = []; for (var i=0; i<twitters.length; i++){ var username = twitters[i].user.screen_name; var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) { return '<a href="'+url+'">'+url+'</a>'; }).replace(/\B@([_a-z0-9]+)/ig, function(reply) { return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>'; }); statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id_str+'">'+relative_time(twitters[i].created_at)+'</a></li>'); } document.getElementById('twitter_update_list').innerHTML = statusHTML.join(''); } function relative_time(time_value) { var values = time_value.split(" "); time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3]; var parsed_date = Date.parse(time_value); var relative_to = (arguments.length > 1) ? arguments[1] : new Date(); var delta = parseInt((relative_to.getTime() - parsed_date) / 1000); delta = delta + (relative_to.getTimezoneOffset() * 60); if (delta < 60) { return 'less than a minute ago'; } else if(delta < 120) { return '<br>about a minute ago'; } else if(delta < (60*60)) { return (parseInt(delta / 60)).toString() + ' minutes ago'; } else if(delta < (120*60)) { return 'about an hour ago'; } else if(delta < (24*60*60)) { return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago'; } else if(delta < (48*60*60)) { return '1 day ago'; } else { return (parseInt(delta / 86400)).toString() + ' days ago'; } } Hey guys. I'm working on a "Preview Post" function. When the Preview Post button is clicked, it should reload the page, show the preview above the textarea, then send the text back to the textarea so the user can keep working. It works until I move to a second line. If there are line breaks they don't get shown in the preview. Also, the text isn't sent back to the textarea. I have a pair of functions that are supposed to handle the input string. The first function records the input when typing, and the second function picks up the post to print as a preview: You can see what I mean he Code: function preview() { var preview = window.document.postform.inputpost.value; window.document.hidden.inputpreview.value = preview; } function copypreview() { var copypreview = '<?php echo $_POST[inputpreview]; ?>'; window.document.postform.inputpost.value = copypreview; } Here's how I use them: Code: <body onload='copypreview();'> <?php include("navbar.php"); ?> <?php if ($_GET[preview]) { echo "<table id=blog><tr><td>"; echo $_POST[inputpreview]; echo "</td></tr></table>"; } ?> <form name="postform" action="submitpost.php" method="post"> Post Title <input size="40" type="text" name="inputtitle" id="inputtitle"> <?php include("tagbar.php"); ?> <textarea onchange="preview();" cols="101" rows="20" id="inputpost" name="inputpost"></textarea> <div align=center> <input type="submit" value="Submit"/> </form> <form name="hidden" method="post" action="addpost.php?preview=1"> <input type="hidden" name="inputpreview"> <input type="submit" onClick="preview();" value="Preview Post"/> </form> Sorry I know it's a lot of code. I tried to make it as readable as possible. Can someone tell me how to fix this? I thought I might have to find a way to insert <br> tags when the user hits the Enter key, but I could have sworn this thing was working with line breaks before. |