SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search for a string in HTML source

    Hi,

    A simple query though I am not able to find a solution, thought someone here might help. Given an URL, how to read the html source of the page and search for a particular string? Everything should happen behind the scene and without any user interaction. I think it may be some simple string function, but have no clue how to go through the whole html page (most cases it would be a dynamically generated page, hence we have to read the html source after the page is generated.) Any help ?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    That's the sort of thing that server-side progamming can provide.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you're essentially looking to add search functionality to your site? ... If server-side is not an option you could achieve this with AJAX.

    Although I really would recomend server-side (like Paul) since searching is an integral part of any website and should not be left to the client-side.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Searching (i.e. finding an url based on the search string) is a server-side task, but once the page is found, you can use javascript to highlight the matching words in html. The code would be along the lines of
    Code:
    <script>
    function hilite(elem, words) {
    	elem.innerHTML = elem.innerHTML.replace(
    		new RegExp('\\b(' + words.join('|') + ')\\b(?=[^<>]*(<|$))', 'gi'),
    		'<span class="hilite">$&</span>');
    }
    onload = function() {
    	hilite(document.body, ['lorem', 'amet', 'test'])
    }
    </script>
    
    <style>
    .hilite { background: yellow }
    </style>
    
    Lorem ipsum dolor sit amet, <b id="test">consectetuer</b> adipiscing elit. test!
    hth

  5. #5
    SitePoint Member
    Join Date
    May 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for your responses but I'm still stuck. It's not exactly a search functionality I am looking at. What I exactly want is say - I have this url http://www.sitepoint.com/, Now I just want to scan the html source of this page (the homepage of the url) and look for a particular string(without loading teh page in the browser window). Is it not possible in java script (I mean without server side)?

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is possible in javascript. You'd have to use AJAX to load the contents of the page into a string and then "scan" through it for the required string.

    I would use JQuery's various AJAX functions to achieve this (because they're very simple) ( http://docs.jquery.com/Ajax )

    You'd have to download jQuery (just click download on that link above) - then you'd have to link the jQuery script like this:

    Code HTML4Strict:
    <script type="text/javascript" src="jquery.js"></script>

    Have a look at the "load()" function on the link above - I think that might be what ur after...
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think this would work across domains.

    kraftzy, no, this is not something you can do with javascript (at least browser-based, to be pedantic).

  8. #8
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you intend to do with the data? - Are you looking to count the occurances of that string and just display a number...?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  9. #9
    SitePoint Member
    Join Date
    May 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JimmyP View Post
    What do you intend to do with the data? - Are you looking to count the occurances of that string and just display a number...?
    Well, its for a firefox extension that I am designing. It requires checking a certain website for the occurance of a certain string and notify the user based on that. The website has no feed, so I thought I could load the html source and scan through it for the string. Anyway, I guess I'd have to find some other way now. Thanx everyone for responding :-)

  10. #10
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I've got the wrong end of the stick here but it seems possible to me.

    You can use AJAX to load the contents of a webpage and then look for the string in that. What do you want to happen if the string is present?
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kraftzy View Post
    Well, its for a firefox extension that I am designing.
    Oh, that's something you should have said from the start. Extensions are not subject to usual cross-site security restrictions, so I have to revoke my statement - what you described is perfectly possible.

  12. #12
    SitePoint Member
    Join Date
    May 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @JimmyP,
    AJAX? any hint on how? XMLHTTPRequest ?

    @stereofrog

    good to hear that. but then I am gonna need a little more help on how :-)

  13. #13
    SitePoint Member
    Join Date
    May 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ^^Hi Guys! Its done. I've used XMLHttpRequest - >responseText to load and read the entire html source of teh given URL. Thnx for all the support :-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •