how to Retrieve the parameters of document.write to detect the creation of dynamic tags

Posted by user1335906 on Stack Overflow See other posts from Stack Overflow or by user1335906
Published on 2012-04-16T10:02:49Z Indexed on 2012/04/16 11:29 UTC
Read the original article Hit count: 173

Filed under:
|

In my Project i am supposed to identify the dynamically created tags which can be done in scripts through

document.write("<script src='jquery.js'></script>")

For this i used Regular expressions and my code is as follows

function find_tag_docwrite(text)
{   

  var attrib=new Object;
  var pat_tag=/<((\S+)\s(.*))>/g;     
     while(t=pat_tag.exec(text)
     {
        var tag=RegExp.$1;
        for(i=0;i<tags.length;i++)  
        {  
                var pat=/(\S+)=((['"]*)(\S+)(['"]*)\3)/g;
                while(p=pat.exec(f))
                {
                    attr=RegExp.$1;val=RegExp.$4;                    
                    attrib[attr]=val;
                }
        }      
     }  


}

in the above function text is parameters of document.write function. Now through this code i am getting the tag names and all the attributes of the tags.

But for the below example the above code is not working

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

In such cases Regular expressions does not work so after searching some time where i found hooks on dom methods. so by using this i thought of creating hook for document.write method but i am able to understand how it is done i included the following code in my program but it is not working.

function someFunction(text) 
{
  console.log(text);
}

document.write = someFunction;

where text is the parameters of document.write.

Another problem is After monitoring all the document.write methods using hooks again i have to use regex for finding tag creations. Is there Any alternative

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about dom