سؤال

Can anyone help me out with a function in javascript which is for searching a site for words (like the ctrl+f function of browsers) and return a bolean if found?

For example on this site it would like this: searchText("Question"); It would search and find it on the top right and would return true.

Thanks already!

هل كانت مفيدة؟

المحلول

There is window.find() method doing exactly what you want. It finds a string in window:

if (find(stringToFind)) {
    alert('Found');
}

Found string will also be highlighted. This function is supported in all major browsers (IE7+).

https://developer.mozilla.org/en-US/docs/Web/API/Window.find

نصائح أخرى

Using jQuery you can do:

if ( $('body').text().search('YOUR TEXT') == -1)
    return false;
else 
    return true;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top