Never been to DZone Snippets 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

« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS 

Append to Article Body

Here's the code to insert some bit of text to the end of your article body. For the Textpattern plugin.

var obj = document.getElementById('body');
obj.value = obj.value+"SOME TEXT";

Set a Field Equal to Something

A basic code sample for tcm_write_macros. It sets a custom field to a value
/* also - custom-1, custom-2, custom-3, custom-4, custom-5, etc for custom fields. */
var obj = document.getElementById('custom-3');
obj.value = "VALUE";

Encoding of HTML/XML special chars

function encode(string) {
	return string.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;').replace('\'','&apos;').replace('"','&quot;');
}

Javascript - Sleep

// Effettuare uno sleep dello script di uno script

function pause(millisecondi)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

Events

I like being able to attach 'events' to buttons/links like this... don't think prototype can do this

$("a.codeButtonB").click(function(){$("pre.codeB").toggle()});

jQuery Example

$("#item li, #otheritem li").click(function(){
  $(this).css('color', '#c00');
});

switching css style sheets with javascript

// description of your code here

//this part goes within the head tags
<link rel="stylesheet" type="text/css" href="standard.css" title="standard" />
<link rel="alternate stylesheet" type="text/css" href="medium.css" title="medium" />
<link rel="alternate stylesheet" type="text/css" href="large.css" title="large" />

<script src="fontsize.js" type="text/javascript" language="javascript"></script>

//this part goes where you want the links that the user will click on to show up (the script part should probably be on one line with no breaks

<td valign="top" id="adjustcell">
<script type="text/javascript">
if (document.getElementById){ document.getElementById('adjustcell').innerHTML = '<table width="100" cellspacing="0" cellpadding="0" border="0"><tr>

<td colspan="4" height="10">&nbsp;</td></tr><tr><td width="25">&nbsp;</td><td width="25"><a href="#" onclick="setActiveStyleSheet(\'default\'); return false;">normal text size</a></td>

<td width="25"><a href="#" onclick="setActiveStyleSheet(\'medium\'); return false;">medium text size</a></td>

<td width="25"><a href="#" onclick="setActiveStyleSheet(\'large\'); return false;">large text size</a></td>

</tr>
<tr><td width="100" align="right" colspan="4" style="color: #ffffff;">Adjust Text Size</td></tr></table>';}
</script>
</td>

//contents of fontsizeswitcher.js (some of this is to switch images if you are using images instead of text links.

// var graphics_path = "" ;
var graphics_init = 0 ;

function init_path()
	{
	graphics_path = document.getElementById("default").src ; // "graphics" ;

graphics_path = graphics_path.substring(0,graphics_path.lastIndexOf("/"));
graphics_path = graphics_path.substring(graphics_path.indexOf(".com/")+4);

	graphics_init = 1 ;
	}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) {
      	a.disabled = false;
	  }
    }
  }
  pressButton(title);
}

function unpressButton(which) {
	if ( graphics_init == 0 )
		{ init_path() }
	document.getElementById(which).src = graphics_path + "/" + which + ".gif";
}

switchButtons = ["default", "medium", "large"];

function pressButton(which) {
	if ( graphics_init == 0 )
		{ init_path() }
	for (i = 0; i < switchButtons.length; i++) {
		if (which == switchButtons[i]) {
			document.getElementById(which).src = graphics_path + "/" + which + "_p.gif";
		} else {
			document.getElementById(switchButtons[i]).src = graphics_path + "/" + switchButtons[i] + ".gif";
		}
	}
	if (which == "null") {
		document.getElementById("default").src = graphics_path + "/default_p.gif";
	}
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
    	return a.getAttribute("title");
    }
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else {
  	expires = "";
  }
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  if ( graphics_init == 0 )
    { init_path() }
  smallpressed = new Image();
  smallpressed.src=graphics_path + "/default_p.gif"
  medpressed = new Image();
  medpressed.src=graphics_path + "/medium_p.gif"
  largepressed = new Image();
  largepressed.src=graphics_path + "/large_p.gif"
  if (document.getElementById) {
    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
    }
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

JavaScript Object Viewer

/*
appName: JSOV 0.1
Author: Billy Chow
Webpage: http://www.dreammx.com
E-mail: billychow#tom.com
*/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSOV - JavaScript Object Viewer</title>
<meta name="robots" content="follow,index" />
</head>
<style type="text/css">
body  {font-family: Verdana, Tahoma, Arial;
     font-size: 12px;
    }
div    {display: inline;}
a,a:link,a:hover  {text-decoration: none;
           color:blue;
}
.o_prop_name  {font-weight: bold;
         color: blue;
         font-size: 14px;
}
.o_prop_value  {font-weight: bold;
         color: green;
}
</style>

<script language="javascript" type="text/javascript">
/*
  appName: JSOV 0.1
  Author: Billy Chow
  Webpage: http://www.dreammx.com
  E-mail: billychow#tom.com
*/
function showObj(obj) {
  var obj= eval(obj);
  var str='<div class="formated">';
  for (prop in obj) {
    str +='<div class="o_prop_name"><a href="#" title="next" onClick="changeObj(this.innerText)">' +prop+ '</a></div>=<div class="o_prop_value">' +obj[prop]+ '</div><br />';
  }
  str += '</div>';
  document.all.txt3.innerHTML=str;
}
function changeObj(txt) {
  var prefix = document.all.txtObj.value;
  document.all.txtObj.value = prefix+'.'+txt;
  showObj(document.all.txtObj.value);
}
function eraser() {
  var tmpstr = document.all.txtObj.value;
  tmpstr=tmpstr.replace(/\.\w*$/ig,"");
  document.all.txtObj.value=tmpstr;
  showObj(tmpstr);
}
</script>

<body>
<h1>JavaScript Object Viewer 0.1</h1>
<div id="txt1">
Input your object name here: <input type="text" name="txtObj" value="window" />&nbsp;&nbsp;<input type="button" name="btnSubmit" value="View" onClick="showObj(document.all.txtObj.value);" />&nbsp;&nbsp;<input type="button" name="btnBack" value="Back" onClick="eraser();" />&nbsp;&nbsp;<input type="button" name="btnDisp" value="DispVal" onClick="alert(eval(document.all.txtObj.value));" />
</form>
</div>
<br />
<div id="txt2">The Object has these attributes:</div>
<hr noshade="noshade" />
<div id="txt3">
</div>
</body>
</html>

防止框架(跳出框架页)

// description of your code here

<SCRIPT LANGUAGE=JAVASCRIPT> 
<!-- 
if (top.location !== self.location) { 
top.location=self.location; 
} 
</SCRIPT>

�键�弊

// description of your code here

function KeyDown(){ 
//�蔽鼠标�键�Ctrl+n�shift+F10�F5刷新�退格键
//alert("ASCII代�是:"+event.keyCode);
  if ((window.event.altKey)&&
      ((window.event.keyCode==37)||   //�蔽 Alt+ 方�键 �
       (window.event.keyCode==39))){  //�蔽 Alt+ 方�键 →
     alert("�准你使用ALT+方�键�进或�退网页�");
     event.returnValue=false;
     }
  if ((event.keyCode==8)  ||                 //�蔽退格删除键
      (event.keyCode==116)||                 //�蔽 F5 刷新键
      (event.keyCode==112)||                 //�蔽 F1 刷新键
      (event.ctrlKey && event.keyCode==82)){ //Ctrl + R
     event.keyCode=0;
     event.returnValue=false;
     }
  if ((event.ctrlKey)&&(event.keyCode==78))   //�蔽 Ctrl+n
     event.returnValue=false;
  if ((event.shiftKey)&&(event.keyCode==121)) //�蔽 shift+F10
     event.returnValue=false;
  if (window.event.srcElement.tagName == "A" && window.event.shiftKey) 
      window.event.returnValue = false;  //�蔽 shift 加鼠标左键新开一网页
  if ((window.event.altKey)&&(window.event.keyCode==115)){ //�蔽Alt+F4
      window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");
      return false;}
}
« Newer Snippets
Older Snippets »
Showing 1-10 of 11 total  RSS