Forum Discussion

TNY_122436's avatar
TNY_122436
Icon for Nimbostratus rankNimbostratus
Jun 19, 2013

Javascript insert to HTML

Hello all,

 

Is it possible to insert a small piece of Javascript into HTML when traffic passes through the LTM into a certain virtual server? So one of our 3rd party vendor we use for monitoring solutions has a javascript that can be use to insert into HTML in order to compute performance measurements. Is it possilbe to create an iRule to go about this or is there a better way of doing this on the F5 itself? Any help is appreciated. Here is the piece of javascript they want inserted into HTML:

 

 

 

 

 

 

 

 

 

 

10 Replies

  • There's several ways to do this, but I prefer a STREAM profile for its simplicity, flexibility, and speed. To make it work, apply a generic STREAM profile to your virtual server and this iRule:

     

     

     

    
    when HTTP_REQUEST {
         Disable the stream filter for all requests
        STREAM::disable
        
         LTM does not decompress response content, so if the server has compression enabled
         and it cannot be disabled on the server, we can prevent the server from sending
         a compressed response by removing the compression offerings from the client
        HTTP::header remove "Accept-Encoding"
    }
    when HTTP_RESPONSE {
         Check if response type is text
        if { [HTTP::header value Content-Type] contains "text" } {
    
             Define the stream replacement
            STREAM::expression {@@@}
    
             Enable the stream filter for this response only
            STREAM::enable
        }
    }
    

     

     

    In this example I'm replacing the end body tag with a piece of JavaScript code and another end body tag. Placing the JavaScript at the bottom of the page has roughly the same effect as an onload statement in the beginning body tag, plus it's easier to manipulate. You could, alternatively, replace the end head tag with a JavaScript function and another end head tag, then replace the beginning body tag with one that has an onload statement.

     

     

  • The editor messed up the iRule. Trying again:

    
    when HTTP_REQUEST {
         Disable the stream filter for all requests
        STREAM::disable
        
         LTM does not decompress response content, so if the server has compression enabled
         and it cannot be disabled on the server, we can prevent the server from sending
         a compressed response by removing the compression offerings from the client
        HTTP::header remove "Accept-Encoding"
    } 
    when HTTP_RESPONSE {
         Check if response type is text
        if { [HTTP::header value Content-Type] contains "text" } {
    
             Define the stream replacement
            STREAM::expression {@@@}
    
             Enable the stream filter for this response only
            STREAM::enable
        }
    } 
    
  • Kevin, I greatly appreciate your help. I'm going to implement this and see how it goes. I will let you know.
  • Kevin, I just noticed that in my original thread, some of my contents of the javascript was missing. Here is the javascript code again. Let me know if there should be changes to the stream expression base on what I missed originally:

     

     

     

     

     

     

     

     

     

     

     

     

     

  • What I did in the STREAM expression was to remove the JavaScript code from the function and simply embed it at the end of the document within script tags. So your STREAM expression basically looks like this:

     

     

    
    {@@@}
    

     

  • This STREAM replacement abandons the JavaScript function at the beginning of the document and adds a block of JavaScript at the end of the document (without the function).

    Here's what the STREAM replacement would produce:

    
    
    
    
    
    
    ...
    
    
    
    

  • Ok. So is this what I should put this in the new Stream:: expression?:

     
     Define the stream replacement
            STREAM::expression {@@@}
    
  • Almost:

    
     Define the stream replacement
            STREAM::expression {@@@}
    

    You don't include the end html tag in the replacement.