Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, January 9, 2012

Infinite Redirect and effects on browser

   Its always great to do something that messes your computer! How about doing something that makes your browser take up 50% of your CPU!
  Javascript has window.location object, generally used for redirecting your page.
 e.g. window.location = "http://www.google.com" redirects you to Google homepage.
 What would happen if you redirected the page to itself! An infinite Reload! Just to see how the browsers behave when this happens, I created an html page with a javascript code redirecting it to itself. As expected the result was an infinte reload happened and the browsers gulped 50% of my processor! I was hoping the usage would go up but it didn't :(
  I was wondering if there was any way, that the browsers could stop this from happening.I found 2 solutions:

1.The browsers can keep a count of number of reloads of a page and on reaching a certain number, the browser can simply kill the page or stop it from refreshing. But then, sites keeping live scores tend to keep on refreshing and you wouldn't like it when the site stops loading at a crucial moment in the game! The solution to this problem could be browsers keeping a count of average time between 2 reloads. If the time is smaller than some smaller certain amount, the page can be stopped from reloading.

2. Web browsers get the javascript code right? So, they can simply ignore the line where the page redirects to itself, if the code line is not within some function. Maybe the

  

Friday, June 17, 2011

New animation: could be used for tutorial pages

   Many times writing tutorials, you may need to put up some questions for the user to answer. It maybe just to give him an example or for some other reason. But, they all have the same working: type the answer and then press the "check!" button. Some will give an alert saying you are incorrect or others will just put up a text or something.But in all of them you have to press the button.
  Lazy guys like me hate doing that much too! So I prefer to use this code to take in the input:

<input type="text" onkeyup="checkanswer()" onfocusout="back()" id="ans"/>

also to show whether the answer is correct or not, I made this script:


<script type="text/javascript">
function checkanswer()
{
var check = document.getElementById("ans");
if(check.value=='Your Answer Here')
{
document.bgColor="#00ff00";
}
else
{
document.bgColor="#ffffff";
}
}


        function back()
       {
                document.bgColor="#ffffff";
       }
</script>


Well the script is self explanatory. After everykeypress, it checks if the answer is correct because of the onkeyup event.If answer is correct, the background colour changes to green, otherwise red. When you move the focus away from the text box the background becomes white away due to the onfocusout event.

This is quite useful for lazy guys like me :)

My main intention was to change the background color of the textbox but could not figure out how to do it so far. So send in your comments on how to do that :)
BTW check the demo here :


Monday, January 31, 2011

A simple Google Search Gadget


  For designing a new-homepage for myself(yeah I do that a lot!), I wanted a google search gadget.So, as usual I decided to make one for myself.Just a simple tweak to one of the online tutorial javascripts and I had a javascript that could be used for google search!
Here's the script that I came up with.

<script type="text/javascript">
function search()
{
var searchkey = getElementById('mytext');
if(myTextField.value != "")
window.location="http://www.google.com/search?q="+myTextField.value;
else
alert("Would you please enter some text?");
}
</script>

To complete the gadget, You need a text-box and a button.Also you need to use 'mytext' as the text-box's id and 'search()' as the onclick event for the button.
So the remaining code is:

<input type='text' id='mytext' /><br>
<input type='button' value='search' onclick='search()' />


This simple script doesn't pass all the parameters that Google does,but I am not Google, am I? This thing does the job so whats the problem? You can make the same thing for Bing by just replacing google.com with bing.com . Also you can make it site specific by using this line:

window.location="http://www.google.com/search?q="+myTextField.value+"site:www.example.com";


Enjoy scripting!