Click to See Complete Forum and Search --> : Find text string in a web page's html source code


llamatron
08-25-2003, 09:14 PM
Hi,

I need some javascript code that will put an id value into a variable. The tricky bit is getting the id number.
If i load up this web page and go to view/source it will say somewhere :

member_id=12345&

the id number can be of a variable length; the delimiter is the ampersand. I need to get that 12345 number from the html source code and somehow store it in a variable.

I'm very new (and rubbish) at javascript so over to the experts.......
Many, many thanks.

AdamBrill
08-25-2003, 09:38 PM
Try this:<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function run(){
test = document.documentElement.innerHTML.split('member_id=');
for(x=0; x<test.length; x++){
if(/\d+&/i.test(test[x])){
the_code = test[x].split('&')[0];
}
}
alert(the_code);
}
</script>
</head>
<body onload="run()">
member_id=12345&
</body>
</html>

pyro
08-25-2003, 09:56 PM
Since I made this... I might as well throw it in the topic.

<script type="text/javascript">
function getVals() {
code = document.getElementsByTagName("html")[0].innerHTML;
results = code.match(/member_id=(\d*)&/g);
for (i=0;i<results.length;i++) {
value = results[i].match(/member_id=(\d*)&/);
alert (value[1])
}
}
onload = getVals;
</script>

llamatron
08-25-2003, 10:13 PM
Fantastic! Worked a treat. You master of algortihms!!!