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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replcaing Text Using Tags and Text File

Status
Not open for further replies.

VinceK

Technical User
Mar 29, 2005
4
US
Hey All,

I'm currently building a CD for my members to use to promote their businesses, using a basic html page. What I need to do is be able to use javascript to replace some tags in the html files with a members information from a seperate text file.

I'm sure this is a very simple process, but I can't seem to find a complete piece of code for this anywhere on the internet. I've been searching for several hours now and I've had enough.

I'd like to be able to use tags ~username~ or ~firstname~ in the body of the html files and have that replaced with the actual information from the text file, this way we can simply drop in a new text file for each member, recompile the cd and bam, we're done.

If someone could help with this I would certainly appreciate it. Or if you could point me to a complete script somewhere that would be great.

Thanks so much.

V
 
im not sure that you can do that with javascript. I'm pretty sure that all javascript is client side, but there are parts of vbscript that can access server side files. So im not sure how you are wanting to manage that part or if maybe you already have that part complete.

once you have the template html file... open it to read and populate a variable with the html from the file. from there you can replace what specific parts you want with this code:

myString=TEMPLATE_HTML_TEXT;
myString=myString.replace("~username~",ACTUAL_USER_NAME);

then you can write a new file with whatever name you chose or overwrite the origional should you decide you dont need it any more.

hope this helps
 
Here's one way:

Use real tags like <username /> and <firstname />

Include this on every page:
Code:
<script language="JavaScript" src="global.js"></script>

In the contents of globals.js:
Code:
window.onload=function(){
  [green]//Change this for each member[/green]
  var username = "jsmith";
  var firstName = "John";

  var usr = document.getElementsByTagName("username");
  for(var i=0;i<usr.length;i++){
    usr[i].outerHTML = username;
  }

  var fn = document.getElementsByTagName("firstname");
  for(var i=0;i<fn.length;i++){
    fn[i].outerHTML = firstname;
  }
}

Adam

Whatever I feel like I wanna do, gosh!
 
Awesome,

That did it Adam thanks so much! I appreciate is very much!

V
 
Hey Adam,

Most fields work well but the most important field is username which is included in all the links. Everything parses perfectly except links.

How can I get this to work within the links since they are already inside <> tags?

Thanks

V
 
I'm guessing you mean something like this?
Code:
<html>
<head>
<script>
window.onload=function(){
  //Change this for each member
  var username = "jsmith";
  var firstName = "John";

  var usr = document.getElementsByTagName("username");
  for(var i=0;i<usr.length;i++){
    usr[i].outerHTML = username;
  }

  var fn = document.getElementsByTagName("firstname");
  for(var i=0;i<fn.length;i++){
    fn[i].outerHTML = firstname;
  }
[red]
  var lnks = document.links;
  for(var i=0;i<lnks.length;i++){
    lnks[i].href = lnks[i].href.replace(/~username~/g,username);
  }  [/red]
}
</script>
</head>
<body>
<a href="[URL unfurl="true"]http://mydomain.com?usr=[/URL][red]~username~[/red]"><username /></a>
Note that I used ~username~ instead of <username /> since it's already in a tag. You can change the syntax to whatever works for you though.

Adam

Whatever I feel like I wanna do, gosh!
 
That did the trick!

Thank you so much Adam, you saved me a ton of time and searching!

V
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top