PDA

View Full Version : Help with Script that filters paragraph. If 1 word exists, write a warning



jpater74
12-21-2007, 10:01 PM
I'm looking to write a script that does the following:

1) It reviews a paragraph that is dynamically created via a tmp_Remarks tag.

2) If the phrase "short sale" or "shortsale" is present, Perform 2 actions:
A) Put a Red Asterik at the top of the page
B) at the bottom, repeat the asterik and a sentence describing what a "short sale " is.

So in other words, there are 100 HTML pages.
Each one has a unique paragraph describing a house

Most will be like this:

"This is a fabulous place, you should buy it..."

But on the rare occasion that it has "Short Sale" in it like

"This property is a short sale, please make an offer"

It would trigger the script and put an astrik on the top of the page and separately a description of what a "short sale" is at the bottom of the page.

Is this possible?
Thanks!
JP

mic2100
12-21-2007, 10:20 PM
Hi, if each of your tags have a id attribute assigned to them this should work,



<script>
function check_tag(id)
{

var tag = document.geElementById(id).innerHTML;
var tag_lower = tag.toLowerCase();

if(tag_lower.indexof("shortsale") == -1 && tag_lower.indexof("short sale") == -1)
{

//the text was not found in the document. Put all processing in here.

}
else
{

//the text as been found and the processing to inticate this should be placed here

}

}
</script>


I hope this helps you, but you will need to write the code for what you want it to do when it does/doesn't find the text within the if statements.

jpater74
12-22-2007, 12:03 AM
Awesome.
Sorry for the ignorance, but which is the variable that would turn into a paragraph?

I tried changing "id" to "This is a lovely home, come buy it" but that didn't work for me.

Also did I correctly write the IF then statement?


<script>
function check_tag(id)
{

var tag = document.geElementById(id).innerHTML;
var tag_lower = tag.toLowerCase();

if(tag_lower.indexof("shortsale") == -1 && tag_lower.indexof("short sale") == -1)
{

//the text was not found in the document. Put all processing in here.
document.write( "This is short sale, the bank will have to approved..." );
}
else
{

//the text as been found and the processing to inticate this should be placed here
document.write( "This is not short sale." );
}

}
</script>

Thanks!
JP

mic2100
12-22-2007, 01:16 AM
<script>

function init()
{

check_tag('check1');

}

window.onload = init;

</script>

<div id="check1">This is a shortsale</div>


You will have to add a function call for every paragraph you want to check, the id is the id attribute that is assigned to the tag which you want to check.

jpater74
12-22-2007, 05:02 PM
So I put it all together, and if I paste this code in, it should trigger the DocWrite right?

<script>

function init()
{

check_tag('check1');

}

window.onload = init;

</script>


<script>
function check_tag(id)
{

var tag = document.geElementById(id).innerHTML;
var tag_lower = tag.toLowerCase();

if(tag_lower.indexof("shortsale") == -1 && tag_lower.indexof("short sale") == -1)
{

//the text was not found in the document. Put all processing in here.
document.write( "Test This is short sale, the bank will have to approve..." );
}
else
{

//the text as been found and the processing to inticate this should be placed here
document.write( "This is not short sale." );
}

}
</script>

<div id="check1">This is a shortsale</div>
</body>
</html>

The page should then say:

This is a shortsale
Test This is short sale, the bank will have to approve...

Philip M
12-22-2007, 05:50 PM
Why not try it? That way you could learn something.

jpater74
12-22-2007, 06:12 PM
Thanks Philip,
I did try it. I spend 2 hours trying it.
I must be doing something wrong.
Frank

Philip M
12-22-2007, 06:24 PM
function check_tag(id) {
var tag = document.getElementById(id).innerHTML;


Javascript is case sensitive:-

if (tag_lower.indexOf("shortsale") == -1 && tag_lower.indexOf("short sale") == -1)

I think you will find that it works after those corrections have been made.

To recap:-

============================================
<script type = "text/javascript">

window.onload = init; // be careful not to have two onload events in your page

function init() {
check_tag("check1");
}

function check_tag(id) {

var tag = document.getElementById(id).innerHTML;
var tag_lower = tag.toLowerCase();

if ((tag_lower.indexOf("shortsale") == -1) && (tag_lower.indexOf("short sale") == -1)) {
//the text was not found in the document. Put all processing in here.
document.write( "Test This is short sale, the bank will have to approve..." );
}

else {
//the text as been found and the processing to indicate this should be placed here
document.write( "This is not a short sale." );
}
}
</script>

<div id="check1">This is a shortsale</div>

=======================================================


But is not the logic reversed?
//the text was not found in the document. Put all processing in here.
document.write( "Test This is short sale, the bank will have to approve..." );

Remember that document.write erases the current page and creates a new one. In other words it cannot be used to update the content of your page after that page has been loaded.