By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

accessing a script's tag (and attributes)

P: n/a
Hey All,

I was wondering if there were a way for a script to access it's parent
tag without having to use the document.all.tags["script"] method which
doesn't necessarily identify it if there are more than 1 script tag in
the document.

eg.

<html><body>
<script rein:myAttribute="I'm a madman">
// how can I access the surrounding tag from script within?
</script>
</body></html>

Thx in advance, Rein

Feb 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

re***********@gmail.com wrote:

<script rein:myAttribute="I'm a madman">
// how can I access the surrounding tag from script within?


var scriptElements = document.getElementsByTagName('script');
var currentScript = scriptElements[scriptElements.length - 1];
should do.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 2 '06 #2

P: n/a
Hey thanks for replaying Martin,

I think I should have been more descriptive about what I was hoping to
achieve. I know that I can access the document (window.document)
globally from script.

I was hoping that javascript enclosed in SCRIPT tag, might be able to
access that tag with a method or keyword (like 'this') so I could pull
custom attributes from the tag.

The problem with getting at it globally is that I might want to have
multiple scripts in the document and I would need to create a way to
uniquely identify the SCRIPT tags. That would be fine but now the
script contained in the tag cannot be generic because it will have to
know of the identifier.

But thank again for replying, it's nice to know there are people out
there willing to lend a hand...

Rein

Feb 2 '06 #3

P: n/a
re***********@gmail.com wrote:
Hey thanks for replaying Martin,

I think I should have been more descriptive about what I was hoping to
achieve. I know that I can access the document (window.document)
globally from script.

I was hoping that javascript enclosed in SCRIPT tag, might be able to
access that tag with a method or keyword (like 'this') so I could pull
custom attributes from the tag.
You can't (other than via Martin's example which has limited
application). Nor can you reliably access custom attributes across
different browsers.

The problem with getting at it globally is that I might want to have
multiple scripts in the document and I would need to create a way to
uniquely identify the SCRIPT tags. That would be fine but now the
script contained in the tag cannot be generic because it will have to
know of the identifier.
A function has no concept of the HTML script element it is contained in.
The HTML specification does not say that script elements must have an
ID or name (though these seem to be supported by at least some browsers)
and not all browsers will cope with custom element attributes.

So you can't reliably identify individual script elements other than as
members of the collection returned by document.getElementsByTagName().

But thank again for replying, it's nice to know there are people out
there willing to lend a hand...


It seems that it is leading to a very kludgey solution, probably
discouraging assistance.

What you seem to want to do is conditionally insert a script element
with some custom data. You then want a function in the script to be
able to identify the bit of custom data so it can use it.

Your attempted solution is to include the data as an attribute of the
script element, but you can't work out how to uniquely identify the
script element and you'll have cross-platform issues with custom attributes.

Perhaps you should use whatever logic you are using to insert the custom
attribute to insert the data into the page as a script variable and
determine some other scheme to associate it with the function, e.g. you
might make it a property of an object, an element of an array or just a
global variable.
--
Rob
Feb 3 '06 #4

P: n/a


re***********@gmail.com wrote:

I think I should have been more descriptive about what I was hoping to
achieve. I know that I can access the document (window.document)
globally from script.

I was hoping that javascript enclosed in SCRIPT tag, might be able to
access that tag with a method or keyword (like 'this') so I could pull
custom attributes from the tag.


And I told you to use

<script type="text/javascript">
var scriptElements = document.getElementsByTagName('script');
var currentScript = scriptElements[scriptElements.length - 1];

then currentScript is the script element and if you then want or need
you can call e.g.
currentScript.getAttribute('attributename')
on that.
That should work for inline script being executed while the page loads
as when the browser executes that script the enclosing script element is
the last that has been parsed.

As for doing
<script rein:myAttribute="I'm a madman">
in HTML markup, that is not necessarily a good idea, not sure where you
got the idea to put colons in attribute names. If that were XHTML then
using a prefix (e.g. rein) might make sense if you declare that e.g.
<script xmlns:rein="http://example.com/2006/ns1"
rein:myAttribute="I'm a madman">
You would need a namespace aware XML DOM Level 2 then. Unfortunately my
suggestion to access the last current script element in
document.getElementsByTagName('script') does not work reliably in XHTML
served as application/xhtml+xml or application/xml or text/xml to
Mozilla as there is no incremental parsing so far.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 3 '06 #5

P: n/a
The idea is to simplify AJAX by component-izing functionality. The
custom properties on the script tag would make it easier for HTML
designers to include functionality on the page. Events and methods
would still be handled in javascript.

Of course, you could make the entire page from javascript but I don't
think the designers would like that very much.

HTML Components (HTCs) is a W3C suggestion from MS that is similar (you
can wrap them in wrappers for Mozilla XBL -
http://dean.edwards.name/moz-behaviors/) but the net result is that it
is still too complicated for many people. Even if it's not too
complicated for you, it seems like too much work to find a few
time-savers. Also, I prefer pure javascript.

In HTCs favour, they posess the 'element' property inside an HTC that
allows you access to the containing HTC tag (along with many other cool
features). Dave Edwards (the fellow who created the moz-behaviours XBL
wrappers for HTC) was also able to represent the 'element' property
along with support for most of the other features.

Rein

Feb 3 '06 #6

P: n/a
Hey Martin,

Sorry it may have seemed I was blowing off your solution. Esp. because
I am realizing that it's just about the only way that I can crack the
nut. I'll probably also reconsider the namespaced attributes - I just
didn't want to worry about stepping on any existing script tag
properties. But there are so few so it probably won't matter.

Rein

Feb 3 '06 #7

P: n/a
re***********@gmail.com wrote:
The idea is to simplify AJAX by component-izing functionality. The
custom properties on the script tag would make it easier for HTML
designers to include functionality on the page. Events and methods
would still be handled in javascript.
I think I get where you are coming from. Do you want to insert a script
element like:

<script type="text/javascript"
id="aScript01" src="somefile.js"
myAtt1="foo" myAtt2="bar"</script>
where myAtt1/2 contain data to be used by functions contained in
somefile.js?

In that case, Martin's method will work when the page loads (provided
you can get access to the custom attributes - custom name space might be
the go) but if you insert the script using AJAX at some later time it
won't unless you insert the script as the last script element in the page.

Of course, you could make the entire page from javascript but I don't
think the designers would like that very much.
I don't think anyone would like that. :-)

HTML Components (HTCs) is a W3C suggestion from MS that is similar (you
can wrap them in wrappers for Mozilla XBL -
http://dean.edwards.name/moz-behaviors/) but the net result is that it
is still too complicated for many people. Even if it's not too
complicated for you, it seems like too much work to find a few
time-savers. Also, I prefer pure javascript.

In HTCs favour, they posess the 'element' property inside an HTC that
allows you access to the containing HTC tag (along with many other cool
features). Dave Edwards (the fellow who created the moz-behaviours XBL
wrappers for HTC) was also able to represent the 'element' property
along with support for most of the other features.


Presumably the data is to be associated with some object. Have you
considered creating a data object that stores your custom values, e.g.
if you have a HTML element with ID=div01, then your custom data object
could contain:

var reinCustObj = {
div01 :
{ att01 : 'foo',
att02 : 'bar'
},
div02 :
{ att01 : 'foo2',
att02 : 'bar2'
}
};

which associates the data with the HTML element or object rather than a
piece of script. You could adjust the syntax of the object to suit your
developers, e.g. they might find the following easier:

var reinCustObj = {};
reinCustObj.div01 = { att01 : 'foo',
att02 : 'bar'
};
reinCustObj.div02 = { att01 : 'foo2',
att02 : 'bar2'
};
Doing the above will lead you very quickly to JSON:

<URL:http://www.crockford.com/JSON/>
--
Rob
Feb 5 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.