JavaScriptSource https://javascriptsource.com Search 5,000+ Free JavaScript Snippets Wed, 29 Sep 2021 22:17:33 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://javascriptsource.com/wp-content/uploads/2021/07/cropped-favicon-32x32.png JavaScriptSource https://javascriptsource.com 32 32 Remove HTML Tags https://javascriptsource.com/remove-html-tags/ Sat, 13 Jan 2018 19:16:00 +0000 https://the-js-source.flywheelsites.com/?p=274 General

This function will remove all HTML tags from a string.

Notes

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: removeHTMLTags.js

/* This script and many more are available free online at
The JavaScript Source!! http://javascriptsource.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(){
 	if(document.getElementById && document.getElementById(“input-code”)){
 		var strInputCode = document.getElementById(“input-code”).innerHTML;
 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == “lt”)? “<” : “>”;
 		});
 		var strTagStrippedText = strInputCode.replace(/</?[^>]+(>|$)/g, “”);
 		alert(“Output text:n” + strTagStrippedText);	
   // Use the alert below if you want to show the input and the output text
   //		alert(“Input code:n” + strInputCode + “nnOutput text:n” + strTagStrippedText);	
 	}	
}

Head

Paste this code into the HEAD section of your HTML document.

<script type=”text/javascript” src=”removeHTMLTags.js”></script>

Body

Paste this code into the BODY section of your HTML document.

<p><strong>Input code:</strong> (<em>Note: Processing is done on the “input-code” id</em>)</p>
<pre>
<code id=”input-code”>
<h2>About</h2>
<p>Here you will find posts about news, trends and developing for internet, 
	mainly focusing on browsers and web user interfaces.</p>
<p><a href=”/about”>Read more</a></p>
</code>
</pre>

<a href=”#” onclick=”removeHTMLTags(); return false;”>» Click to remove all HTML tags from the input code</a>.
]]>
Window Print Method https://javascriptsource.com/window-print-method/ Sat, 13 Jan 2018 17:51:00 +0000 https://the-js-source.flywheelsites.com/?p=248 Add a button to allow your visitors to print your Web page. Compact and simple, yet highly requested.

<!– Paste this code into the STYLE section of your HTML document
     so the button itself will not print  –>

@media print {
input.noPrint { display: none; }
}
<!– Paste this code into the BODY section of your HTML document  –>

<input class=”noPrint” type=”button” value=”Print This Page” onclick=”window.print()”>
]]>
getElementsByAttribute https://javascriptsource.com/getelementsbyattribute/ Sat, 13 Jan 2018 17:42:00 +0000 https://the-js-source.flywheelsites.com/?p=244 Ever run into a situation where you want to get an array of all elements with a specific attribute? Or even want elements with a certain value for that chosen attribute as well? Just add this code snippet to your script and your problem will be solved.

<!– Paste this snippet into your existing code  –>


/* This script and many more are available free online at
   The JavaScript Source :: http://javascriptsource.com 
   Copyright Robert Nyman, http://www.robertnyman.com
   Free to use if this text is included */

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
    var arrElements = (strTagName == “*” && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != “undefined”)? new RegExp(“(^|\s)” + strAttributeValue + “(\s|$)”) : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == “string” && oAttribute.length > 0){
            if(typeof strAttributeValue == “undefined” || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}
]]>
getXML https://javascriptsource.com/getxml/ Sat, 13 Jan 2018 16:34:00 +0000 https://the-js-source.flywheelsites.com/?p=224 Obtain the contents of an XML element and convert it to text. This script is great for using with Ajax-type applications, when it’s necessary to send back the contents of an element to the server without knowing what’s there.

/* This script and many more are available free online at
The JavaScript Source :: http://javascriptsource.com
Created by: Neal Venditto :: http://neal.venditto.org/ */

function getXML(aNode) {
  var out = ”;
  if(aNode.nodeType == Node.ELEMENT_NODE) {
    out = ‘<‘+aNode.nodeName;
    if(aNode.hasAttributes()) {
      var atts = aNode.attributes;
      for(var x=0;x<atts.length;x++) {
        out +=’ ‘+atts[x].nodeName+’=”‘+atts[x].nodeValue+'”‘;
      }
    }
    
    if(aNode.hasChildNodes()) {
      out += ‘>’;
      var kids = aNode.childNodes;
      for(var x=0;x<kids.length;x++) {
        switch(kids[x].nodeType) {
          case Node.ELEMENT_NODE:
            out += getXML(kids[x]);
            break;
          case Node.TEXT_NODE:
            out += kids[x].nodeValue;
            break;
          case Node.COMMENT_NODE:
            out += ‘<!–‘+kids[x].nodeValue+’–>’;
            break;
          case Node.CDATA_SECTION_NODE:
            out += ‘<‘+’![CDATA[‘+kids[x].nodeValue+’]’+’]>’;
            break;  
        }
      }
      out += ‘</’+aNode.nodeName+’>’;
    } else { 
      out += ‘/>’;
    }
  }  
  return out;
}
]]>
insertAfter() https://javascriptsource.com/insertafter/ Fri, 12 Jan 2018 22:08:00 +0000 https://the-js-source.flywheelsites.com/?p=179 Use this snippet to insert a node after another node. Quite handy in laying out Web pages.

<!– Add this snippet to your existing JavaScripts  –>

/* This script and many more are available free online at
The JavaScript Source :: http://javascriptsource.com
Created by: Public Domain */

function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}
]]>
Multiple onLoad 2 https://javascriptsource.com/multiple-onload-2/ Fri, 12 Jan 2018 22:02:00 +0000 https://the-js-source.flywheelsites.com/?p=175 General

Need to load several functions after the page loads? Use this snippet to call several functions using the onLoad event handler. Easy to use.

Notes

See the notes at: “Executing JavaScript on Page Load

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: multipleOnload.js and load it last. Or, if you use one file for several scripts, you can add it to the bottom of page.

/* This script and many more are available free online at
The JavaScript Source!! http://javascriptsource.com
Created by: Simon Willison | http://simon.incutio.com/ */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != ‘function’) {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  functionHere();
});

Head

Paste this code into the HEAD section of your HTML document.

<script type=”text/javascript” src=”multipleOnload.js”></script>
]]>