cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="wwdemo demo-body">
	<div class="demo-container">

<!-- 		<h1>Force a File to Download with Javascript</h1> -->

		<a class="js-download-link button" href="https://wattswork.s3.amazonaws.com/test-media/video/test_Drop-1280x720-16-9-HD.mp4">Download MP4 File</a>
		
		<a class="js-download-link button" href="https://wattswork.s3.amazonaws.com/test-media/PDFs/test-file.pdf">Download PDF File</a>
		
		<a class="js-download-link button" href="https://wattswork.s3.amazonaws.com/test-media/Text/test-file.txt">Download TXT File</a>
		
		<div class="demo-description">
			<p></p>
			<ul>
				<li>
					Downloads files without altering file headers.</li>
				</li>
				<li>Does not work on iOS devices.</li>
				<li>This method does not allow for renaming of files.</li>
			</ul>
		</div>
		
	</div>
</div>
            
          
!
            
              body
{
	padding: 0;
	margin: 0;
}

.wwdemo,
.wwdemo a
{
	font-family: Arial;
	color: #585858;
}

.wwdemo .demo-container
{
	background-color: #FFFFFF;
    width: 88%;
    margin: 2% auto;
    max-width: 40em;
    padding: 1.5% 3% 3%;
}

.wwdemo h1
{
	text-align: center;
	word-spacing: -0.1em;
   letter-spacing: -0.02em;
}

.wwdemo a.button
{
	-moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
	-webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
	box-shadow:inset 0px 1px 0px 0px #ffffff;
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #ffffff), color-stop(1, #f6f6f6));
	background:-moz-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:-webkit-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:-o-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:-ms-linear-gradient(top, #ffffff 5%, #f6f6f6 100%);
	background:linear-gradient(to bottom, #ffffff 5%, #f6f6f6 100%);
	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#f6f6f6',GradientType=0);
	background-color:#ffffff;
	-moz-border-radius:6px;
	-webkit-border-radius:6px;
	border-radius:0.22em;
	border:1px solid #dcdcdc;
	display:inline-block;
	cursor:pointer;
	color:#666666;
	font-family:Arial;
	font-size:15px;
	font-weight:bold;
	padding:19px 46px;
	text-decoration:none;
	text-shadow:0px 1px 0px #ffffff;
	margin: 2em auto;
	display: block;
	max-width: 12em;
	text-align: center;
}

.wwdemo a.button:hover
{
	background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #f6f6f6), color-stop(1, #ffffff));
	background:-moz-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:-webkit-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:-o-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:-ms-linear-gradient(top, #f6f6f6 5%, #ffffff 100%);
	background:linear-gradient(to bottom, #f6f6f6 5%, #ffffff 100%);
	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#f6f6f6', endColorstr='#ffffff',GradientType=0);
	background-color:#f6f6f6;
}

.wwdemo a.button:active
{
	position:relative;
	top:1px;
}

.demo-description
{
	font-size: 1.1em;
	line-height: 1.4em;
	margin: 2em 0;
}
            
          
!
            
              // jQuery module pattern enclosure.
var fileDownload = (function(){
	
	var classname = document.getElementsByClassName("js-download-link");

	function init()
	{
		for (var i = 0; i < classname.length; i++) {
			 classname[i].addEventListener('click', function( e ){
				e.preventDefault(); // Stops the normal element action.
				var attribute = this.getAttribute("href");
				window.forceFileDL( attribute );
			 }, false);
		}
	}

	window.forceFileDL = function( sUrl )
	{
		//iOS devices do not support downloading. We have to inform user about this.
		if (/(iP)/g.test(navigator.userAgent))
		{
			//alert('Your device does not support files downloading. Please try again in a desktop browser.');
			window.open(sUrl, '_blank');
			return false;
		}

		//If in Chrome or Safari - download via virtual link click
		if (window.forceFileDL.isChrome || window.forceFileDL.isSafari)
		{
			//Creating new link node.
			var link = document.createElement('a');
			link.href = sUrl;
			link.setAttribute('target','_blank');

			if (link.download !== undefined)
			{
				//Set HTML5 download attribute. This will prevent file from opening if supported.
				link.download = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
			}

			 //Dispatching click event.
			if (document.createEvent)
			{
				var e = document.createEvent('MouseEvents');
				e.initEvent('click', true, true);
				link.dispatchEvent(e);
				return true;
			}
		}

		// Force file download (if supported by server).
		if (sUrl.indexOf('?') === -1)
		{
			sUrl += '?download';
		}

		window.open(sUrl, '_blank');
		return true;
	}

   window.forceFileDL.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
   window.forceFileDL.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;

   return{
		init: init
	}
  
}());

fileDownload.init();



            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console