google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Cách tải tệp tin JavaScript, CSS linh động hơn Bài viết này sẽ hướng dẫn bạn cách để xây dựng một hàm JavaScript đơn giản để tải các tệp tin JavaScript và CSS linh động hơn. Tuy không có gì mới nhưng vẫn là một trong nhiều cách để làm website trở nên có thể tùy biến cao.


Nhãn: tải tệp tin JavaScript, tệp tin CSS, hàm JavaScript, linh động, tùy biến cao

Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

Generally we includes an external css ( Cascading Style Sheets ) and js ( javascript ) files in the HEAD section of our HTML page. We include them by following conventional method -

1<link rel="stylesheet" type="text/css" href="stylesheet/mycss.css"/>
2<script language="javascript" src="javascript/myjs.js" type="text/javascript"></script>

This method added files to the page as they encountered in page source code or synchronously.

Here we will learn how to dynamically load an external file in simple steps.

1. For loading .js or .css file dynamically we use DOM methods to first create new script or link element. Assign it to appropriate attributes and then finally use appendChild() function to include it into head section on document tree.

Example :

01function includejscssfiles(filename, filetype)
02{
03    if (filetype=="js") //if filename is an JavaScript file
04    {
05        var fileref=document.createElement('script')
06        fileref.setAttribute("type","text/javascript")
07        fileref.setAttribute("src", filename)
08    }
09    else if (filetype=="css") //if filename is an CSS file
10    {
11        var fileref=document.createElement("link")
12        fileref.setAttribute("rel", "stylesheet")
13        fileref.setAttribute("type", "text/css")
14        fileref.setAttribute("href", filename)
15    }
16    if (typeof fileref!="undefined")
17    document.getElementsByTagName("head")[0].appendChild(fileref)
18}

2. Now we will create one more function that contains file name and file type. In this function we will call includejscssfiles() by passing file name and file type as its parameters.

1function loadfiles()
2{
3    includejscssfiles('mycss.css', 'css');
4    includejscssfiles('myjs.js', 'js');
5}

3. Call loadfiles function on onload on tag. Thats it.

function parameter filetype lets you tell the script what file type to expect before loading. After that, the function sets out to create the element using the appropriate DOM methods, assign it the proper attributes, and finally, add it to the end of the HEAD section.

By referencing the HEAD element of the page first and then calling appendChild(), this means the newly created element is added to the very end of the HEAD tag.

Hope this tutorial will help you.

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

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

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

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

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web