Results 1 to 5 of 5

Thread: (Javascript) Is it possible to access the script dom element from inside the script?

  1. #1
    Join Date
    Oct 2006
    Location
    Argentina
    Beans
    584
    Distro
    Ubuntu

    (Javascript) Is it possible to access the script dom element from inside the script?

    Hello, I know that I could use an id but I wonder if there is another way to do it for example:

    Code:
    <body>bla, bla, bla...
    
    <script>alert(this.tagName)</script>
    
    </body>
    Alert should display: "script"
    Thanks in advance.

  2. #2
    Join Date
    May 2006
    Beans
    219

    Re: (Javascript) Is it possible to access the script dom element from inside the scri

    dunno if this is gonna be any help but assigning an id to the script tag works
    Code:
    <script id="test">
    alert( document.getElementById('test').innerHTML );
    </script>

  3. #3
    Join Date
    Oct 2006
    Location
    Argentina
    Beans
    584
    Distro
    Ubuntu

    Re: (Javascript) Is it possible to access the script dom element from inside the scri

    Yes, yes, I know that I can use an id but I meant without id, class, or other alike solutions.
    For example if I am not mistaken document.write writes in the current element, I want to get that element without identifying the script tag.
    Is it possible?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Beans
    535
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: (Javascript) Is it possible to access the script dom element from inside the scri

    I'm curious as to why you want to do this. There may be a better way of achieving what you want to do without getting the script DOM element.

    document.write(s) is equivalent to document.body.innerHTML = s (it writes to the body element, overwriting the current content.)

    It is hard to get the script element without using an id but you can use
    document.getElementsByTagName('script'), which returns an array of script elements in the order they appear on the page.
    DMedia - Distributed Media Library
    LaVida - A simulation game for Linux
    AskUbuntu

  5. #5
    Join Date
    Oct 2006
    Location
    Argentina
    Beans
    584
    Distro
    Ubuntu

    Re: (Javascript) Is it possible to access the script dom element from inside the scri

    Quote Originally Posted by dv3500ea View Post
    I'm curious as to why you want to do this.
    I have a page that loads content dynamically and sometimes the content that gets loaded has a script that manipulates the loaded content.
    Currently I assign an unique id to the script on the server that is the number of the process plus an increasing counter in case there were many script in the same request.
    I thought that if it is possible, it would be much simpler and easy to get the element from inside the script like with events: onclick="myfunction(this)"

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •