JavaScript Reload JS file three ways

Source: Internet
Author: User
Tags button type getscript

Example 1. Reload JS file

The code is as follows Copy Code

function Loadjs (file) {
var head = $ ("head"). Remove ("script[role= ' reload ')");
$ ("<scri" + "pt>" + "</SCR" + "ipt>"). attr ({role: ' Reload ', Src:file, type: ' Text/javascript '}). Appendto (He AD);
}

Example 2. The way to reload the JavaScript file (set an ID for JS) and encapsulate it in a way that is easy for everyone to use:

The code is as follows Copy Code

function Reloadablejsfn (ID,NEWJS)
{
var oldjs = null;
var t = null;
var Oldjs = document.getElementById (ID);
if (Oldjs) oldjs.parentNode.removeChild (OLDJS);
var scriptobj = document.createelement ("script");
SCRIPTOBJ.SRC = Newjs;
Scriptobj.type = "Text/javascript";
Scriptobj.id = ID;
document.getElementsByTagName ("Head") [0].appendchild (Scriptobj);
}

Example 3

jquery is directly using the getscript on it.

  code is as follows copy code

<script Type= "Text/javascript" src= ". /jquery.js "></script>
<script type=" Text/javascript ">
$ (function ()
{
$ (' #loadButton ') . Click (function () {
$.getscript (' New.js ', function () {
Newfun (' Checking new script '); This function is inside the new.js, and when clicked Click Click to run this function
});
});
});
</script>
<body>
<button type= "button" id= "Loadbutton" >load</ Button>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.