Fred Lazy said:
First of all, a simple script include tag would exist:
<script src="
http://somedomain.com/something.js" id="Click here"></script>
An id attribute value must not contain spaces, but more importantly,
the script element does not have an id attribute in HTML. Relying on
non-standard attributes is fragile, since browsers are bound to treat
them differently.
Can the "id" be converted into a variable string within something.js and
later become a "linked_text" in the document.write html-output, as follows:
/* start (of an oversimplied version) of something.js */
var linked_text = somehow suck in the "id" of this include file;
d.write('<a href="..." onclick="anotherfunction();">'+linked_text+'</a>')
/* end of file */
The only attribute of the script element that's safe to use is the
"src" attrbiute. The other valid attributes ("charset", "type",
"defer", and "language" - the last one only in transitional documents)
are meaningfull to the browser, and writing extra information into
them can make the script unusable.
So, if your user *really* can't be expected to make a script previous
to including yours where they set the necessary variables, e.g.,
<script type="text/javascript">
var something_text = "Click here";
var something_other = 42;
</script>
(which is pretty easy to describe to them and make examples of),
then I'd suggest using the src attribute:
<script src="somepath/something.js?text=Click%20here"></script>
Then you could use server-side scripting to create a script file
with the values pre-inserted (easier on your users).
If server-side scripting isn't possible, you'll have to fall back
on parsing the src attribute manually:
---
function extratVars() {
// find last script element on page
var scrs = document.getElementsByTagName("script");
var lastScript = scrs[scrs.length-1];
// extract vars after ?
var src = lastScript.src;
var paramstr = src.substring(src.indexOf("?")+1);
var params = paramstr.split("&");
var vars = {};
for (var i = 0; i < params.length; i++) {
var parts = params
.split("=");
vars[unescape(parts[0])] = unescape(parts[1]);
}
return vars;
}
// use vars
var vars = extractVars();
alert(vars["text"]); // alerts "Click here"
---
Tested i Firefox, IE 6 and Opera 9.
Naturally the text_link variable could easily be passed via a standard
on-page javascript function above the include call, but the usage of the
script will be cross-domain, for site visitors to include a link to a
function on their websites, and be able to specify the linked text string,
but they cannot modify the external something.js file.
It's not that hard to explain that they need to set some variables
before including your script.
Or make a page for them where they can enter the text, and it generates
the code they need to insert.
These site visitors are not necessarily half-knowledgable in js or html
either, so the shorter the code of the include, the better, i.e. one
include would be better than having one function containing only a variable
plus the include itself, i.e. I'm trying to find a one-liner solution.
Use the "src", Luke If your goal is to make it simple for others
to use, you should use server side scripting and not rely on the browser
being able to work on the script element.
Or again, make a helper page where they write the texts in input
controls and then it generates the HTML they need to add to their
page.
/L