Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REPLACE HTML

Status
Not open for further replies.

ArizonaRedneck

Programmer
Oct 25, 2004
61
US
Is it possible to get the html of a page during it's page load... basically I want to replace part of the text (i.e. *First Name) with dynamic text. I was hoping to somehow get the html for the page, replace the values, and then display the new html to the user. any ideas?
 
Can you instead use a Label control at the place where you'd like to replace the text? Doing so would let you change the .Text property of the label on page load.
 
I can't use a label because I'm making a template and I would have to make a label for each one... kink of tedious. As far as the innerHTML, how would I do a replace in there. Thanks for the help guys.
 
My recommendation for this is to use JavaScript, rather than server-side code.

"innerHTML" is a property of IE's DOM. You refer to it:

element.innerHTML

That returns a string. To alter the string, you use JavaScript string functions, such as "subString()" and "indexOf()".

Example code:

Code:
<HTML>
<HEAD>
<script>
function changeHTML()
{
  var x = document.getElementById("myDIV").innerHTML;
  var targetString = "a string";
  var replaceString = "and now for something completely different.";

  var startPos = x.indexOf(targetString,0);

  var newString = "";

  newString = x.substring(0,startPos) + replaceString +
  x.substring(startPos+targetString.length,x.length);

  document.getElementById("myDiv").innerHTML = newString;

}
</script>

</HEAD>
<BODY onload="changeHTML();">

<div id="myDIV">
  <h1>My Header</h1>
  <b>a string</b>
</div>

</BODY>
</HTML>


Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
thanks man! That worked great, my only problem is that I'm on XP and it tried to block the content when I viewed the page and didn't show up correctly until I clicked to "allow blocked content". this would not look cool if the user ignored it and the page said "*FirstName" instead of "Bob". Is there a way around this? Thanks again for your advice
 
IE only does that when you browse/load a local file through the file system.

If you truly truly truly want to alter the HTML prior to it being served to the browser, you can override the server control's RenderControls method.

I'm not sure, though, if your HTML is being rendered by a server control or not, so can't get more specific than that.

But the RenderControls() method, if you override it, allows you to control the HTML produced by a given web server control.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
that worked GREAT! Thank you so much for your help, and for getting right back to me. I havn't done much client side scripting before so this is a little new to me. I have just one final question. with the part of:

newString = x.substring(0,startPos) + replaceString +
x.substring(startPos+targetString.length,x.length);

what would the syntax be for multiple values to replace

in VB.net it would be:
replace(replace(x,targetString1,replaceString1),targetString2,replacestring2)


Could I combine them into one statement like in VB or would I need seperate ones?

thanks
 
I haven't seen a structure like that in JavaScript.

Of course, you can put all of your targetStrings in an array, then loop through the array, calling the replace function for each array element.


I'll leave that as an exercise for the reader. Google for: JavaScript Array Loops.




Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top