1

I have a site that will scrape for new data on the first page visit. I would like to use AJAX to do this, so that I can present the user with at least some loading.gifs during the scrape, but that is only if Javascript is enabled.
My site utilizes a PHP template engine, so I thought to put the scrape function in <noscript> tags in the html template. Since this would occur after all the PHP code, I would have to reload the page so I can render/parse the scraped data with PHP.

This method seems a little sloppy, I was wondering if there is an efficient way to do this.

paul B
  • 206
  • 3
  • 12

2 Answers2

1

Is there any reason why you need to do this with Javascript? Why not just do it with PHP, using cURL to get the content and other PHP extensions such as SimpleXML to get hold of the content? You could even cache the scraped content in a database table for a set amount of time so that each page load doesn't force PHP to do the scraping all over again.

Jeremy
  • 2,651
  • 1
  • 21
  • 28
  • The scrape is done exactly that way. My issue is how to scrape first and load the page if JS is disabled, or load first, then populate with ajax if JS is enabled – paul B Jan 16 '11 at 23:31
  • I may be missing the point of what you're trying to do but why bother with the Ajax loading if the scrape is being performed server side by PHP in the first place? If the data is being cached on your server as you say then there's no benefit to an AJAX load of the data. I may well be missing something though in what you're trying to do... – Jeremy Jan 17 '11 at 17:53
1

The <noscript> tag will only interpreted by the client (web browser) after the page has been sent, which means it's too late to trigger a php function. I would probably give up on the dual approach if I were you, but there might be some other (hacky) ways to accomplish this...

Maybe you could put an iframe in the <noscript> tag, whose src is no-js separate scraping script.
Or you could test js capability on a landing page, then send people to the page made for their setup.

Possible helpful links:
Check if JavaScript is enabled with PHP
Can I let PHP know that a user does not have javascript enabled?

Community
  • 1
  • 1
grossvogel
  • 6,694
  • 1
  • 25
  • 36