SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie: retrieve a given part of HTML code from a given URL

    I'm pretty new to javascript and I think that eventhough what I would like to do seems easy it is for me at the moment out of reach without help.

    I am searching for a function that simply retrieves the html code between two specified strings from a specified URL:
    Code:
    var thestring = function GetStringURL(theurl, string1, string2) {...} ;
    where 'theurl' is the webpage URL, 'string1' is the string of text to be found in the html code of 'theurl', and 'string2' is the first match to be found after 'string1'. Moreover this is supposed to work with greasemonkey.

    Example:
    Code:
    var thestring = function name ('http://www.google.com/', 'bgcolor=', ' text') {...} ;
    will look for the first match of 'bgcolor=' in 'http://www.google.com/', then will look for first match of ' text' after 'bgcolor' and it will return the string of text inbetweeen, i.e. in this case it will return '#ffffff'.


    Anyone has a hint on how to do that?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    javascript has a same domain policy that will prevent you from reading most data from another domain.

    If you create a serverside script on your own domain(eg, with php), which will proxy the request, then you could accomplish this.

  3. #3
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the quick answer.
    This policy is a shame... but I understand this should be the case for security reasons...
    You wrote about "domains" (not all other sites) and "most data" (not all data), does it mean it won't work as well if the main page is 'XXX.thedomain.com' and the page where the data has to be read from is 'YYY.thedomain.com' ?

    From the beginner guide I read that:
    JavaScript cannot establish any connection to whatever computer, except to download a new HTML page or to send mail. This, too, would create unacceptable hazards:
    and later:
    So JavaScript only works on things that are in HTML pages or part of the browser. You cannot influence anything that's not contained by the browser.
    So from the first quote it seems it would be possible to download a new HTML page, but from the second one it seems it wouldn't be possible?

  4. #4
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I think I'm a bit too late to edit the previous post and I have to post a new one...

    I think I now understand there's no contradiction at all in the two quotes above.

    Wouldn't it be possible instead to open up a new window, store the result in a global variable, and close the window (without losing all the values that were previously stored by the script in the previous page)?

    Or another way that would prevent from opening such a pop-up, open the target page in the same window, read off the string and store it, go back to the previous page (without losing anything from the previous step of the js code, which I guess may be a bit more problematic...) ?

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://en.wikipedia.org/wiki/Same_origin_policy

    If the window you open is not of the same origin, js won't let you access the dom, so you won't be reading anything into a variable. And navigating to the other page doesn't help you, because you don't have any of your javascript running in the other page.

  6. #6
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for the answer.
    I'll have to go for the serverside script as you wrote... thanks!


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
  •