Search code examples
asp.nethtmlradix

using html-base tag in aspx page


I have an aspx page which shows preview of html-email. The html email comes from DB as text. This is assigned to a div. Now i have a base tag which carries a url which is dynamic based on the DB server name.

When i assign the html from DB to the div, the base for the relative url of the images inside the html are taking the base url of the page instead of the base url from the html text. basically, the page is rendered as,

<html>
 ....
 <div> <html>
          <head>
             <base href="dynamic url" />
          </head>
          <body>
            <img src="images/header.jpg" />
            .....
          </body>
        </html>
 </div>
 .....
</html>

The 2nd html comes from DB.
How to force the dynamic url of the img tag to take the base url from base tag.

Now the img src is coming as, page_url/images/header.jpg. I want it as dynamic_url/images/header.jpg.


Solution

  • I think you should be able to do this in two different methods. First replace the text "images/" to your dynamic URL on the server side before you assign the email HTML to the container. It should be straight forward whatever language you are using.

    If that is not possible then you can do that on client side with JQuery. The code below loops through all the img tags on the page and replaces to a path you specify. This does not actually change the source code but it renders the images with the new path.

    $(document).ready(function(){
        $("img").each(function(i) {
            $(this).attr('src', $(this).attr('src').replace('old_path', 'new_Path'));
        });
    });