Click to See Complete Forum and Search --> : finding a specific text value in a string


druss
03-17-2003, 03:06 AM
I want to find the value of a specific string but cannot seem to do it, for example
mySting = "my name = goran"
i need to find the string 'my name = '
and through that i need to get the value of 'my name'

so far this is all i got:

myString = "my name = goran"
theResult = myString.indexOf("my name = ")

dont know how to get the value and remember that 'goran' changes, it dosent stay the same value...


Thanks
Goran

gil davis
03-17-2003, 06:01 AM
<script>
myString = "my name = goran";
theResult = myString.indexOf("my name = ");
if (theResult != -1) theValue = myString.substring(theResult + 10);
alert(theValue);
</script>

pyro
03-17-2003, 07:32 AM
Or, you could use the split command and do it like this:

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

myString = "my name = goran"
val = myString.split("=");
alert (val[1]);

</script>

druss
03-17-2003, 10:59 PM
Hmm, for some reason it isnt working for me when its in frames. This is what i got, and it also is the frame page


----------------------------------
<HTML>

<script>
test = document.source.documentElement.outerHTML;
theResult = test.indexOf("hello=");
if (theResult != -1) theValue = test.substring(theResult + 10);
alert(theValue);
</script>

<frameset rows="60%,*">
<frame name="source" src="source.htm">
<frame name="test" src="Untitled1.htm">
</frameset>
</html>
------------------------------------



Thanks
Goran

gil davis
03-18-2003, 05:53 AM
First, you cannot access something until it has been loaded. Your script will execute before the browser even reads the frameset.

Second, frames are window objects, not document objects.test = document.source.documentElement.outerHTML;probably should be more liketest = window.source.document.body.outerHTML;but that gives you the object, not the object's contents.

Third, you changed the compare string without changing the offset.if (theResult != -1) theValue = test.substring(theResult + 10);should beif (theResult != -1) theValue = test.substring(theResult + 6);Try this:<HTML>
<script>
function init() {
theResult = source.document.body.innerHTML.indexOf("hello=");
if (theResult != -1) theValue = source.document.body.innerHTML.substring(theResult + 6);
alert(theValue);
}
</script>
<frameset rows="60%,*" onload="init()">
<frame name="source" src="source.htm">
<frame name="test" src="Untitled1.htm">
</frameset>
</html>