Jump to content

Ingolme

Moderator
  • Posts

    14,901
  • Joined

  • Last visited

  • Days Won

    177

Everything posted by Ingolme

  1. eval() runs PHP code that's in a string. See documentation about it here: http://php.net/eval The string should not have <?php tags in it, because eval() starts off inside the PHP block already and <?php would be a syntax error. You should never use eval(), there's no good reason to and it opens the possibility of users running their own scripts on your server. The first echo would output "This is a $string $time morning!" because the string is wrapped in single-quotes which does not parse variables. The second one would show "This is a beautiful winter morning!" because the variables have been evaluated. The third one would literally print "<?php $code1 $code2" because the string is wrapped in single-quotes. The fourth one would literally print "<?php echo 'Hello World!'; ?>" If you wanted eval to print "Hello World!" then you would have to do this: $code = "echo 'Hello World!';" eval($code); Don't put <?php ?> tags in code that's suppose to be evaluated because it would throw a syntax error. If inside the eval() string you put contents in double quotes it will just consider it a string and not PHP code.
  2. Ingolme

    center navigation

    Set the display property (not position) of the list items to "inline-block". Then set the text-align property of their container (probably .top-nav) to "center".
  3. I just did. Set the text-align property of the element that contains the video to "center".
  4. You can show it in critiques if it's hosted on a web server and works on a web browser. If it's a game that needs to be downloaded then it's in a different category.
  5. You were right in your previous post that your code behaved differently than it would all in a single function. I had not noticed at first that the set() function was being called once for each level of nesting as opposed to only once for each click event. event.currentTarget is a reference to the actual element that the event listener was attached to, as opposed to event.target, which is a reference to the lowest leaf node that was actually clicked on. Events are asynchronous, they don't run along with the rest of the code on the page, only when an event is fired.
  6. You should use the <video> element, not embed. To center it, use CSS. Since the video is an inline element, you can center it using the text-align property of the parent element.
  7. I'm pretty sure he meant this: if (variable == 1) { myFunction(); }
  8. All of them are, but your issue with the code not working has nothing to do with indentation.
  9. It's a mystery. Google keeps these things a secret so that people don't try to cheat the search engine. It is known, however, that a link is more valuable if it is surrounded by meaningful content relevant to the theme of the link's website. In general, the rule is: Make the site for the user, not for search engines. If a link is there, make sure it's something the user actually would be interested in.
  10. That function will always run because you're assigning 1 to the variable.
  11. You can use external scripts on your local computer. Just put the Javascript code in a file called "script.js" in the same directory as your HTML file and then use this code to include it: <script src="script.js"></script> Now, your Javascript is running too early, the HTML has not loaded yet. Move all your Javascript to the very bottom of the document right before the closing </body> tag.
  12. They run at the same time (on the same click event), but in sequence, you can still put both of them inside one function as long as they're in order. What you're doing is no different than this: element.addEventListener("click", bothActions, false); function bothActions() { reset(); set(); } And you can further improve efficiency by just removing those function calls and putting the code right into the parent function: element.addEventListener("click", bothActions, false); function bothActions() { // reset() code pasted here // set() code pasted here } Once you've done that you end up with code that's practically the same as mine.
  13. Your reset and set functions always run at the same time, so putting the code from both of them into one function does the same thing. The reason it doesn't work for levels of nesting is because it has not been designed that way, the "inherit" value is going to interfere. Generally I start all the list items visible (in case the user does not have Javascript enabled), then hide them. What you're doing is starting them all off hidden and then displaying them. Normally I write my code like this, but that requires taking "display:none" out of your stylesheet. function toggleAll(e) { // Reference to elements var items = document.getElementById("nav").getElementsByClassName("d"); var numItems = items.length; // For efficiency // Hide all other items for(var i = 0; i < numItems; i++) { items[i].style.display = "none"; } // Show this item e.currentTarget.style.display = ""; }
  14. You should read the Javascript tutorial again, especially the HTML DOM part of the tutorial: http://www.w3schools.com/js/js_htmldom.asp getElementsByTagName() and other methods are explained there. What you need to do is access your image element. If you understand HTML DOM you should know how to do that.
  15. Javascript runs as soon as it has loaded. That means that the image has not yet loaded. You should load the script after the rest of the document, so that all the HTML elements are already available. It would be more organized to have your Javascript in an external file. You're not updating the imageArea object in the game loop, so nothing is ever going to change. It only contains the values of the image the moment it was first assigned. For the sake of efficiency, don't execute strings in your setTimeout() call. Pass a function reference instead: setTimeout(gameLoop, 10); 10 milliseconds is very short for an interval, that's expecting the browser to do the operations 100 times per second. The browser's normal refresh rate is usually 60 frames per second, so updating more frequently than that is pointless.
  16. What browser are you using? I don't see why you can't paste into the post editor. If the rich text editor is not working properly you can press the little light switch icon to make it a plain text editor. Be sure to wrap your code in [ code ] tags. I can't help much without seeing your code.
  17. The reason it's not working is because you forgot to prepend a protocol to the URL: http://<?php echo $_SERVER['HTTP_HOST']; ?>/09php/html_forms.php There are some things that should be fixed: 1. You shouldn't have a button inside a link, just put the class name on the link itself 2. You actually don't need $_SERVER['HTTP_HOST'] because you can make a link relative to the site root just by prepending a slash. <a href="/09php/html_forms.php" class="btn btn-default">Go back to the form</a>
  18. The toggleAll function is only called when an element is clicked. The instructions are written in order: 1. Hide all elements 2. Then show the element we want shown. The reason this toggleAll function is better than what you have written is that it does not rely on the event capturing and bubbling phase. If you really want, you can embed it "statically" in your HTML like this: <ul class="d" onclick="toggleAll(event)"> Which I would advise against, but that's what your requirement is. To embed it in the HTML you just have to move the global variables into the function: function toggleAll(e) { // Reference to elements var items = document.getElementById("nav").getElementsByClassName("d"); var numItems = items.length; // For efficiency // Hide all other items for(var i = 0; i < numItems; i++) { items[i].style.display = ""; } // Show this item e.currentTarget.style.display = "inherit"; }
  19. I prefer using PDO than MySQLi partly because there's no need to call a store_result() method.
  20. The timstamp is passed as a parameter to your animate() function. function animate(TIMESTAMP) { console.log(TIMESTAMP);
  21. You can get the X, Y, width and height of the image using its offsetLeft, offsetTop, offsetWidth and offsetHeight properties respectively. The offsetLeft and offsetTop coordinates are relative to the image's closest positioned ancestor, also known as its offset parent. I don't know if your target area is an HTML element or just an area defined by numbers. If it's an element, you can use the same properties as the image does. If it's just data then you already know the position and size. // CHANGE THIS LINE TO A PROPER DOM REFERENCE TO YOUR IMAGE var image = document.getElementsByTagName("img")[0]; // Information about the image's position var imageArea { x: image.offsetLeft, y: image.offsetTop, width: image.offsetWidth, height: image.offsetHeight }; // Information about the target area the image should intersect with var targetArea = { x: 100, y: 100, width: 100, height: 200 }; // Collision logic here if (imageArea.x < targetArea.x + targetArea.width && imageArea.x + imageArea.width > targetArea.x && imageArea.y < targetArea.y + targetArea.height && imageArea.height + imageArea.y > targetArea.y) { // collision detected! } If you can't understand this, you still need to learn more about Javascript and do more research. Don't try to do a project that's beyond your capabilities.
  22. I don't think you need to worry about scalability. I'm quite sure Javascript can handle assigning events to thousands of nodes without too much trouble. You shouldn't worry about optimizing code until you're actually encountering slowdowns. This code will do what you want without relying on event capturing. var items = document.getElementById("nav").getElementsByClassName("d"); var numItems = items.length; // For efficiency for(var i = 0; i < numItems; i++) { items[i].addEventListener("click", toggleAll, false); } function toggleAll(e) { // Hide all other items for(var i = 0; i < numItems; i++) { items[i].style.display = ""; } // Show this item e.currentTarget.style.display = "inherit"; }
  23. Well, it would be drag and drop if the movement of the character was doing by clicking on it, moving the mouse and then letting go of the mouse button. It sounds more like character motion is doing through pressing keys or something similar.
  24. You have to know the position of both the image and the area you want it to intersect with in the same frame of reference. Once you have that information, a simple rectangle collision algorithm will tell you whether they're intersecting or not.
×
×
  • Create New...