JavaScript - Quick Javascript Questions
more or less when a user has a command pop up
in this prompt they are asked to type another item from a list with an okay or cancel at the bottom how do i stop the script if a user hits cancel and move onto the calculation step and by pass the other imput prompts? sorry if this isn't the clearest, i am new at javascript. THanks in advance. Similar TutorialsI know that global variables are properties of the window object... so does that mean that local variables are properties of the function they belong to? And does that mean that functions are methods of the window object and that nested functions are methods of the function they belong to?
Hi all, This is a quickie. I'm a JavaScript newbie. I have had a crack programming with it but I need some questions answered to help my understanding. Please tell me the convention that governs the use of "" and '', because so many articles that I am finding in my learning journey are using them interchangeably and it is so confusing. For instance, but not limited to, take the id and value tags of an <input type=... of whatever. Is it best to use '' or "" for the labels you give? What is the convention governing the use of a ; (semicolon) because so many people use one where others do not. I'm under the impression it is an EOL terminator like in Bash, right? So it goes after declaring variables or after something within an if statement perhaps? Thanks QF I am new to java and tried writing the code for practice but it doesn't seem to work <html> <Head> <title>Discount</title> <script language ="Javascript"> <!-- function CalculateBalance(numItems,totPrice,discount) { var balance; balance = (1 - discount)*totPRice; alert ('Your balance is $'+balance +' Thank You.'); document.write("<br>YOu bought "+ numItems+" items for $"+totPrice); document.write("<br>Your balance is $"+ balance+" your discount was"+discount*100+"% Thank you"); } //--> </script> </head> <script language ="Javascript"> <!-- var discount; var numItems = parseInt(prompt('HOw many time did you buy [ >=12 is 20% and >= 6 is 10% discount]',"0")); alert ('You bought '+numItems +' items.'); document.write("You bought "+ numItems+" items."); var totPrice = parseFloat(prompt('The total price that you paid was',"0.0")); if(numItems >= 12) {discount = .2;} else if (numItems >=6) {discount+ .1} else {discount = 0;} CalculateBalance(numItems,totPrice,discount); //--> </script> </html> It only prints to the screen the number of items I bought I am working on a site for an online radio station and I want an external player to launch on site visit and to continue playing as the visitors surf my site. I feel I am 95% there as I have the player spawning on launch and checking for popup blockers. If a popup blocker is found the user is prompted to add my domain to their allowed lists so that future visits will launch the player right out of the box. My problem is this. When someone navigates back to the "home" page (where the script is located the external player window reloads and I do not want it to. I have found a couple of sites that say do this and that but I am a 100% novice at javascript and can not figure it out. here is the url of the site I am building http://www.radiotrips.com/czmradio - If you have popup blocker on you should get an alert. If not a small player should launch here is the code that I am using to spawn the external player and check for popup blockers ************** <script language="javascript" type="text/javascript"> function detectPopupBlocker() { var czmTest = window.open("http://www.radiotrips.com/czmradio/player/mini/no_ads.html","Cozumel Radio","width=478,height=260"); if (!czmTest) { alert("We were unable to automatically open the Cozumel Radio Player due to your pop-up blocker... Please add CozumelRadio.net to your allowed sites for your best listening experience"); } else { window.open("http://www.radiotrips.com/czmradio/player/mini/no_ads.html","Cozumel Radio","width=478,height=260"); } } ************** if anyone could add the code that would make the browser check to see if this window is already open and if so not reload it I would GREATLY appreciate it. I feel like it is 1 to 2 lines of code max according to what I have seen elsewhere but just cant figure it out. Thanks a Million in advance Chris I put the jquery script into my subdirectory. In the header which one is correct: Code: <script type="text/javascript" src="../jquery/jquery.js"></script> or Code: <script src="../jquery/jquery.js"></script> And there is this code: do I put this in the body and enclose it with the <script>tag? Code: $(function(){ // set interval for 5 minutes setInterval(function(){ $.ajax({url: "keep-alive.php"}); }, 300000); }); I have a twitter widget at this side http://1905.gs/blog on the right sidebar. At the bottom of the widget there is the follow me link. I would like to embed a follow me "button" which I have created the script for: The button script: <a href="https://twitter.com/1905GS" class="twitter-follow-button" data-show-count="false" data-lang="tr">Takip et: @1905GS</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script> And here is the current javascript link: http://www.1905.gs/blog/wp-content/p...tter-widget.js How I am trying to make it look like: http://a1202.hizliresim.com/u/d/2nryy.jpg thank you very much for your help, Ok, so I have a couple of questions about JavaScript that I would like answered by the programming gurus on this forum, please. Firstly, I have an idea for an online text-based role-playing game (games like GangsterParadise, etc.), and I am wondering, is it possible (or advisable?) to create the site using nothing but HTML, CSS and JavaScript? Can it be done, and if so, are there any downsides to doing this? I have heard using strictly JavaScript as a programming language on its own, on a site where members will have their own password-protected accounts, should not be done, as there are serious security flaws - is this true, and if so, why? Secondly, most JS programmers on the forum have probably digested and been through hundreds and hundreds of books on the subject since beginning to learn JavaScript - in your opinion, what are the best books to get hold of, for a relative beginner to JavaScript? What book makes the language easy to understand, and doesn't have you scratching your head to make sense of what it is saying? Thanks a lot in advance for the help, it's much appreciated. Hey everyone. I am very new at JavaScript and I need some help. I want to have a section on my home page where a person can choose a bike by clicking on a couple of arrows on the right and left ends of the bike. I want something similar to this type of program. http://qlpros.com/ I don't want a border around the bike. I have been looking for a couple hours now and I haven't found anything. Any help would be much appreciated!! My other question is how to transfer javascript to an external style sheet in dreamweaver. Thanks!! I am newbie in javascript. While being studying it I have not understood some concepts well. JS is prototype-based language, as in Java it has one main prototype Object, also there are some other widely used object, like Array, RegEx, String ...... . So I can access and inherit from this objects/prototypes. So the questions is where do this object (declarations) are stored ? They are also allocated in memory ? Or when JS sees that there is declaration of some object is present i script it reads from file or whatever ? When I am using Function with key word new, what happens. As far as function is also object, it has field prototype and when calling this function with new it returns object of prototype type ? Am I right ? So if I haven't specified prototype it will returns object of Object type ? How does javascript knows about real type of variable. For example when I use regexp, it has method exec. I can call it on every variable. But I guest if variable doesn't have this field it returns undefined ? So the question is how does JS determine what real type of variable is ? Where this information is stored ? Closure in JS. I don't understand this concept well. I think of it like reference count in Java (used or been used by Garbage Collector). If we have a least one reference to variable it will still exist and it doesn't matter where it was declared. So making getter method inside another func makes it exists in memory. If I am wrong please give good explanation of this. Does JS runs in its own virtual machine like Java ? Or browser plays this role. I guess if it has GC, since one process cannot access address space of another process it should be something like virtual machine which controls memory allocation. I would be very grateful for help. Thx in advance. Reply With Quote 01-21-2015, 10:44 AM #2 Dormilich View Profile View Forum Posts Senior Coder Join Date Jan 2010 Location Behind the Wall Posts 3,532 Thanks 13 Thanked 372 Times in 368 Posts Questions About Javascript - JavaScript | Dream.In.Code I need someone to make a small modification to this javascript code: <script type = "text/javascript"> function hyperlink() { var url = (location.href) url = url.replace(/\.(html)/i, "_2.html") window.location.href = url; } </script> Here is the situation: On this webpage: http://tehcake.com/video/30rock/2x14%20-%20Copyx.html where it says "Backup Links 1 2", if you click on the "1" in "Backup Links 1 2" it goes to this webpage, which is what it is supposed to do: http://tehcake.com/video/30rock/2x14%20-%20Copyx_2.html However, on this webpage, where it says "Backup Links 1 2", if you click on the "1" in "Backup Links 1 2" it leads to a non existing page by adding an extra "_2" to the end of the file name. Anyways, I need a way to modify this code (same one as above): <script type = "text/javascript"> function hyperlink() { var url = (location.href) url = url.replace(/\.(html)/i, "_2.html") window.location.href = url; } </script> This script subtracts ".html" and adds "_2.html" to the URL. Anyways, i need to change it so that if the filename has an _2.html in the file name, like in this URL: http://tehcake.com/video/30rock/2x14%20-%20Copyx_2.html the script would not add an extra _2 to the URL, instead it would just link to the same page. I dont know much about javascript so I need help to make this change. Hi, Ever seen on the iphone where you have to slide the slider across the screen to unlock it? I have a duplicate of this using javascript but no matter what I try I cannot make it redirect. I want the page to be redirected after the slider has gone to the other side. Heres the HTML Code: <!DOCTYPE html> <head> <meta charset='UTF-8'> <title>Slide To Unlock</title> <link rel='stylesheet' href='css/style.css'> <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script> <script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script> <script src='js/slidetounlock.js'></script> </head> <body> <div id="page-wrap"> <div id="well"> <h2><strong id="slider"></strong> <span>slide to unlock</span></h2> </div> </div> </body </html> And the javascript Code: $(function() { $("#slider").draggable({ axis: 'x', containment: 'parent', drag: function(event, ui) { if (ui.position.left > 550) { $("#well").fadeOut(); } else { // Apparently Safari isn't allowing partial opacity on text with background clip? Not sure. // $("h2 span").css("opacity", 100 - (ui.position.left / 5)) } }, stop: function(event, ui) { if (ui.position.left < 551) { $(this).animate({ left: 0 }) } } }); // The following credit: http://www.evanblack.com/blog/touch-slide-to-unlock/ $('#slider')[0].addEventListener('touchmove', function(event) { event.preventDefault(); var el = event.target; var touch = event.touches[0]; curX = touch.pageX - this.offsetLeft - 73; if(curX <= 0) return; if(curX > 550){ $('#well').fadeOut(); } el.style.webkitTransform = 'translateX(' + curX + 'px)'; }, false); $('#slider')[0].addEventListener('touchend', function(event) { this.style.webkitTransition = '-webkit-transform 0.3s ease-in'; this.addEventListener( 'webkitTransitionEnd', function( event ) { this.style.webkitTransition = 'none'; }, false ); this.style.webkitTransform = 'translateX(0px)'; }, false); }); I have a few buttons on a page I'm developing and I want the onclick event to call a javascript function in an external file and execute it. I would like to be able to pass a parameter to that function and then either have the function take the user to a new URL or make changes to the webpage content. Initially (just to test) I had inline javascript that caused an alert to popup. That worked fine. Next I took the inline code and put it in a function in an external javascript file that was referenced in the HTML: Code: <script type="text/javascript" src="../assets/js/ews-js.js"></script> <a link="www.xx.com" class="sk-shop-button" onClick="onclick_shop_button">Shop for Supplies</a> The function is as follows: Code: function onclick_shop_button() { window.alert('Life Is Good!'); return false; } When I attempt to call it this way nothing happens. I'm guessing I should not be using a link there since I have to build the URL inside the Javascript (for now though the Javascript is just showing an alert). In one use case I want to be able to pass a parameter which would contain a product name so the function could load the correct webpage. So the function would have to build the URL using the product name as part of the URL then load that page. In a different use case (for a different button) the user would click on an EDIT button that would then update parts of the webpage allowing the user to click on areas to delete content. I am not sure how to implement either use case. I know I can use CSS to modify the look of the webpage and then use subsequent onclick events to do the deleting but am not sure how to delete HTML from within Javascript. Same with how to go to a new URL from within Javascript. Finally I am not sure why the inline Javascript works but the external does not. Any help would be GREATLY appreciated! I have been stuck on a few questions in my computer science class about javascript. I have read the entire chapter but I cannot find the answers to these questions. I believe that I have tryed every important word in every section of the chapter in all questions. I have used every source for this information available to me and after all of them found this website. This is probably extremely basic information but I am unable to solve it. The questions include(They were not numbered in assignment however I have numbered them to try and help): 1. The Graphics class method for displaying text is _________. 2 The applet or JFrame method that returns the Container object that represents the applet's (or JFrame's) space (its visual "real estate") is __________. 3. The import statement needed to use button components in applets or GUI applications is ________. 4. The general term for methods that are invoked as a result of a user action is __________. 5. Clicking a button results in the creation of an _____________ object to represent the button click. 6. Clicking a button may result in notification being sent to an ________ object. 7. The method used to arrange for a button to notify another object when it is clicked later is to an __________. 8. The class definition for objects that receive notifications of user operations on controls like buttons must contain the following phrase: __________. These questions are the last ones that I have left. I have tryed everything. I can submit answers as many times as I want, however there is a deadline. Hello. I have a page that has bunch of moving elements, and obviously I want the movement to be smooth. It worked just great in chrome, but very chunky in firefox. I ran a profiler, found the bottleneck and dealt with it, so things work fine now, but after seeing the result of profiler I now have few questions: The function that is responsible for moving elements around takes 33% of time. Here's the code: Code: Tile.prototype.relocate=function(x,y){ this.elem.style.left=this.x=x; this.elem.style.top=this.y=y; } It's called from timer (setInterval) for elements that are not where they should be (they have .style.position set to "relative"). Is there a better way of moving elements around? Another function: Code: function type(code){ return code>>8; } It takes 13% of time. Now this is just ridiculous. 181854 binary shifts take 174.579ms. Is there a way to make it look like a function call but just replace the call with actual code at compile time, just like C's preprocessor? I'm quite new to JavaScript, but I wanted to add a feature to a sample website that I need to have done by tomorrow night. I have two HTML paragraphs which I will call "a" and "b". Below those paragraphs I have a text box and a submit button. This is how I declared them in the HTML doc: Code: <input type="text" name="message" value="Blank Message" id="newsmessage" /> Code: <input type="button" value="Post" id="submitbutton" onclick="updatepost()" /> Now when I click the submit button I want paragraph b to replace itself with paragraph a, and I want paragraph a to replace itself with the text in the text box. Here's the JavaScript that I have written. (I'm aware it's probably quite off.) Note: paragraph a is toppost, paragraph b is bottompost, the text box is named message, and I created p1 and p2 as temporary holders for the strings. Err... variables. Code: <script type="text/javascript"> function updatepost() { p2=document.getElementById("toppost"); document.getElementById("bottompost").innerHTML=p2; p1=document.message.elements[i]; document.getElementById("toppost").innerHTML=p1; } </script> As of right now the paragraph a holds the text "a" and paragraph b holds the text "b". When I click post paragraph a still reads "a" and paragraph b reads "[object HTMLParagraphElement]". Sorry for my minimal JavaScript knowledge; I have full intentions of fully learning the language quite soon. I just wanted to implement this feature in a site that I had to create and my deadline is tomorrow. Thanks guys! -Dave I recently read a couple blogs that listed some points for writing better Javascript code. Several points applied to the code I write, so they caught my eye. For example, Quote: Use === instead of == and Quote: Use [] Instead of New Array() All of my programs are for numerical computations. Input comes from a textarea box, it is converted to floats, then it is put in arrays, compared, manipulated, etc. For example, Code: rawArray = rawtextareaString.split(/\s+/g); . . . tempx = parseFloat(rawArray[k]); . . . sqAMatrix[i][j] = tempx; . . . Before I rush out and revise all my code to incorporate these suggestions, are there are concerns I should be aware of? Gotchas? Compatibility issues? Should I even bother? I need help with the javascript in this page. I don't know how to do what it is asking me for and I am running out of time. Thanks a million, RHonda <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script> function numberGuess() { stringVar=prompt("Enter your guess number"); while (stringVar<999) { } } </script> <script> function enterNames() { stringVar=prompt("Enter name"); } </script> <script> function enterProducts() { stringVar=prompt("Enter product name"); return } </script> </head> <body> <p> </p> <h1>WDV221 Intro Javascript</h1> <h3 align="center">Javascript Loops</h3> <p>Topics: loops, for loops, while loops, sentinels!</p> <p>Place all script code on this page.</p> <p>1. Create a function that will ask you for a number until you enter 999 which will end the loop. You do not need to display the number. </p> <p>2. Create a function that will ask you for a name until you tell the function that you do not have any more names. After you enter each name it will ask you if you have more names to enter. If you enter yes the process will continue. Display each name at the end of the following sentence.</p> <p>The name you entered is: </p> <p>3. Create a runtime script that will use a prompt( ) to accept how many products will be displayed. Use a for loop to display "Product Name" and the product number as a <p> element.</p> <p><strong>Example output:</strong></p> <p>Product Name 1</p> <p>Product Name 2</p> <p>...</p> <p>4. Create a runtime script that will use a nested for loop to create a table with 3 rows and 5 cells. Place the same number or a letter in each cell. </p> <p>Hint: Write a table in HTML. Do a row first then do multiple rows. </p> <p>5. Create a function called totalSales( ) that will ask the user to enter an amount until you enter "done". Add those amounts together and display them in an alert when the user is done entering amounts..</p> <p>Have fun!!</p> </body> </html> Hello everyone. I just came across a customer that wants me to place 1 video and 1 swf image player in the same place on there website. I tried this with a div layer where i put the video over the swf image but with a different monitor it would move. I did have a time out process of 2.1 mins. Does anyone know of any code that i could put in that would show the video for the time and when it finished it would show the swf images? If so can you tell me how to do this or help me with the code? If you need to see the current code it is simple just straight html done by a different coder. I would of done it with a css but he didn't want to. If you know of a way let me know. Hello! I have a few simple questions and I'm not having any luck with my Google searches. Any information is appreciated! Pardon my pseudo-coding... It's the simplest way to describe what I'm looking for. 1. I have a two dimentional array called List. I want to write a for loop to loop through the first dimension of the array. How is this done? Code: for (Line in List[][]) { write ( List[Line][1] + " = " + List[Line][2] + "<br/>"); } 2. Is there a simple example of how to create and use an object with multiple properties, functions, etc? For example, the following "person" object: Code: Person as object - property FirstName as string - property LastName as string - function Name = property FirstName + " - " + property LastName - property Children[] as array of Person objects - property Gender as string - property Mate as Person object - property DOB as date 3. Can enumeration be done easily in Javascript? Code: enum StatusFlags blank as 0 new as 1 free as 2 reserved as 4 sold as 8 endedum enum StatusDecoration new as "bold" old as "normal" free as "green" notfree as "red" reserved as strikethrough"" unreserved as "no-strikethrough" sold as "italics" unsold as "no-italics" endenum Thanks! Hi, im sorta a newbie but have a few questions i cannot find online anywhere. By the way im trying to create a chrome extension . Its a fairly simple extension, all i want it to do is select a few buttons automatically when the page is loaded. Without me having to click anything. A bot persay. How to automatically select a button/drop down menu How to save a form How to submit a form to a website How it works if you dont get it? Selecting a certain size like S/M/L, color, or anything of that nature AUTOMATICALLY WITHOUT HAVING TO CLICK ANYTHING. Which then adds to my cart. Thats all i want it to really do. Thanks for reading. |