0

I would like to know a way to check if the webpage a href points to contains certain text using javascript.

<a href="/path/to/document.html" onclick="check_page()">click</a>

<script type="text/javascript">
    function check_page()
    {
        //check for text in /path/to/document.html        }
</script>
j08691
  • 204,283
  • 31
  • 260
  • 272
Ruth
  • 5,646
  • 12
  • 38
  • 45
  • Are you looking for text in the URL itself, or in the document that the URL points to? – Pointy Oct 19 '12 at 13:56
  • Text in the actual html file it points to – Ruth Oct 19 '12 at 13:57
  • 4
    if it is pointing to a webpage in the same domain you can use ajax to load the page as text and find what you are looking for – atar Oct 19 '12 at 14:00
  • 1
    Maybe this question helps: http://stackoverflow.com/questions/680562/can-javascript-read-the-source-of-any-web-page – Niels Oct 19 '12 at 14:07

1 Answers1

0

Try this:

<script>

function check_page(url){

if (url.href.indexOf("/path/to/document.html")>-1){ 
alert('error');
}
}
</script> 
<a href="/path/to/document.html" onclick="javascript: return check_page(this)">test</a>
Lo Juego
  • 1,305
  • 11
  • 12