View Full Version : is this possible? entering a word and have it show where you want.......
mbrmbr
06-15-2008, 02:45 PM
I have a paragraph with the same word typed over and over again throughout it, but I dont feel like modifying that word over and over again each time I change it in every single spot that it is located.....
example sentence:
word that will be typed over and over again: GOOGLE
Hello this is my name and I like to use GOOGLE as my search engine, however I think that GOOGLE is not the best search engine but I still use GOOGLE anyway.
-----------------------------
Now I want to change it to YAHOO but w/o changing the word each time it shows in the sentence
Hello this is my name and I like to use YAHOO as my search engine, however I think that YAHOO is not the best search engine but I still use YAHOO anyway.
Am I making sense?
Horus_Kol
06-15-2008, 07:20 PM
do a find/replace ALL in whatever editor you are working in?
mbrmbr
06-15-2008, 08:40 PM
do a find/replace ALL in whatever editor you are working in?
I need to be able to do it with just raw HTML code and no programs.... If I remember correctly I've seen it done with a code like [$word] in PHP, but I really don't know.
Horus_Kol
06-15-2008, 10:19 PM
i really don't get what you're trying to do here...
you can use PHP to insert variables in a string:
$word = "GOOGLE";
echo "Hello this is my name and I like to use " . $word . " as my search engine, however I think that " . $word . " is not the best search engine but I still use " . $word . " anyway.";
Is that what you want?
mbrmbr
06-15-2008, 11:12 PM
i really don't get what you're trying to do here...
you can use PHP to insert variables in a string:
$word = "GOOGLE";
echo "Hello this is my name and I like to use " . $word . " as my search engine, however I think that " . $word . " is not the best search engine but I still use " . $word . " anyway.";
Is that what you want?
Is there a way to do that in HTML or to make it work in an html code?
Horus_Kol
06-15-2008, 11:46 PM
HTML code is just markup - you can't do variable replacement in HTML all by itself...
It is possible to do it using JavaScript:
<div id="search_engine"></div>
<script type="text/javascript">
var word = 'GOOGLE';
document.getElementById('search_engine').innerHTML = 'Hello this is my name and I like to use ' + word + ' as my search engine, however I think that ' + word + ' is not the best search engine but I still use ' + word + ' anyway.';
</script>
coothead
06-16-2008, 04:04 AM
Hi there mbrmbr,
as Horus_Kol has pointed out, javascript will be required to search and replace a specific word in an HTML document. :agree:
Here is an example...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var indx, c;
var contents=[];
var searchTerm='google'; /* this is the word to be replaced */
var regExObj=new RegExp(searchTerm,'gi'); /* this allows for upper or lower case letters */
var els=document.body.getElementsByTagName('*'); /* this finds all the 'body' elements */
for(c=0;c<els.length;c++) {
if(els[c].firstChild.nodeValue!=null) { /* this checks for text */
contents[c]=' '+els[c].firstChild.nodeValue; /* this finds the text */
indx=contents[c].search(regExObj); /* this searches for the word */
if(indx) {
contents[c]=contents[c].replace(regExObj,'YAHOO'); /* this replaces the old word with the new */
els[c].firstChild.nodeValue=contents[c]; /* this is the amended 'element' content */
}
}
}
}
</script>
</head>
<body>
<div>
<p>
Hello this is my name and I like to use GOOGLE as my search engine, however I
think that gooGLE is not the best search engine but I still use GoOgLe anyway.
</p><p>
Hello this is my name and I like to use GOOGLE as my search engine, however I
think that GOogLE is not the best search engine but I still use gOoGlE anyway.
</p><p>
Hello this is my name and I like to use GOOGLE as my search engine, however I
think that GOOGLE is not the best search engine but I still use GOOGLE anyway.
</p>
</div>
</body>
</html>
Do not use this script. Instead use the script in post #15
mbrmbr
06-16-2008, 01:24 PM
None of these codes are working when I test them out.
coothead
06-16-2008, 05:59 PM
Hi there mbrmbr,
None of these codes are working when I test them out.
Are you saying that the code that I posted does not work. :disagree:
Well, I am afraid, that is utter nonsense. :agree:
The code that I posted has been thoroughly tested in these browsers...
IE 6
IE 7
Firefox 2.0.0.14
Opera 9.5
Safari 3.1
Perhaps you could show us the code that you have used for your test and
also let us know the browsers in which you have tested it unsuccessfully.
mbrmbr
06-16-2008, 06:57 PM
I am testing them via dreamweaver 8 through a IE7 preview in a basic page format.
EDIT: cootheads code is being displayed, but when the main word is changed nothing changes. It displays but it cant be modified.
EDIT 2: I change this variable "var searchTerm='google'; /* this is the word to be replaced */"
to
var searchTerm='yahoo'; /* this is the word to be replaced */
but it is not modifying everything in the paragraph.......... maybe I have it explained wrong.....
I want it to show immediatly, I dont want it to run scripts just to have a main word assotiated with a code..... I want a word to be assigned a variable so that it can be changed with only modifying it once.
This is a completly made up HTML code but use it as an example:
<style type="text/css">
<!--
.style1 {
<field1>="google";
}
-->
</style>
<span class="style1">This is <field1> and <field1> is very <field1>.</span>
THE SENTENCE SHOULD READ
This is google and google is very google. Thanks for the response.
Hi there mbrmbr,
Are you saying that the code that I posted does not work. :disagree:
Well, I am afraid, that is utter nonsense. :agree:
The code that I posted has been thoroughly tested in these browsers...
IE 6
IE 7
Firefox 2.0.0.14
Opera 9.5
Safari 3.1
Perhaps you could show us the code that you have used for your test and
also let us know the browsers in which you have tested it unsuccessfully.
coothead
06-17-2008, 01:43 AM
Hi there mbrmbr,
I need to be able to do it with just raw HTML code
Is there a way to do that in HTML or to make it work in an html code?
I don't want it to run scripts just to have a main word associated with a code
I would suggest that you do not hold your breath as Hell will freeze over before HTML is able to modify itself. :agree:
mbrmbr
06-17-2008, 01:03 PM
the reason I am asking if it can be done in html is because that is what my page is made of, if I use javascript or PHP dont I have to make everything on my page like that or can I run it w/o changing anything?
coothead
06-17-2008, 04:32 PM
Hi there mbrmbr,
all that you have to do is is put this script...
<script type="text/javascript">
window.onload=function() {
/* Edit the searchTerm and replacement to suit your requirements */
var searchTerm='google'; /* this is the word to be replaced */
var replacement='YAHOO'; /* this is the replacement word */
/************* Do not edit any of the following ************/
var indx, c;
var contents=[];
var regExObj=new RegExp(searchTerm,'gi'); /* this allows for upper or lower case letters */
var els=document.body.getElementsByTagName('*'); /* this finds all the 'body' elements */
for(c=0;c<els.length;c++) {
if(els[c].firstChild.nodeValue!=null) { /* this checks for text */
contents[c]=' '+els[c].firstChild.nodeValue; /* this finds the text */
indx=contents[c].search(regExObj); /* this searches for the word */
if(indx) {
contents[c]=contents[c].replace(regExObj,replacement); /* this replaces the old word with the new */
els[c].firstChild.nodeValue=contents[c]; /* this is the amended 'element' content */
}
}
}
}
</script>
... in the head section of your html document.
It will then locate the word that you want to replace. As long as this word is in an appropriate element such as...
div, p, td, span, a, textarea, h1 etc
..it will replace it with the word or words of your choice. :agree:
Do not use this script. Instead use the script in post #15
mbrmbr
06-17-2008, 06:45 PM
Hi there mbrmbr,
all that you have to do is is put this script...
<script type="text/javascript">
window.onload=function() {
/* Edit the searchTerm and replacement to suit your requirements */
var searchTerm='google'; /* this is the word to be replaced */
var replacement='YAHOO'; /* this is the replacement word */
/************* Do not edit any of the following ************/
var indx, c;
var contents=[];
var regExObj=new RegExp(searchTerm,'gi'); /* this allows for upper or lower case letters */
var els=document.body.getElementsByTagName('*'); /* this finds all the 'body' elements */
for(c=0;c<els.length;c++) {
if(els[c].firstChild.nodeValue!=null) { /* this checks for text */
contents[c]=els[c].firstChild.nodeValue; /* this finds the text */
indx=contents[c].search(regExObj); /* this searches for the word */
if(indx) {
contents[c]=contents[c].replace(regExObj,replacement); /* this replaces the old word with the new */
els[c].firstChild.nodeValue=contents[c]; /* this is the amended 'element' content */
}
}
}
}
</script>
... in the head section of your html document.
It will then locate the word that you want to replace. As long as this word is in an appropriate element such as...
div, p, td, span, a, textarea, h1 etc
..it will replace it with the word or words of your choice. :agree:
Thanks, now this code will work in IE 6 IE 7 Firefox 2.0.0.14 Opera 9.5 Safari 3.1 correct? Finally, I dont know if this is asking too much, but is there a way to allow multiple terms to be replaced?
coothead
06-18-2008, 04:29 AM
Hi there mbrmbr,
...is there a way to allow multiple terms to be replaced?
There is. :agree:
The following example will change...
The dog has four legs, the grass is green.
...to...
The bird has two legs, the sky is blue.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bdog\b/gi, /\bfour\b/gi, /\bgrass\b/gi, /\bgreen\b/gi];
var replacement=['bird','two','sky','blue'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
</head>
<body>
<h1>
The dog has four legs, the grass is green.
</h1>
</body>
</html>
It is important that you make certain that each searchTerm word is in this form...
/\bdog\b/gi
...and that the replacement word is in the corresponding position in the array. :agree:
mbrmbr
06-18-2008, 07:21 PM
the first code worked yesterday, now it will not work today. here is my code.
<script type="text/javascript">
window.onload=function() {
/* Edit the searchTerm and replacement to suit your requirements */
var searchTerm='google'; /* this is the word to be replaced */
var replacement='YAHOO'; /* this is the replacement word */
/************* Do not edit any of the following ************/
var indx, c;
var contents=[];
var regExObj=new RegExp(searchTerm,'gi'); /* this allows for upper or lower case letters */
var els=document.body.getElementsByTagName('*'); /* this finds all the 'body' elements */
for(c=0;c<els.length;c++) {
if(els[c].firstChild.nodeValue!=null) { /* this checks for text */
contents[c]=els[c].firstChild.nodeValue; /* this finds the text */
indx=contents[c].search(regExObj); /* this searches for the word */
if(indx) {
contents[c]=contents[c].replace(regExObj,replacement); /* this replaces the old word with the new */
els[c].firstChild.nodeValue=contents[c]; /* this is the amended 'element' content */
}
}
}
}
</script>
<td>google google google</td>
coothead
06-19-2008, 04:35 AM
Hi there mbrmbr,
thanks for your post, which brought to light a slight flaw in my script.
If a searchTerm had no space between it and the containing element as in your example...
<td>google google google</td>
...it would fail.
This has now been rectified.
I would suggest that you now use this script instead...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bgoogle\b/gi];
var replacement=['YAHOO'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
</head>
<body>
<table><tr>
<td>google google google</td>
</tr></table>
</body>
</html>
mbrmbr
06-19-2008, 09:23 PM
Hi there mbrmbr,
thanks for your post, which brought to light a slight flaw in my script.
If a searchTerm had no space between it and the containing element as in your example...
<td>google google google</td>
...it would fail.
This has now been rectified.
I would suggest that you now use this script instead...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bgoogle\b/gi];
var replacement=['YAHOO'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
</head>
<body>
<table><tr>
<td>google google google</td>
</tr></table>
</body>
</html>
The script is only editing the word when it is placed in its own table alone, it wont work when surrounded by <tr> or <td> is there any way to modify this?
coothead
06-20-2008, 01:39 AM
Hi there mbrmbr,
The script is only editing the word when it is placed in its own table alone, it wont work when surrounded by <tr> or <td> is there any way to modify this?
Sorry, but you have lost me there. :confused:
Please show me your code that doesn't work, instead of quoting mine. :agree:
How else can we solve you problems?
mbrmbr
06-20-2008, 06:24 PM
Hi there mbrmbr,
Sorry, but you have lost me there. :confused:
Please show me your code that doesn't work, instead of quoting mine. :agree:
How else can we solve you problems?
This works:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bgoogle\b/gi];
var replacement=['YAHOO'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
</head>
<body>
<table><tr>
<td>google google google</td>
</tr></table>
</body>
</html>
This does not work:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bgoogle\b/gi];
var replacement=['YAHOO'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
</head>
<body>
<tr>google google google</tr>
</body>
</html>
This does not work:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bgoogle\b/gi];
var replacement=['YAHOO'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
</head>
<body>
<td>google google google</td>
</body>
</html>
It only works when it is surrounded in a table. :eek:
coothead
06-21-2008, 02:14 AM
Hi there mbrmbr,
you cannot use the tr element or the td element in isolation. :disagree:
In other words...
<tr>google google google</tr>
...and...
<td>google google google</td>
...are totally meaningless. :disagree:
I find it almost incomprehensible that you are seeking javascript code when you barely know
the basics of html code. :supereek:
The very minimum code that is valid and makes any sense at all is...
<table>
<tr>
<td>google google google</td>
</tr>
<table.
I would, humbly, suggest that before you state, with such certainty, that my code does not work
with your code, that your code is at the very least well formed and valid. :agree:
mbrmbr
06-21-2008, 07:34 PM
The elements were chosen from this post here here (http://www.htmlforums.com/showpost.php?p=652850&postcount=13). Maybe you should explain in more detail to avoid this situation in the future.
I too, humbly, suggest as an alternitive for you to consider a name change to coolhead and chill.
Here is my code, maybe I have made some errors please correct me if I have.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bterm\b/gi];
var replacement=['YAHOO'];
var indx,c,k;
var contents=[];
var els=document.body.getElementsByTagName('*');
for(c=0;c<els.length;c++) {
for(k=0;k<searchTerm.length;k++) {
if(els[c].firstChild.nodeValue!=null) {
contents[c]=' '+els[c].firstChild.nodeValue;
indx=contents[c].search(searchTerm[k]);
if(indx) {
contents[c]=contents[c].replace(searchTerm[k],replacement[k]);
els[c].firstChild.nodeValue=contents[c];
}
}
}
}
}
</script>
<TABLE width="100%" border="0" cellspacing="10" cellpadding="0">
<tr><td><div align="justify">
<p><img src="IMAGE" width="509" height="20" /><br />
This is the exact HTML that I am using to show you as an example <strong>example</strong> and here is the term I want to have change <span class="style9">TERM</span> The term (TERM) is not changing with the code. A little <u><strong>help</strong></u> would be appreciated </p>
</div></td></tr>
</TABLE>Hi there mbrmbr,
you cannot use the tr element or the td element in isolation. :disagree:
In other words...
<tr>google google google</tr>
...and...
<td>google google google</td>
...are totally meaningless. :disagree:
I find it almost incomprehensible that you are seeking javascript code when you barely know
the basics of html code. :supereek:
The very minimum code that is valid and makes any sense at all is...
<table>
<tr>
<td>google google google</td>
</tr>
<table.
I would, humbly, suggest that before you state, with such certainty, that my code does not work
with your code, that your code is at the very least well formed and valid. :agree:
coothead
06-22-2008, 06:02 AM
Hi there mbrmbr,
we appear to have been at cross purposes here. :agree:
I have assumed, wrongly it seems, that we were dealing with elements that contained just text. :o
My code, therefore, did not allow for img elements.
The amended code will, hopefully, allow for everything, including the kitchen sink. :D
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#mytable {
width:100%;
}
#mytable div {
text-align:justify;
}
#mytable strong {
text-decoration:underline;
}
#mytable img {
width:509px;
height:20px;
display:block;
}
.style9 {
color:#900;
}
</style>
<script type="text/javascript">
window.onload=function(){
var searchTerm=[/\bterm\b/gi,/\bis not\b/gi,/\bA little\b/gi,/\bwould be\b/gi];
var replacement=['YAHOO','is now','Your','is'];
var indx,k;
for(k=0;k<searchTerm.length;k++) {
indx=document.body.innerHTML.search(searchTerm[k]);
if(indx) {
document.body.innerHTML=document.body.innerHTML.replace(searchTerm[k],replacement[k]);
}
}
}
</script>
</head>
<body>
<table id="mytable" cellspacing="10" cellpadding="0">
<tr>
<td>
<div>
<p>
<img src="some_image.jpg" alt="">
This is the exact HTML that I am using to show you as an <strong>example</strong> and
here is the word I want to have changed to <span class="style9">TERM</span>. The word (TERM) is not
changing with the code. A little <strong>help</strong> would be appreciated.
</p>
</div>
</td>
</tr>
</table>
</body>
</html>
mbrmbr
06-22-2008, 12:07 PM
the code works great, thank you.
While this thread is still going, I would also like to use this type of system in another location that does not allow javascript... Is this something that can be done using CSS or some other alternitive?
coothead
06-22-2008, 01:52 PM
Hi there mbrmbr,
it can probably be done server side. :agree:
Check out the appropriate forum for further help. ;)
mbrmbr
06-24-2008, 10:36 PM
Forget it.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.