google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Use DOM to Insert External JavaScript files In this JavaScript article tutorial, jsB@nk is happy to show you another JavaScript solution to include external JavaScript files in your web pages. This JavaScript method uses DOM (Document Object Model) to process. Please go to the inner post-page for full completed instructions and JavaScript source codes for samples, along with many reality problems.

Try more other related JavaScript solutions for inserting external JavaScript files:
- Simple Tips and Code to Load External JavaScript, CSS Files Dynamically
- Loading External CSS & JavaScript Files Faster With PHP mod_rewrite
- How to use an external JavaScript
- The best way to load external JavaScript


Label: DOM, external JavaScript file, Document Object Model, JavaScript solution

Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

This page shows you how to use javascript (js) to insert a html that contains js code and have that js code run. If you don't know how to insert a element using js, see: Adding HTML Content with Javascript.

The Problem

There are lots js based widgets today. For example, web share widget, Google search widget, Comment widget, twitter, facebook, amazon ads... etc. They let you easily add functionality to your site.

If your webpage is static html, or generated by php or content manager software, you can directly include these widgets at the right place at the server side. However, sometimes it is convenient to use a js to insert them. Because, you may want the location of the widget to be different for different pages. Or, only centain pages you want to include the widget, but you don't know which until the page is generated.

So, the solution is to use a client side js that insert these js widgets.

If you use a javascript to insert a js code xyz, will the newly inserted xyz script run?

It depends on how you construct the element to be inserted. If the 「<script...>...</script>」 is constructed using 「innerHTML = ...」, then it won't work. But if it is constructed as a DOM element, then it'll work.

Using innerHTML Does Not Work

First, let's see what happens if you don't do anything special.

Suppose you want to use js to insert the following js code.

<div class="xyz">
 <p>Fantastic!</p>
 <script type="text/javascript">alert("woot!");</script>

</div>

Suppose you do it this way:

var myElement = document.createElement("div"); // create new div element

myElement.setAttribute("class", "xyz"); // add a class attribute


// add html in the div element
myElement.innerHTML='<p>Fantastic!<\/p> <script type="text/javascript">alert("woot!");<\/script>';

// add the new element after the first p tag
document.body.insertBefore(myElement, (document.getElementsByTagName("p"))[0]);

It will fail in IE8, Google Chrome, Opera. (but works in Firefox 3.6.12).

You can see what your browser do here: insert_js.html. For a version where each js file is on its own page, see: js_insert_js.html.

Creating 「<script>」 Object by createElement

Here's how to make it work. It's easy. Instead of using 「myElement.innerHTML="<script>...</script>";」, you should create the �script� element as a DOM object, then you insert this object.

Here's a sample js code:

var ele = document.createElement("script");
ele.type = "text/javascript";
ele.innerHTML='alert("woot!");'

document.body.insertBefore(ele, (document.getElementsByTagName("p"))[0]);

Here's a test page using this code: js_insert_js_works.html.

Twitter Example

Suppose you want to insert this code:

<div class="twtr">
<a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="xah_lee">Tweet</a>

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</div>

It's a widget that shows a Twitter button with number of tweets of the page.

Here's how you do it in your js:

// insert a twitter div tag before element �cg�
function insertTwitter (cg) {
    var mdv = document.createElement("div");
    mdv.setAttribute("class", "twt");

    mdv.innerHTML='<a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-via="xah_lee">Tweet</a>';

    var ele = document.createElement("script");
    ele.type = "text/javascript";
    ele.src = "http://platform.twitter.com/widgets.js";

    mdv.appendChild(ele);

    document.body.insertBefore(mdv, cg);

}


// insert before the first p tag
insertTwitter((document.getElementsByTagName("p"))[0]);

Thanks to �idealmachine� and DVK. stackoverflow.com.

AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web