Jump to content

Adam

Moderators
  • Posts

    5,717
  • Joined

  • Last visited

  • Days Won

    6

Everything posted by Adam

  1. No the code was a call to methodName(), equivalent to: var str = 'foo'; foo.methodName(); Given that a string is an object, you can still call methods on them even using the shorthand notation. JS provides a few other shorthand notations like that, for example: // Regex /^foo$/.test('bar') // Objects {foo: 'bar'} // Arrays ['foo', 'bar']
  2. .. Just change the src? Sorry, you need to post more information than that for us to help. Best bet would be to post the code with a reasonable explanation of what's happening.
  3. This topic has been moved to PHP Coding Help. http://forums.phpfreaks.com/index.php?topic=362143.0
  4. Prototype is used for extending objects. Every object in JS has prototype, and everything in JS is an object (even functions, strings, numbers, etc.) So essentially, you can use prototype to extend anything. There's a few pre-defined objects you're likely to be familiar with; String, Function, Number, Date, etc. It's also worth noting that unlike in PHP, objects only have properties. However you can assign those properties a Function, so it appears that they have methods, even though it's just a property containing a Function object. Also importantly, those objects - in-fact every object - extends Object. So to create a new, callable property for Strings, you would use: String.prototype.methodName = function() {} To create a new, callable property for every object, you would use: Object.prototype.methodName = function() {} These can only be used on constructed objects by the way, you couldn't call them directly like: String.methodName() It would have to be: 'foo'.methodName() The difference being that the methods are not added directly to the String object, they're added to instances of String objects as they're constructed -- that's prototype's job. Although if you chose you could drop "prototype." from the declaration and call it directly on String, like in my first call example. Making sense? You can also prototype your own, custom objects. For example: function Test() { } Test.prototype.returnFalse = function() { return false; } var test = new Test(); test.returnFalse(); // false Your calls to your example test() function don't do the same thing. The first, with the new keyword, creates a new instance of test() as an object, treating the function as a constructor. The second call just returns true and is stored in o. I'm not sure where you got it from that new is a bad habit? Behind the scenes you're always creating new objects in JS, it's perfectly fine if used right.
  5. Oh.. Ha. I just clicked the link and saw a pool table
  6. LOL fail.
  7. It's perfectly doable but it's not just about learning JavaScript, there's physics involved. You need to be able to determine the velocity of a ball, what effect a cushion would have, what angle you hit other balls at, etc. As mentioned, it's probably not a good starter project.
  8. You're echoing the line-feed in PHP, not JS. That means: ... Is the JS being sent to the browser, which is invalid syntax. Strings can't span over multiple lines without a backslash at the end of the previous line. In PHP you can send the literal string "\n" by escaping it with a backslash (so a double backslash really): echo "<script type=\"text/javascript\">alert('Hello\\n How are you?')</script>"; Or by using single quotes, because special characters aren't parsed in single quote strings: echo '<script type="text/javascript">alert("Hello\n How are you?")</script>'; Although there's no need at all to use PHP to echo the string, you could just close the PHP tag and re-open it after: { ?> <script type="text/javascript">alert('Hello\n How are you?')</script> <?php }
  9. Standard that it's blocked. You might not be seeing any errors because you have the display_errors config set to 0. Please open another thread if you want more help with that though.
  10. That's pretty much standard on a shared host.
  11. ?? Nothing seems to work?
  12. +1 -- there's no benefit in using namespaces the way you are doing.
  13. There's nothing wrong with the syntax. All you're doing is using the php binaries to execute the script, writing the output to nothingness (">/dev/null") and then telling the command to run in the background ("&"). I'm guessing there's a permissions issue or something along those lines, so I would add in the second parameter and remove the write to /dev/null so you can check the response.
  14. The code triggering a hook shouldn't define how the plug-ins work, they should just pass in some context like the blog object/array. Including "hi" within the event name, and using it like you are, forces the plug-in to define an object with a hi() method. Keep it flexible and allow the plug-ins to pass in any callback they want. The "blog.write" in my example was just an event named "write" within a "blog" event namespace by the way. Doing it that way allows you to be more flexible with what your hooks listen for.
  15. Yeah, you're just passing in an anonymous function, not an object. Where are you expecting the add() method to come from? You'd be much better off passing in the data like in my example though, as opposed to some random method names like "add()". That would get confusing, and if there's ever say two pieces of data in question, then how would that work?
  16. Your method is fine to include the plugin code, but as scootstah mentioned you would need to use something like hooks to expose the core functionality. Otherwise your plug-ins are going to be incredibly limited. Hooks are essentially just kind of events that the plug-ins listen for, and can alter what happens or modify some data/objects passed in. There's a load of ways you could implement them, but here's a very rudimentary example: $hooks->register('blog.write', function($blog) { // Do something with the blog return $blog; }); $this->hooks->trigger('blog.write', array($blog)); You'd probably want to allow a callback to be passed in (optionally instead of a function) to allow the plug-ins to become more robust. You would also need to think about error/exception handling, extending core objects, core dependencies, permissions and prioritisation, etc. Think about it before you publish anything; if you dramatically change the implementation after launch you'll annoy any plug-in writers. I can't really comment on some of the challenges you'll face, because I've never actually implemented anything to this level before. Been meaning to for a while though. I would imagine there's some code out there already you could use if you don't fancy writing it yourself.
  17. Hmm.. Is there a reason you're writing all of the pop-up's contents using JS? Why not place it all in an HTML file you specify in open(), and then just use JS to add in the extra bits of data you need? Or perhaps even easier, pass the data in as parameters and build the HTML using PHP?
  18. Including the () after the function means that you're invoking it immediately, and then passing into the setTimeout function anything it may return. Just drop those and you will be passing the function itself: setTimeout(myWindow.setFocus, 2000);
  19. You could just define it before the script is included? <script type="text/javascript"> var someVar = '<?php echo htmlspecialchars($someVar) ?>'; </script> <script type="text/javascript" src="js.js"></script>
  20. My attempt at explaining the JS issue to someone: http://forums.phpfreaks.com/index.php?topic=361547.0
  21. You need to use: myWindow.document.write( "<" + "/script>" ); There's actually a recent thread about this in the misc board as well. Because JS is wrapped in HTML tags, the browser first looks for the opening and closing script tags, then the JS engine parses what's in the middle. So the browser finds the closing script tag, even though it's within a string, and passes the JS engine a broken script.
  22. You can just echo the PHP variable into a JS variable declaration: var someVar = '<?php echo htmlspecialchars($someVar) ?>'; Added the escaping to ensure no characters within the string would break the JS syntax.
  23. I disagree, I'm with Kevin on this one. From my experience the people doing the hiring are a little more down to Earth these days, and appreciate that a degree is not required to be good at the job. It's probably a regional thing though..
  24. Fixed jQuery version: // Create an array of elements var elements = $('#forename, #surname, #sex, #country'); // Bind the focus/blur events elements.focus(function() { // We always want the dim effect hidden when focusing on an element toggleDim(false); }).blur(function() { var showDim = true; // Loop through the elements until we find one with a value elements.each(function() { if ($(this).val() !== '') { showDim = false; return false; } }); toggleDim(showDim); });
  25. Just realised there's a bug in the code. The jQuery each should use the alternative syntax and access the value with the Val() method. I'm on the train at the moment but will update the code when I'm home.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.