/    Sign up×
Community /Pin to ProfileBookmark

dynamically load javascript file

Hi guys,

I’m using google map on my site and it takes time to load as the js file is pretty big. Is there anywhere to load the js file dynamically like if I click on a link on the page, it will then load the js files instead of loading the js files when the page first load.

Thanks & Regards,
Mark

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@kmthienauthorJul 09.2007 — Hi,

I also find sth like this:

http://giantrobot.co.nz/gr/2006/08/25/72/

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

s.type = 'text/javascript';

s.src = '/scripts/library.js';

document.getElementsByTagName('head')[0].appendChild(s);

There is a small problem here. How can we check if the js files has already been loaded before? hence, before we load the js file dynamically, we can check if it's already been loaded previously.

Thanks & Regards,

Mark
Copy linkTweet thisAlerts:
@felgallJul 09.2007 — You don't need to check if it has been loaded previously as that code adds the tag to reference the code into the page and will either grab the content from the cache or download it from the server as required.

The only thing you might need to check is if it has already been added to the page but that can be easily tracked by placing the code inside an if statement and setting a variable when it is added that you test before adding it.
Copy linkTweet thisAlerts:
@kmthienauthorJul 09.2007 — how about loading css file dynamically? any clue?
Copy linkTweet thisAlerts:
@LeeUJul 09.2007 — how about loading css file dynamically? any clue?[/QUOTE]
Try this.
Copy linkTweet thisAlerts:
@kmthienauthorJul 09.2007 — last question, how to add multiple js and css files?
Copy linkTweet thisAlerts:
@kmthienauthorJul 09.2007 — Try this.[/QUOTE]

I do not need a tabber dude !
×

Success!

Help @kmthien spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...