Jump to content

KevinM1

Moderators
  • Posts

    5,222
  • Joined

  • Last visited

  • Days Won

    26

Everything posted by KevinM1

  1. KevinM1

    Replace

    Dynamic in which way? I mean, if you're using PHP to store/display pages, then scrub user input with htmlEntities before saving it to the DB/file. Or, at the very least, run htmlEntities before outputting it to the screen. If this is supposed to be for validation purposes, the site may not validate if you use JavaScript for this, as I'm not sure if the script actually changes the markup, or merely changes how it's rendered. So, the validator may still see the actual '&' characters instead of their related entities.
  2. KevinM1

    Replace

    I believe you need to get your hands on the actual text nodes in question. Is there any reason why you're not doing this manually? Or simply not doing something like: function htmlEntities(text) { return text.replace(/&/g, '&'); } . . . var myBody = document.getElementsByTagName("body")[0]; var myP = document.createElement("p"); var myText = document.createTextNode("This is text to be appended to a paragraph and to test the '&' symbol."); myText = htmlEntities(myText); myP.appendChild(myText); body.appendChild(myP);
  3. Well, like vikramkeet.singla said, you can filter out the submits by CSS class. It's easiest to use jQuery for this: $(".yourClassName").click(function() { //do whatever you want here }); So, give your submits a class to replace 'yourClassName' and fill out the body of the onclick callback function. That will attach that onclick callback to every element of that class.
  4. <script type="text/javascript"> window.onload = function() { var links = document.getElementsByTagName('a'); var goodLinks = new Array(); for(var i = 0; i < links.length; i++) { if(links[i].href.slice(-5) === "add=1") { goodLinks.push(links[i]); } } //goodLinks is now an array with all <a> elements whose href attribute ends in add=1 } </script>
  5. I think that approach probably would be the best but what do you mean with the div elements would not be visible to a javascript disabled device? can you clearify? He's saying that if the div's are initially set to display: none in the CSS, they won't be visible when the trigger element is interacted with because there's no JavaScript there to handle the event. EDIT: In that case, the OP could put an extra set of those div's in a noscript tag.
  6. You can shorten that code a bit, too: var xReturnValue = -10; var yReturnValue = -5; var arr = document.getElementsByTagName("div"); //you don't need to declare it as an empty array first for(var i = 0; i < arr.length; i++) { var result = arr[i].innerHTML; //you already have an array of all the <div> elements...why not use it? var elem = arr[i]; while(elem != null) //why use two loops when you can get the values all at once? { xReturnValue += elem.offsetLeft; yReturnValue += elem.offsetTop; elem = elem.offsetParent; } var timeString = "&dummy=" + new Date().getTime(); var url = "render.php?result=" + escape(result) + "&xReturnValue=" + escape(xReturnValue) + "&yReturnValue=" + escape(yReturnValue) + timeString; request.open("GET", url, true); request.send(null); } I made a modified version of it (without AJAX) that works in Firefox. Unfortunately, IE7 gives me an unknown runtime error. If you're interested, the script is here: http://www.nightslyr.com/arraytest.html
  7. Just escape it: $dollars = 20; echo "I have \$$dollars.";
  8. Ah, there were a couple of problems with the code I gave you. Scope can be tricky in JavaScript. I actually tested the code I'm about to give you, and it works in both IE and Firefox: <html> <head> <script type="text/javascript"> window.onload = function() { var selects = document.getElementsByTagName("select"); var triggers = document.getElementsByTagName("a"); for(var i = 0; i < triggers.length; i++) { (function() { var trigger = triggers[i]; var currentSelect = selects[i]; trigger.onclick = function() { var selectValue = currentSelect[currentSelect.selectedIndex].value; if(selectValue != "#") { location.href = selectValue; return false; } } })(); } } </script> </head> Be sure to keep the window.onload part. That ensures the code won't fire until everything else is loaded. And, in general, you should write your applications like this: PHP processing (handle user/form requests, get database info, put output into variables that can be echoed/printed) | | V HTML rendering/JavaScript insertion (write the JavaScript & HTML) All your commands/form data should be handled first. Then, you build your page structures from that backend processing. You can include() generic page data (like a footer you use on every page), and store unique/dynamic output in variables. This keeps everything separate and tidy, and reduces on errors by decoupling each type of code from one another. Done this way, the PHP loop to create your <select>'s is completely separate from the JavaScript loop to create the onclick event handlers.
  9. KevinM1

    HDTV

    I'd say those sound like issues I'd want to avoid. D'oh! But, no, the noise issues only happen very rarely. I think it's more to do with me using component cables for TV rather than HDMI, because, like I said before, I have no problems at all playing PS3/Xbox 360 games on it, even when the action gets heavy.
  10. Yup, you'll need a separate id for each link. Remember: id = unique value. By definition, there shouldn't be an id used for more than one element. Another way to do it is to use another element that will only be used as a trigger for navigation for each <select> list. I like using <button>'s. You could do something like: var selects = document.getElementsByTagName("select"); var triggers = document.getElementsByTagName("button"); for(var i = 0; i < triggers.length; i++) //could just as easily have the test "i < selects.length" as selects.length and triggers.length should be the same { (function() { triggers[i].onclick = function() { var currentSelect = selects[i]; var selectValue = currentSelect[currentSelect.selectedIndex].value; if(selectValue != "#") { location.href = selectValue; return false; } } })(); //use a self-executing anonymous function just to make sure scope isn't screwed up } With this, your <button>'s wouldn't even need id's, as each <button> would be tied with each <select>.
  11. I used to feel the same way. There's no real logical reason for that general feeling, though. In fact, there's a term for it -- Not Invented Here (http://en.wikipedia.org/wiki/Not_Invented_Here). Frameworks/libraries exist so you can be more productive. Why reinvent the wheel when someone else already has developed efficient, flexable code that can handle the low-level stuff? Regarding JavaScript, jQuery(http://docs.jquery.com/Main_Page) is an example of a framework done right. It's lightweight, powerful, and makes coding a lot easier. Now, this isn't to say that 'rolling your own' would be a useless endeavor. Even if your own framework isn't as efficient as one of the professional ones, what you learn in creating one is invaluable. Similarly, there are plenty of 3rd party libraries that aren't so hot, so you shouldn't assume that all 3rd party code is wonderful. Still, with that said, you won't lose street cred if you decide to use jQuery, or Prototype, or any of the other good ones.
  12. Mind sending me the web address of your site in a PM so I can look at it?
  13. Yeah, but your function is still needlessly complex even for that. There's no benefit, at all, to using a for-loop or eval in your function. I mean, you can simply do something like: function goToUrl() { if(args.length == 2) { args[0] + ".location" = args[1]; } else { location.href = args[0]; //assuming args[0] is the location you want to go } } Same thing, executed in a much simpler way. In any event, I suggest you don't even do that. There's no reason for HTML to have knowledge of the script(s) working on it. So why place an onclick function call within a <button>? You don't gain any real flexablity. The only benefit is a few lines less of code to write. For me, I'd rather put everything in the <head>, like so: <script type="text/javascript"> window.onload = function() { var myButton = document.getElementById("myButton"); myButton.onclick = function() { location.href = "http://www.google.com/"; } } </script> Because then all my buttons are generic HTML elements, with only id's to determine their functionality. And if their locations are dynamically created, say, by a PHP script, I have one centralized place to put that code, instead of hunting through the HTML to find the right place to put it.
  14. Hmm...have you tried using innerHTML instead of creating a text node? In other words: newElm.innerHTML = "<? echo $featureContent; ?>";
  15. KevinM1

    HDTV

    I have no complaints with my 32' LG LCD HDTV. It's almost two years old, and I've had no major problems. There are some noise/static/pixel issues if there's a lot of herky-jerky motion with a lot of colors (think up-close, full-speed sports replay), but it's pretty rare. Works great with HD-capable game consoles. No picture issues at all with HDMI.
  16. That's needlessly complex for a button to simply redirect the user. I mean eval()? A for-loop? Are you serious? A far easier way to do it: <script type="text/javascript"> window.onload = function() { var backButton = document.getElementById("backButton"); backButton.onclick = function() { location.href = "http://www.google.com/"; } } </script> . . . <button type="button" id="backButton" value="Go Back" />
  17. Well, your main problem is that your PHP loop is causing you to overwrite your JavaScript function every time it iterates, which is why it only brings you to the last option. Regardless, there's a far easier way to go about this. The easiest way to accomplish what you want is to wait for the PHP to write out the HTML, then let the script act on it. That way, you save yourself from the headache of trying to mix and match PHP, HTML, and JavaScript at the same time. I've written a small test file that should illustrate how to go about this. I'll explain it in detail below. Test page code: <html> <head> <script type="text/javascript"> window.onload = function() { var selects = document.getElementsByTagName("select"); var trigger = document.getElementById("trigger"); trigger.onclick = function() { for(var i = 0; i < selects.length; i++) { var mySelect = selects[i]; var selectValue = mySelect[mySelect.selectedIndex].value; if(selectValue != "#") { location.href = selectValue; return false; } } } } </script> </head> <body> <select> <option value="#">Please select a search engine</option> <option value="http://www.google.com/">Google</option> <option value="http://www.yahoo.com/">Yahoo</option> <option value="http://www.msn.com/">MSN</option> </select> <select> <option value="#">Please select a news site</option> <option value="http://www.cnn.com/">CNN</option> <option value="http://www.msnbc.com/">MSNBC</option> <option value="http://www.abcnews.com/">ABC News</option> </select> <br /><br /> <a href="#" id="trigger">GO!</a> </body> </html> 1. window.onload tells the script to wait until the document (the HTML) is fully loaded before acting. 2. We get references to both selects (getElementsByTagName returns an array with all elements matching the tag name) and the link we're using as a trigger for the location change. 3. We create an onclick event handler for the trigger. This does a couple of things: a. It executes a for-loop. b. Inside of this loop, we create a local, temporary variable named mySelect which refers to the current <select> in the loop. This helps us get the selectedIndex value. c. We also create a local, temporary variable named selectValue which holds the selectedIndex value for the current <select> in the loop. This is mainly done to save on typing, because I'm lazy. d. We then check if the value in the current <select> is a url. If so, we go there, if not, we keep looping. And that's all there is to it. Something to note: This method of navigation will only send the user to the first valid option selected by the user. In my example, if the user selects both Google and MSNBC, they'll be taken to Google every time. Also, this example assumes you have only one button/link controlling the entire thing. You can have one button per submit, which wouldn't be much more difficult to implement.
  18. Lots of editors highlight matching braces, but I can see what you're saying. I personally like: if() { } But, logically if() { } Makes more sense.... Oh, I know that a lot of editors highlight matching braces. I mean, I use Notepad++, so I have it right at my fingertips. But, it doesn't really help much if you have several long nested blocks which require you to scroll to see one of the highlighted pair. I rely much more on the indentation markers myself, which is one of the reasons why I like a block style to my coding.
  19. Hey, I just did a check: both versions work in IE7. The id change doesn't show up in the source code, but it works. You can see it in action here: http://www.nightslyr.com/elm1.html You'll notice that the textarea border changes from red to green when the button is clicked.
  20. Yeah, it works in Firefox, but not IE (what else is knew? :-\ ). Unfortunately, trying the direct approach (i.e., elm1.id = "idRemoved") isn't showing any changes to the source when I try it in IE, either. Of course, given how IE works, it may actually be working with that behind the scenes, so try the direct version.
  21. Okay, try something like this: <script type="text/javascript"> window.onload = function() { var submitBtn = document.getElementById("submitBtn"); var elm1 = document.getElementById("elm1"); submitBtn.onclick = function() { elm1.setAttribute("id", "idRemoved"); } } </script> . . . <form name="Name" method="post" action="#"> <textarea name="elm1" wrap="virtual" class="inputTextStyle" id="elm1"><?php echo $variable; ?></textarea> <input type="button" name="convert" id="submitBtn" /> </form>
  22. Can you show how you're invoking these functions (I'm presuming they're functions), and a sample of the HTML they're supposed to be operating on?
  23. First, like everyone else has said, Java and JavaScript are not the same thing. Other than their names, the two have nothing in common. Java is an object oriented programming language whose programs run on a virtual machine. It was created by Sun Microsystems. JavaScript is a scripting language which runs in a web browser. It was created by Netscape. Continuing to refer to JavaScript as Java will cause people to point and laugh. Second, do you have any idea as to what you're doing. I mean, we can all write an onclick callback function to open up popup windows, and show you the basic code (like two people have already done), but that's all worthless if you don't actually understand what it is you're looking at.
  24. That's why I switched to that style myself. I kept getting a lot of parse errors because I'd forget a brace or two. Doing it like this: function myFunc($arg) { foreach($arg as $value) { echo $value; } } Makes it a lot easier on me, and saves me time in the long run as I don't have to hunt for matching braces.
  25. This generally means your script is being run before the document (the HTML) is fully loaded. That element 'has no properties' because it hasn't been loaded yet. Stick your code within a window.onload callback, like so: <script type="text/javascript"> window.onload = function() { //script goes here } </script> That will ensure that the HTML is completely loaded before the script attempts to run.
×
×
  • 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.