Opened 16 years ago
Closed 14 years ago
#7775 closed enhancement (wontfix)
Method to add script tag with in-file javascript code
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | normal | Milestone: | |
Component: | web frontend | Version: | none |
Severity: | normal | Keywords: | needpatch |
Cc: | Branch: | ||
Release Notes: | |||
API Changes: | |||
Internal Changes: |
Description
While it is possible to add script
tags for external javascript file using the add_script
function in trac.web.chrome
, it isn't possible to add script
tags with in-file javascript code content.
While using only external javascript files is a good web design approach, some plug-ins might need to add small dynamic generated javascript code into the generated XHTML, e.g. to set variables which value depends on trac.ini
settings or external sources.
I had this problem recently with the GoogleMapMacro.
Because script
tags shoudn't refer to an external file and have code content, I would like to suggest a new python method and template tag:
-
trac/templates/layout.html
diff -Naur Trac-0.11.1-py2.5.egg/trac/templates/layout.html Trac-0.11.1-py2.5.egg.new/trac/templates/layout.html
old new 22 22 </py:if> 23 23 <script py:for="script in chrome.scripts" 24 24 type="${script.type}" src="${script.href}"></script> 25 <script py:for="script in chrome.scriptscontent" type="${script.type}"> 26 /*<![CDATA[*/ 27 ${script.content} 28 /*]]>*/ 29 </script> 25 30 ${Markup('<!--[if lt IE 7]>')} 26 31 <script type="text/javascript" src="${chrome.htdocs_location}js/ie_pre7_hacks.js"></script> 27 32 ${Markup('<![endif]-->')} -
Trac-0.11.1-py2.5.egg
diff -Naur Trac-0.11.1-py2.5.egg/trac/web/chrome.py Trac-0.11.1-py2.5.egg.new/trac/web/chrome.py
old new 106 106 req.chrome.setdefault('scripts', []).append(script) 107 107 scriptset.add(filename) 108 108 109 def add_script_content(req, content, mimetype='text/javascript'): 110 """Add in-file javascript code to the template. 111 112 """ 113 import hashlib 114 m = hashlib.md5() 115 m.update(content) 116 digest = m.hexdigest() 117 118 scriptcontentset = req.chrome.setdefault('scriptcontentset', set()) 119 if digest in scriptcontentset: 120 return False # Already added that code 121 122 scriptcontent = {'content': content, 'type': mimetype} 123 124 req.chrome.setdefault('scriptscontent', []).append(scriptcontent) 125 scriptcontentset.add(digest) 126 109 127 def add_javascript(req, filename): 110 128 """Deprecated: use `add_script()` instead.""" 111 129 add_script(req, filename, mimetype='text/javascript')
Attachments (1)
Change History (12)
follow-up: 3 comment:1 by , 16 years ago
comment:3 by , 16 years ago
Replying to cboos:
e.g. to set variables which value depends on trac.ini settings or external sources
Couldn't that be done by sending a request?
Yes it could be handled using a request, but this would add an extra unneeded step. It would be a little silly if a script tag can be added but not inline code which is also valid XHTML.
follow-up: 5 comment:4 by , 15 years ago
Milestone: | → 0.13 |
---|
Ok, admitting we allow adding inline js code like you suggested, isn't md5 computation a bit overkill? Simply adding the code itself in a set would be enough.
comment:5 by , 15 years ago
Simply adding the code itself in a set would be enough.
And we might want to do the same for warnings, as I've noticed we can get multiple times the same warning.
comment:6 by , 15 years ago
Keywords: | needpatch added; needinfo removed |
---|
comment:7 by , 14 years ago
Milestone: | next-major-0.1X → 0.13 |
---|
Patch is more or less OK, will tweak according to comment:4 and apply.
comment:8 by , 14 years ago
7775-add-inline-script-r10417.patch is an update for current trunk, and the duplicate elimination is much simpler (even using a set()
is overkill, as there will only ever be a handful of items at most).
But isn't this superseded by add_script_data()
? It allows adding JavaScript data from Python, which was the original request. I vote "wontfix" (or "worksforme").
comment:9 by , 14 years ago
Milestone: | 0.13 → next-major-0.1X |
---|
This add_inline_script()
could still be theoretically useful for triggering some immediate action on page load ($(document).ready(...)
). Maybe we could just defer the ticket until someone has a real use for this?
follow-up: 11 comment:10 by , 14 years ago
I don't mind. My thought was that any code triggered on page load would almost always be the same, and only the data would change. In that case, the code should be in the template or a separate .js
, and the data can be passed with add_script_data()
.
comment:11 by , 14 years ago
Milestone: | next-major-0.1X |
---|---|
Resolution: | → wontfix |
Status: | new → closed |
Replying to rblank:
… In that case, the code should be in the template
Not easily possible for plugins, unless they write a template stream filter, which seems overkill for that.
or a separate
.js
,
and I was not sure what the timing would be for a $(document).ready(...)
snippet present in a secondary .js file… well, I suppose it's going to be the same (the document as a whole must be ready…).
So OK for wontfix.
Couldn't that be done by sending a request?