DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Ultimate Guide to FaceIO
  • Understanding Git
  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Exploring Shadow DOM With Examples Using Cypress

Trending

  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Leveraging FastAPI for Building Secure and High-Performance Banking APIs
  • The Ultimate Guide to API vs. SDK: What’s the Difference and How To Use Them
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  1. DZone
  2. Coding
  3. Languages
  4. HTML5 Blob Objects Made Easier

HTML5 Blob Objects Made Easier

Let us simplify HTML5 blob objects for you.

Gil Fink user avatar by
Gil Fink
·
Sep. 05, 12 · Analysis
Like (1)
Save
Tweet
Share
21.10K Views

Join the DZone community and get the full member experience.

Join For Free

Lately, while getting ready for a HTML5 session, I found out that the File API specifications changed a little bit. In the past, in order to create a runtime Blob object you used the BlobBuilder object. That object is now deprecated and you should stop using it. Instead you are going to use the new blob object constructor which decrease the amount of code and objects you use.

Blob Object Constructor

The Blob constructor gets two parameters:

  • Array of data that will be appended to the blob.
  • Property bag that enable to configure the blob. You can use two properties: type and endings.
    The type property indicate the content type of the Blob. The endings property gets two values: transparent and native. Those values indicate how strings containing \n are written (in native they are written to match host OS file system conventions). The transparent value is the default.

Here is an example of using the new constructor:

var blob = new Blob(['alert("hello")'], {type: 'text/javascript'});

In this code example, a blob object is created and is holding some JavaScript code.

Full Example – Create a Runtime JavaScript Script Tag Using a Blob

Here is a simple example to create a JavaScript script tag and attach it to the document’s body using the Blob API:

<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8" />

    <title>Blob</title>

    <script type="text/javascript">

        (function () {

            window.URL = window.URL || window.webkitURL;



            function contentLoaded() {

                var blob = new Blob(['alert("hello")'], { type: 'text/javascript' });



                var script = document.createElement('script');

                script.setAttribute('src', window.URL.createObjectURL(blob));

                document.body.appendChild(script);

            }



            window.addEventListener('DOMContentLoaded', contentLoaded, false);

        } ());

    </script>

</head>

<body>

    <div id="container">

    </div>

</body>

</html>

Running the code will produce an alert with the hello string.

Summary

The HTML5 specifications change from time to time and those changes can break your code. In this post, I wrote about a positive change in the specification regarding the use of blobs.

HTML Object (computer science)

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Ultimate Guide to FaceIO
  • Understanding Git
  • Creating a Deep vs. Shallow Copy of an Object in Java
  • Exploring Shadow DOM With Examples Using Cypress

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: