Jump to content

codefossa

Members
  • Posts

    583
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by codefossa

  1. Unless you are wanting to remove the submit button, then the two above me are correct. If you do want to remove it and still be able to submit ..

     

    Something like this could work.

    window.document.querySelector("#myFormId").addEventListener("keyup", function(event)
    {
    if (event.which == 13)
    	this.submit();
    }, false);

  2. Well I assume you want to be able to use them for logging in as well, and you don't want a ton of files for each login. You can put it all into one, then grep through the file to see if the login + pass exists for logging in. For registration, you would make sure the name doesn't exist, then append it to the file.

  3. I wanted to let ya figure it out on your own first, but here. (:

    Demo: http://xaotique.no-ip.org/tmp/41/

     

    // When page loads ...
    window.addEventListener("load", function()
    {
       // Run in 8 seconds
       setTimeout(function()
       {
           // Put div into variable and show it
           var div = window.document.querySelector("#myDiv");
           div.style.display = "block";
    
           // Run in 5 seconds
           setTimeout(function()
           {
               // Hide div
               div.style.display = "none";
           }, 5000);
       }, 8000);
    }, false);

  4. Your variables are declared outside of the function for a start.

     

    Why not just set the max length of the input field to 1? JS is client sided, so it's the same security .. If you need JS to deal with it on top of that, I would still keep the input's length limit just to make things easier for the client. Maybe not even bother reading the value from the field ..

     

    Demo: http://xaotique.no-ip.org/tmp/40/

     

    HTML

    <form method="post" action="#">
    <input type="text" maxlength="1" id="word" style="width: 15px; text-align: center;" />
    <span id="result"></span>
    </form>

     

    Javascript

    // Wait for window to load
    window.addEventListener("load", function()
    {
    // Wait for keyup to trigger
    window.document.querySelector("#word").addEventListener("keyup", function(event)
    {
    	// Check inputted value
    	var input = event.which >= 65 && event.which <= 90 ? String.fromCharCode(event.keyCode) : null;
    
    	// Check if key pressed is a-z (add more above)
    	if (input != null)
    	{
    		// Set the input field to the character.
    		this.value = input;
    	}
    	else
    	{
    		// Key pressed wasn't a-z
    	}
    }, false);
    }, false);

  5. Why switch between double and single quotes for element attributes? Not wrong, just bad for reading. The extra double quote in the input element may be the problem, and you should check a doctype that fits your site to keep everything easier to manage.

     

    Also, if you give a link, I could check the link on my phone.

  6. It looks to me like you're gonna have to do each char individually. Have fun. :P Hopefully their alphabet ain't too huge. lol

     

    The hex column here can help ya out. I'm not sure that's the whole alphabet though.

    www.jlg-utilities.com/documentation_1_1/alphabets/Croatian.html

  7. It works with the help of jquery :)

    Thanks CPD and Xaotique

    If I were you, I'd use the second method I showed. Simply because, there's no real reason to include jQuery just for a single action. Everything else you have is longhand, so you may as well just add one extra function.

     

    Just my thought. Glad ya got it.

  8. You shouldn't need window.document.querySelectorAll(). The document object inherits this method so document.querySelectorAll() should be just fine.

    I've always used window.document. Habit from GM scripts. lol I don't think it makes a difference, but I could be wrong.
  9. CPD's suggestion will help you learn better. But once you know what you're doing, I would still use jQuery's functions.

     

    I know I need to stop just writing code for people ... Came to this realization after I wrote it. lol

    Demo: http://xaotique.no-ip.org/tmp/39/

     

    So .. lets look at what this would be like in jQuery quick.

    $(document).ready(function()
    {
       $("#myTable td").click(function()
       {
           $(this).fadeOut();
       });
    });

     

    If you wanted to do it the long way though, you could do something like this.

    window.addEventListener("load", function()
    {
    function fadeOut(elm, speed)
    {
    	speed = speed / 100;
    	elm.style.opacity = 1;
    
    	var timer = setInterval(function()
    	{
    		if (Number(elm.style.opacity) - speed < 0)
    		{
    			elm.parentNode.removeChild(elm);
    			clearInerval(timer);
    		}
    		else
    		{
    			elm.style.opacity -= speed;
    		}
    	}, 50);
    }
    
    var items = window.document.querySelectorAll("#myTable td");
    
    for (var i in items)
    {
    	items[i].addEventListener("click", function()
    	{
    		fadeOut(this, 5);
    	}, false);
    }
    }, false);

  10. So you need to cycle through the array:

     

    for(i = 0; i <= document.forms["Myform"].elements.length; i++) {
      // Code omitted
    }
    

     

    Wouldn't that calculate the form length each time? I think it might be a little better to do something like ..

    for (var i = 0, n = document.forms["Myform"].elements.length; i <= n; i++)

  11. Something like this would probably work out better. Inside the if (del) you could do whatever you want it to do when you delete. I just let the form continue submitting, but in your case it may be different. You could use Ajax or whatever you want in there.

     

    On a side note, you shouldn't use $_GET parameters to delete anything. What if a spider comes and scans them links, wherever they're posted, or people trick each other into going to them. You should use $_POST.

     

    Demo: http://xaotique.no-ip.org/tmp/38/

     

    window.addEventListener('load', function()
    {
    window.document.querySelector("#myForm").addEventListener('submit', function(event)
    {
    	event.preventDefault();
    
    	var del    = confirm("Are you sure you want to delete this topic?") ? true : false;
    	var status = del ? "You confirmed, thanks!" : "You cancelled the action.";
    
    	window.document.querySelector("#status").innerHTML = status;
    
    	if (del)
    		window.document.querySelector("#myForm").submit();
    }, false);
    }, false);

  12. Well .. I guess I'll be nice enough to go through a post for the new year. :P

    Demo: http://xaotique.no-ip.org/tmp/37/

     

    First, let's get a time from PHP. This is just in case they don't have JS enabled, they'll still get a time. We are also going to provide the HTML used by JS afterwards.

     

    <span id="remaining">
    <?php
    
    function timeRemaining($date)
    {
    	$diff = strtotime($date) - time();
    
    	$day   = floor($diff / (60 * 60 * 24));
    	$day   = $day < 10 ? "0" . $day : $day;
    	$diff -= $day * 60 * 60 * 24;
    
    	$hour  = floor($diff / (60 * 60));
    	$hour  = $hour < 10 ? "0" . $hour : $hour;
    	$diff -= $hour * 60 * 60;
    
    	$min   = floor($diff / 60);
    	$min   = $min < 10 ? "0" . $min : $min;
    	$diff -= $min * 60;
    
    	return "{$day}:{$hour}:{$min}:{$diff}";
    }
    
    echo timeRemaining("1/1/2014");
    
    ?>
    </span>

     

    I made a small function to get the time until whatever date (using strtotime(), so you could use other formats). Now, this is just going to get the time until that date based on the current server time. We will set the Javascript to use the same timezone as the server, in my case Eastern US, so -5 from UTC.

     

    Note that I'm using jQuery, just so I don't have to type extra code ..

     

    $(document).ready(function()
    {
    // Function that will get us a timestamp for an offset.
    function getTimeOffset(offset)
    {
    	var date = new Date();
    	var time = new Date((date.getTime() + (date.getTimezoneOffset() * 60000)) + (3600000 * offset));
    
    	return time.getTime();
    }
    
    // Function to calculate difference in time. (like the one we made in php)
    function timeRemaining(diff)
    {
    	var day = Math.floor(diff / (60 * 60 * 24 * 1000));
    	day = day < 10 ? "0" + day : day;
    	diff -= day * 60 * 60 * 24 * 1000;
    
    	var hour = Math.floor(diff / (60 * 60 * 1000));
    	hour = hour < 10 ? "0" + hour : hour;
    	diff -= hour * 60 * 60 * 1000;
    
    	var min = Math.floor(diff / (60 * 1000));
    	min = min < 10 ? "0" + min : min;
    	diff -= min * 60 * 1000;
    
    	var sec = Math.floor(diff / 1000);
    	sec = sec < 10 ? "0" + sec : sec;
    
    	return day + ":" + hour + ":" + min + ":" + sec;
    }
    
    var endTime = new Date("1/1/2014").getTime();
    
    // A timer that will update every half a second.
    var timer = setInterval(function()
    {
    	var diff = endTime - getTimeOffset(-5);
    	$("#remaining").html(timeRemaining(diff));
    }, 500);
    });

     

    You can do checks that it's not past the date and stuff, but at the moment it would show negative time until a date that has passed. If you have any questions, feel free to ask, but it should be easy enough to follow along if you know any PHP & JS. Hopefully this is what ya wanted. It's what I got from what I could understand of your question.

×
×
  • 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.