javascript - Reference script container element? -


i'm wondering if there way handle on dom element contains script inside it. if had:

<script type="text/javascript> var x = ?; </script> 

is there way can assign "x" reference script element contains "x"?

you include marker text in script element, , (similar david said), can loop through script elements in dom (using getelementsbytagname or whatever features library, if you're using one, may have). should find element reliably. (live example):

<body>   <script id='first' type='text/javascript'>     (function() {       var x = "marker:first";     })();   </script>   <script id='second' type='text/javascript'>     (function() {       var x = "marker:second";     })();   </script>   <script id='third' type='text/javascript'>     (function() {       var x = "marker:third";     })();   </script>   <script id='last' type='text/javascript'>     (function() {       var scripts, index, script;        scripts = document.getelementsbytagname("script");       (index = 0; index < scripts.length; ++index) {         script = scripts[index];         if (script.innerhtml.indexof("marker:second") >= 0            && script.id !== "last") {           display("found marker:second in script tag #" + script.id);         }       }        function display(msg) {         var p = document.createelement('p');         p.innerhtml = msg;         document.body.appendchild(p);       }     })();   </script> </body> 

note that, script above, if you're looking script tag marker within different script tag, you'll need handle that. above it's handled checking id of script tag, can break in 1 don't want find, (live example):

if (script.innerhtml.indexof("marker:" + "second") >= 0) {   display("found marker:" + "second in script tag #" + script.id); } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -