AddyProg
AddyProg

Reputation: 3050

Finding Specific text in body and removing it via JS

I am creating HTML and ASPX file via C# code in APX everything works fine but in HTML page it appends the aspx tag which shows as plain text in body. i want to remove any text in body that starts with <% and ends with %>

here is the generated html

<body>
   <center>
 <%String action = "http://" + Request.Url.Authority+"/ABC/Data/a12.aspx" ; %>
     <div>
        <form>
          // some input fields and a submit button
        </form>
     </div>
   </center>
</body>

I have tried

document.body.innerHTML = document.body.innerHTML.replace( <%String action = "http://" + Request.Url.Authority+"/ABC/Data/a12.aspx" ; %>, "");

but didn't work for me, as the generated aspx tag is not same all the time..

Upvotes: 0

Views: 825

Answers (3)

Khalid T.
Khalid T.

Reputation: 10547

Code Render Blocks shouldn't be rendered in HTML pages unless you're using them wrong. Try to modify your markup as follows:

<body>
   <center>
     <div>
        <form action='<%= "http://" + Request.Url.Authority + "/ABC/Data/a12.aspx" %>'>
          // some input fields and a submit button
        </form>
     </div>
   </center>
</body>

Upvotes: 0

Cayter Goh
Cayter Goh

Reputation: 11

By using regular expression, I guess this should give what you need:

<script>
  document.body.innerHTML = document.body.innerHTML.replace(/(<%).*(%>)/ig, '');
</script>

Upvotes: 0

edi9999
edi9999

Reputation: 20544

In this code:

document.body.innerHTML = document.body.innerHTML
    .replace( <%String action = "http://" + Request.Url.Authority+"/ABC/Data/a12.aspx" ; %>, "");

You are: not using a string as the first argument, this is invalid.

I would suggest you to use a regex, that matches <% [anything] %>.

Like this:

document.body.innerHTML = document.body.innerHTML
    .replace(/<%.*%>/g, "");

Upvotes: 1

Related Questions