Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
Showing 21-29 of 29 total

Back to Top

<a href="#" onclick="window.scrollTo(0,0); return false">Back to Top</a>

JS translation bookmarklet

I got this mostly working - the only thing that sucks is encoding of non-ascii characters like �, �, etc... I've tried escape(), but it's just stripped the offending character. Anyone know js and feel like fixing this?

javascript:langpair="fr|en";
  text=""+(window.getSelection?window.getSelection()
           :document.getselection?document.getSelection()
           :document.selection.createRange().text);
  if(!text)text=prompt("enter%20text%20to%20translate","");
  if(text!=null)void(window.open(
      "http://google.com/translate_t?langpair="+langpair+"&text="+text,
     "translate","scrollbars=1,resizablel=1,width=500,height=500"))

Javascript Image Preload

<script type="text/javascript">
<!--

if (document.images)
{
preload_image_object = new Image();
// set image url
image_url = new Array();
image_url[0] = "http://mydomain.com/image0.gif";
image_url[1] = "http://mydomain.com/image1.gif";
image_url[2] = "http://mydomain.com/image2.gif";
image_url[3] = "http://mydomain.com/image3.gif";

var i = 0;
for(i=0; i<=3; i++)
preload_image_object.src = image_url[i];
}

//-->
</script>

Back and Forward Links in History

Using form buttons:

<form>
<input type="button" value="Back" onclick="history.back()">
<input type="button" value="Forward" onclick="history.forward()">
<input type="button" value="Reload" onclick="location.reload()">
</form>


Using a normal link:

<a href="javascript:history.back();">Back</a>


Redirect after 5 seconds

<HTML>
<HEAD>
  <TITLE>Redireccionado</TITLE> 
  <SCRIPT LANGUAGE="JavaScript">
  function redireccionar() {
    setTimeout("location.href='articulo.php?id=tw_redireccion'", 5000);
  }
  </SCRIPT>
</HEAD>
<BODY onLoad="redireccionar()">
<P>Bla, bla, bla,...
</BODY>
</HTML>

Validate a email

function validEmail(email) {
  var emailReg = "^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$";
  var regex = new RegExp(emailReg);
  return regex.test(email);
}

Creating nodes with Javascript DOM

    var newa=document.createElement('a');
    newa.className="class";
    var newimg=document.createElement('img');
    newimg.src="./image.png";
    newimg.alt="Image name";
    newimg.className="Image class";
    newa.appendChild(newimg);
    newa.href="#";

Validate a url

function validarURL(valor){
  if (/^w+([.-]?w+)*.w+([.-]?w+)*(.w{2,3})+$/.test(valor)){
   return (true)
  } else {
    return (false);
  }
}

Javascript todo-list sorter

The code below will, if put in the HEAD of a page, move all LI-items that contains a <del>-tag to the bottom of the list. Handy for TODo-lists.

        <script type="text/javascript">
                function organizeList(list) {
                      var listItems = list.getElementsByTagName("LI");
                      var len = listItems.length;
                      for (var i = 0;i<len;i++) {
                            if (listItems[i].getElementsByTagName("DEL").length > 0) {
                                list.appendChild(listItems[i]);
                                i--;
                                len--;
                            }
                      }
                  }
          window.onload = function() {
              var lists = document.getElementsByTagName("UL");
              for (var i = 0;i<lists.length;i++) {
                    organizeList(lists[i]);
              }
          }
        </script>
« Newer Snippets
Older Snippets »
Showing 21-29 of 29 total