3

Is there any way to get the parent node of current script tag. I wanted to assign id attribute to the script tag but found that it does not support standard attributes.

Actually I want to get the title of a document inside a frame and document.title is returning the title of the webpage.

Irfanullah Jan
  • 3,336
  • 4
  • 24
  • 34
  • Just for clarification: are we talking `frame` or an `iframe`? – Cᴏʀʏ Dec 07 '11 at 04:02
  • Yeah, with those possible exceptions, you can't get the element that the script resides in without an id or other method. You can however use the this keyword for inline functions. – Jeffrey Sweeney Dec 07 '11 at 04:04
  • is the document being loaded into the frame on the same domain as the parent page? – Aaron Dec 07 '11 at 04:27
  • If you're loading content form another domain (or being loaded into a page on another domain), you can't access the other page's DOM, and therefore, don't have access to its tag. – Aaron Dec 07 '11 at 04:48

1 Answers1

6

I believe this is what you are looking for:

document.currentScript.parentNode

The documentation for Document.currentScript says:

Returns the element whose script is currently being processed.

maxpaj
  • 6,029
  • 5
  • 35
  • 56