html - add javascript to page or external javascript file -
possible duplicate:
when should use inline vs. external javascript?
i have javascript variables use in javascript (ie. external javascript files)
this javascript variables page specific , varies per page , user.
is better have these in other javascript file meaning http request or including within html content of page, bloating page.
i separation want maximize performance, either increasing html page size or http request.
i ask question in terms of performance/optimization.
opinions on please?
you couple different ways. 1 way include data in object included main script , have object per page. like
var data = { "page1": { "title": "blah" }, "page2": { "title": "different blah", "users": [...] } } alternatively config loader. in each html page include
<script src="loader.js" type="text/javascript"></script> which contain this
// if page blog.html page equal blog var page = window.location.href.match(/\/(\w+).html/)[1]; var s = document.createelement("script"); s.src = "/path/to/javascript/" + page + ".js"; s.type = "text/javascript"; document.getelementsbytagname("head")[0].appendchild(s); this load additional data specific page needs , if done in beginning work along side libraries equivalent of onready like
jquery(function() { dostuff(); }) for method work need ensure data in each file uniform in data contains.
var pagedata = { ... }; these 2 methods should give bit think in coming solution.
Comments
Post a Comment