Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~

Javascript Snippets

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>


<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>


<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>


<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>


<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>


<!--// 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-->


<!--// 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>


<!--// 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

Share this post

Link to post
Share on other sites


Share this post

Link to post
Share on other sites

SHOW/HIDE script added.

Share this post

Link to post
Share on other sites

SLIDING NAVIGATION script added.by Prateek

Share this post

Link to post
Share on other sites

FISH-EYE EFFECT NAVIGATION script added.by Prateek

Share this post

Link to post
Share on other sites
This topic is now closed to further replies.