2

I have a web page index.php, I want to add and remove JS files to this page dynamically

So far I did,

<script type="text/javascript" src="" id="dynamic_files"></script>

I had planned to change the src of this script tag by

function loadJsfiles( filename ){ 
    var filePath = 'include/js/'+filename;
    $("script#dynamic_files").attr('src',filePath);
};

and the included js file has script tags in it:

document.write('<script type="text/javascript" src="include/js/profile1.js"></script>');
document.write('<script type="text/javascript" src="include/js/profile2.js"></script>');
document.write('<script type="text/javascript" src="include/js/profile3.js"></script>');
document.write('<script type="text/javascript" src="include/js/profile4.js"></script>'); –

My question is that,

will that script tag printed in to my document page, if so how do I remove it

Reference: Dynamic js files

11
  • Why not just try it yourself? Commented Jun 9, 2012 at 4:57
  • and the included js file has script tags in it, Well, it shouldn't have <script> tags in an included script file. Leave 'em out. Commented Jun 9, 2012 at 4:57
  • @Jared Farrish my planning is to add js files for some events, ie: profile view, edit view, etc and the js files will have to print perticular files in to my webpage Commented Jun 9, 2012 at 5:01
  • @xdazz i tried but the src looks same by viewing the source Commented Jun 9, 2012 at 5:03
  • Within your script.js files to be included, do you have <script> tags in them like markup? Commented Jun 9, 2012 at 5:09

3 Answers 3

1

You CANNOT do document.write after load

Look here:

Javascript: Run "document.write" in createElement script failed

You will notice your page will be wiped.

It does not matter which method you use to insert scripts, you need to either stop them from using document.write OR replace the document.write with a custom method.

Perhaps you want to use something like require.js - which is a javascript loader (thanks Jared for a better link than https://developers.google.com/loader/ )

Sign up to request clarification or add additional context in comments.

5 Comments

There's also Require.JS, which works with jQuery.
jQuery EasyUI might pair well with Require.JS and provide a more advanced system to work within than what's being proposed. It appears to be simpler to use than ExtJS, but pretty powerful.
@JaredFarrish mplungjan i'm using bennadal's corMVC as my platform, what's your opinion about that??
@user1400191 - It's site looks a little sparse (is it new?). I don't know anything about it. I like Backbone.js as a JS MVC system. Knockout is another very interesting entry, an MVVC.
@user1400191 - Here's an overview of Asynchronous Module Definition (AMD) with Knockout and RequireJS. Interesting article.
0

The better example is google analytics

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

2 Comments

that link does the same as the code in the site posted by the OP. It is the document.write in the OPs OTHER js file that is the problem
@Alejo JM Thanks for your effort
0

This is what I do:

var dynScript,dynParent;

function createjsfile(filename) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename + "?noxhr="+(new Date()).getTime());
    return fileref;
}

function loadjsfile(filename){
    dynScript = createjsfile(filename);
    dynParent = document.getElementsByTagName("head")[0];
    dynParent.appendChild(dynScript);
}

function replacejsfile(filename) {
    var newelement = createjsfile(filename);
    dynParent.replaceChild(newelement, dynScript);
    dynScript = newelement;
}

On body.load event I do loadjsfile("script.js")

and when I want to reload it replacejsfile("script.js")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.