Click to See Complete Forum and Search --> : Replace Method Questions
kwilliams
12-11-2003, 09:38 AM
I'm trying to create a simple JS Replace Method to replace HTML tags from a database's data with empty spaces. This is what I've come up with so far:
.....<script language="JavaScript">
function tagreplace() {
var tags = "<P>"; <--LINE 19
tags.Replace(tags,"");}
</script>
</head>
<body onLoad="tagreplace()".....
When I set it up this way, I get this error message:
Object doesn't support property or method
//NOTE: REFERRING TO LINE 19, WHICH IS var tags = "<P>";
I also tried changing the "<P>" to "<P>", but it didn't help. Does anyone see something that's ovbiously wrong with the Replace Method I'm trying to use? Any & all help is appreciated. Thanks.
I eventually want to set this up to replace all of the <HTML> tags (i.e. <P>,</P>,<BR>, etc.). If anyone could also tell me what I'd need to do to accomplish this, it would be greatly appreciated.
KWilliams
Red flags go up here...
Are you trying to do this to do some data scrubbing from your database (ie, to make sure uses do not enter HTML/JavaScript/Server-side language code that will be run?) If so, you MUST do this server-side, with the language you are using to read from the database. If I'm way off, let me know and I'll see if I can help you further.
kwilliams
12-11-2003, 10:17 AM
Hell agaon Pyro,
I should have explained that a bit further, but I didn't want to get to complicated on the post. I'm using a new Rich Text Editor courtesy of DMXZone, which will allow our employees to format data through an intranet application that I built which updates our internet site. The RTE uses HTML tags to create the formatting, which looks great.
I'm also working on a new search engine through Verity, and it allows for searches of database tables. One of the database tables searched contains HTML tags (dbo.Minutes). When the user searches this DB table, they get the results they need, but the HTML tags show up along with the text. Once they click on the link to the details page, the HTML tags go away, because the dynamic page uses the HTML tags to format the text. I thought that if I used the JS Replace Method, that I could remove the extra tags for this page.
To see the search engine results page that I'm talking about, you can go to: http://search.douglas-county.com/query.html?rq=0&col=minutes&qt=+01%2F23%2F2002&charset=iso-8859-1&ql=
If you click the result for "01/23/2002", it will pull up a dynamic page that uses the DB data w/HTML tags.
I hope this helps. Any advice? Alternative ways to accomplish this would also be great. Thanks.
KWilliams
MichaelM
12-11-2003, 10:47 AM
First problem is that JavaScript is case sensitive, therefore tags.Replace(...
is very different (not to mention doesn't work) than
tags.replace(...
so if you change that line to read
tags.replace(...
then you won't get the error you're currently getting.
However, this won't fully solve the problem you're trying to solve.
The best suggestion that I have would be to use a Regular Expression in order to clean the displayed text of any HTML mark-up. It appears to me that the search engine software is already doing some level of "escaping" the text it reads from the DB, such that it writes out <[HTML_Tag]> instead of leaving it as HTML and displaying formatted text. There might be an option/preference in your search engine software that may help you avoid this problem all together. If not, then try doing something like:
strSR //the string variable holding the search results
strSR = strSR.replace(/\<\;\/{0,1}\w*[\s*\w*=\'*\""*.*\""*\'*]*\>\;/g, "");
Hope this helps...
ray326
12-11-2003, 10:49 AM
Can you be a little more structured in the way you're extracting that description? I.e., can you do it in such a way that every < will be guaranteed to have its > partner? Or is Verity just blindly slicing out x number of chars from the start of the file?
You'd really be better off stripping that stuff on the server side before incorporating it into the page.
kwilliams
12-11-2003, 10:53 AM
Hi MichaelM,
Concerning JS being very case-senstive, I know what you mean. And I'll make sure to make the appropriate change. Concerning the replacing of HTML tags for database searches, I think that I've figured out a solution with the help of your suggestion.
I'm going to remove the replace method from that page, and put it into the page that only searches DB's. Verity UltraSeek's Search Engine uses several pages with python & HTML code to search index a site and databases. I'm going to work on finding the page that is only used for DB searches, and add the JS Replace Method into that code. That way it won't affect the rest of the page, and will change only the DB results.
Hopefully this plan will work, and I'll let you know if it does. Thanks again for your help & advice.
kwilliams
12-11-2003, 10:55 AM
ray326,
How could you easily strip HTML from the DB table's data when using it on the site? And if I do this, wouldn't the HTML formatting not work on the dynamic page?
kwilliams
12-12-2003, 01:18 PM
Ok, I found the class that contains the search results "description", and it's called
So I created this script to replace any instances of <P> with an empty space.
<script language="JavaScript">
if (class = "description"){
var tags = "<P>";
tags.replace(tags,"");}
</script>
Unfortunately, it's not working. I also tried replacing the <P> with <P>, but it didn't change the result. Does anyone see something that I'm obviously doing wrong?
KWilliams
Pittimann
12-12-2003, 01:47 PM
Hi!
Two little things (in limegreen):
if (class == "description"){
var tags = "<P>";
tags=tags.replace(tags,"");
}
Cheers - Pit
kwilliams
12-15-2003, 10:33 AM
Ok,
I did a little research on possible other solutions, and I think that one mentioned by a previous post may work.
It concerned stripping the HTML tags from the results by using a JS Regular Expression, like this:
<script language="JavaScript">
<!--Regular expression which snips out anything within < and >
function stripHTML(oldString) {
return oldString.replace(/<[^>]*>/g, "");
}
//-->
</script>
The results for the DB table are contained within a CSS class named "description". I think that the above script is the answer, but I have a couple of questions on how I can make this method work for me:
1) How can I capture a CSS class named "description" into a JS variable?
2) How can I strip the HTML tags ONLY from the results under this same CSS class "description" when using the above script?
P.S. Here's the URL again for the search results page that uses "01/23/2002" as an example: http://search.douglas-county.com/query.html?rq=0&col=minutes&qt=+01%2F23%2F2002&charset=iso-8859-1&ql=
Thanks for any help.
KWilliams