Jump to content

Javascript Snippets


Skemcin

Recommended Posts

If you have a script that is simple and easy to use, please send it via PM along with an example where it is being used (live web page) and what purpose it serves. Here is a list of other requirements:

  • Code MUST be helpful to common/daily use, not a customized solution.
  • Code must be compatible with IE,NN,FF,OP on PC and MAC.
  • Code must be commented to describe appropriate functions, conditions, and variables.

If you have any questions, please send me a PM. Do not expect a quick turnaround on posting submissions. I will get to them when I do but I feel that a two or three day turn over period is reasonable.PLEASE, do not go googling solutions and submitting them. I just want simple easy to use COMMON scripts. If this becomes burdensome then I may stop this all together. So use it, don't abuse it.Thanks.SSL REDIRECT

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: use this script to sniff out and redirect a page that you want to force to be loaded in and SSL.// necessary mods: simply change the "yourdomain.com" to the root url of your site and place the script between the <head> tags of the page you wish to load in a secure environment.if (document.location.protocol != "https:") {	document.location.href = "https://www.yourdomain.com" + document.location.pathname;};//--></script>

ALERT/REDIRECT (variation I)

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: used best inside a server side scripting language conditional statement, this sends and alert to the client and then goes to the previous page.// necessary mods: simply change the "Your Message Here" to a user friendly message that communicates the reason for the redirect.alert('Your Message Here - please try again.\nClick OK to continue.');history.back();//--></script>

ALERT/REDIRECT (variation II)

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: used best inside a server side scripting language conditional statement, this sends and alert to the client and then goes to a specific page.// necessary mods: simply change the "Your Message Here" to a user friendly message that communicates the reason for the redirect. For this variation, also change the "pagename.htm" value to the target of the redirect - absolute or relative path.alert('Your Message Here - please try again.\nClick OK to continue.');document.location="pagename.htm";//--></script>

POP-UP WINDOW

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: create a popup window at a certain size and location with desired functionality// necessary mods: each yes/no value can be changed to accommodate desired functionality while the height and width integers can be change to determine the size of the window.  The "top" and "eft" integers allow the popup window to be positioned relative to the top-left corner of the client's monitor. Change the "HelpWindow" name ot assign a desired name to the window.var features="status=no,scrollbars=yes,resizable=yes,menubar=yes,width=385,height=500,top=75,left=75";	function Info(x){		//window.name = "HelpWindow";	var openit = window.open(x, "HelpWindow", features);	}//--></script>

<!-- link code used to call popup - change the values used to reflect your page(s) accordingly --><a href="java script: void(0);" onclick="Info('pagename.htm');" title="text link name">text link name</a>

SIMPLE MOUSE OVER

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: image rollover - partI// necessary mods: replace "aPageID" with the corresponding unique name used in the html provided below and replace "apagename_off" and "apagename_on" with the corresponding image that is the mouse off/on look/design. Save as a separate .js file and reference on every page using mouseovers by using the <script language="javascript" type="text/javascript" src="rollover.js"></script>if (document.images){	aPageIDOn = new Image();	aPageIDOn.src = "images/apagename_on.gif";	aPageIDOff = new Image();	aPageIDOff.src = "images/apagename_off.gif";		bPageIDOn = new Image();	bPageIDOn.src = "images/bpagename_on.gif";	bPageIDOff = new Image();	bPageIDOff.src = "images/bpagename_off.gif";}	function SwitchImage(locat,name)	{	if (document.images){newBar = eval(name + ".src");	document [locat].src = newBar;	}} //--></script>

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: image rollover - partII// necessary mods: none - just put this in the <head> tags on any page where a mouseover is to be used.function SwitchImage(){}//--></script>

<!-- // author: skemcin// description: image rollover - partIII// necessary mods: assigning variables and page references as follows:- replace "apagename.htm" with the target page you with to take the visitor to- replace "aPageID" with the corresponding unique name used in the javascript provided above- replace "apagename_off" with the corresponding image that is the mouse off look/design- replace "xx" with the appropriate widtha nd height of the image used as the rollover--><a href="apagename.htm" title="aPage Title"    onmouseover="SwitchImage('aPageID','aPageIDOn')"   onmouseout="SwitchImage('aPageID','aPageIDOff')">   <img id="aPageID" name="aPageID" src="images/apagename_off.gif" width="xx" height="xx" border="0" alt="aPage Title"></a><br><a href="bpagename.htm" title="aPage Title"    onmouseover="SwitchImage('bPageID','bPageIDOn')"   onmouseout="SwitchImage('bPageID','bPageIDOff')">   <img id="bPageID" name="bPageID" src="images/bpagename_off.gif" width="xx" height="xx" border="0" alt="bPage Title"></a><br>

RANDOM IMAGE LOADER

<script language="javascript" type="text/javascript"><!--//// author: skemcin// description: random image generator - place this code exactly where you want the random image to be rendered.// necessary mods: you will need to do the following things to make this work as desired:// - change "_random/" to the relative or absolute path of the folder where you will store your random images - I highly recommend keeping these seperate from all other images as it makes it easier to maintain.// - change or remove "theme_fall_" to your desire.  I use it to to uniquely identify a series of images that are related.  It is prepended to the random number javascript create whihc in turn constructs the filename used to render/locate the image// - change or remove "width='150' height='100'" the width and height are optional - if you know all your images are the same dimensions, I would recommend keeping them.  If, for whatever reason, you wish to have images that are a variety of sizes, then remove these so they are not physically constrained.// - convention - this is the most important thing!! most random scrips have you define the file names in the script - making it harder to change them around. This script relies on what I have found to be a typical habit and that is to simply name all your images the same name but adding a number on the end.// - change "27" to reflect the highest number assigned to your files.  Since this is javascript, you must have a zero(0) image defined since that is where javascript begins to count image0.gif, image1.gif, image2.gif . . .	random_num = (Math.round(Math.random()*27));	// example of a n image this would find is: http://www.yourdomain.com/_random/theme_fall_12.gif	document.write("<img src='_random/theme_fall_" + random_num + ".gif' width='150' height='100' hspace='0' vspace='0' border='0'><br />");//--></script>

SHOW/HIDE SCRIPT

<!--// author: (as noted in script)// description: Part I - show/hide javascript// necessary mods: none// use source: http://www.iribbit.net/i/_files/toggle.js-->

<!--// author: (as noted in script)// co-author: skemcin// description: Part II - show/hide HTML code// necessary mods: change each of teh respective grp1, grp2, grp3, etc. values as needed - the link needs to be the same value as the div you want it to control.  You can have multiple links control one div, just use the same ID in the link definition. However, only one div can be expanded at a time. Only the last div on the page will be expanded/collapsed if more than one div are given the same ID.// use source: http://www.iribbit.net/i/_files/toggle.htm-->

SLIDING NAVIGATION

<!--// author: prateek// description: Navigation effect that slides the link from left to right a certain amount of pixels.// necessary mods: nonefunction shiftMe(object, direction, pixels){	if (pixels==0)return;	var obj = document.getElementById(object);	if (obj.style.marginLeft=="") obj.style.marginLeft="0px";	var marginLeftVal=obj.style.marginLeft;	var marginLeftInt=new Array();	marginLeftInt=marginLeftVal.split('p');	if (direction=="r")	marginLeftInt[0]--;	if (direction=="l")	marginLeftInt[0]++;	pixels--;	obj.style.marginLeft=marginLeftInt[0]+"px";	timer=setTimeout('shiftMe("'+object+'","'+direction+'",'+pixels+')',1);}-->

<div id="main"><a href="google.com" id="h2" onmouseover="shiftMe('h2','r',7)" onmouseout="shiftMe('h2','l',7)">Google</a><br><a href="google.com" id="h3" onmouseover="shiftMe('h3','r',7)" onmouseout="shiftMe('h3','l',7)">Google</a><br></div>

FISH-EYE EFFECT NAVIGATION

<!--// author: prateek// description: This navigation/text effect quickly but gradually changes the font size of the text as it is mouseovered// necessary mods: nonefunction changeTextSize(object,amount,direction){	if (amount==0)return;	var obj = document.getElementById(object);	if(obj.style.fontSize=="")obj.style.fontSize="16px";	var fontSizeVal=obj.style.fontSize;	var fontSizeInt=new Array();	fontSizeInt=fontSizeVal.split('p');	if (direction=="m")	fontSizeInt[0]++;	if (direction=="l")	fontSizeInt[0]--;	amount--;	obj.style.fontSize=fontSizeInt[0]+"px";	timer=setTimeout('changeTextSize("'+object+'",'+amount+',"'+direction+'")',1);}-->

<div id="main"><a href="google.com" id="h7" onmouseover="changeTextSize('h7',13,'m')" onmouseout="changeTextSize('h7',13,'l')">Google</a><br><a href="google.com" id="h8" onmouseover="changeTextSize('h8',13,'m')" onmouseout="changeTextSize('h8',13,'l')">Google</a><br></div>

See all these scripts in effect here!

Edited by Skemcin
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...
  • 2 months later...
Guest
This topic is now closed to further replies.
×
×
  • Create New...