Load a js image - javascript

I am writing an Android RSS reader. It uses webview to display each article. To identify the most read news I load a fake image from my server like:
<img src='http://example.com/update_read_count.php?article_id=1234'>
At the server I will increment the view counter.
Is there a way I can respond back with the number of times it has been read. Or is there a way I can update a DOM element inject this count in the page?

Since it is an image, the only way you could do that would be to generate an image containing the text you wanted on the server. e.g. with this function.
If you wanted to update the DOM, you'd need to get the data in a more useful format. Using XMLHttpRequest to fetch the data and then returning it in JSON format would be traditional.

Related

Contentful api - getting images

I am new to contentful API but so far getting content from the API has been pretty straight forward. I have created a new space using their "blog" template and I see that in the "body" field there is an "insert media" button. I don't think I get how this is supposed to be used. When I insert an image into the "body" field, it generates a code that doesn't get rendered when I pull the content form the API. I am using a markdown parser to render the text. If you create an entry with images, these images will be available as an asset. Do I need to make a separate API call for every asset I want rendered with my entry?
When you use the Insert Media button, it should generate something such as:
![Lewis Carroll](//images.contentful.com/zz2okzf5k4px/2ReMHJhXoAcy4AyamgsgwQ/ec4998388330a939288c04558c57477a/lewis-carroll-1.jpg)
That url points to the image directly, so you don't need to do any extra calls to get the asset. The asset is an entity which contains metadata as well as the url to the asset file itself, but in this case you already have that url.
You said you are rendering the Markdown, maybe there's a problem in the code that gets generated? Could you post that?

Live updating from JSON

I have a JSON file which is dynamically and contain match info including an unique id. The JSON is divided into 3 arrays live, upcoming and recent. Since i'm quite new to Javascript i'm wondering what would be the best way to go in order to make this livescore script. I need it to be updating without refreshing browser? What is my options? Maybe someone has a snippet?
The JSON is automatically updates through another script which is connected to a cron job, so the script does not need to do anything regarding the JSON. Only retrieve and show the data.
I'm using dreamhost, which gives me access to shell, so websockets and so on is an option.
You'll need a jquery user to give you a snippit for this one, but in vanilla ecmascript 6, you use an XMLHttpRequest object to get the JSON from your server. This object can request data from the server asynchronously and is triggered by the client/browser so you can update the live match info when and as often as you like. You would just have to write a function to replace the data on the webpage with the new info when it is updated.

Is fetching remote data server-side and processing it on server faster than passing data to client to handle?

I am developing a web app which functions in a similar way to a search engine (except it's very specific and on a much smaller scale). When the user gives a query, I parse that query, and depending on what it is, proceed to carry out one of the following:
Grab data from an XML file located on another domain (ie: from www.example.com/rss/) which is essentially an RSS feed
Grab the HTML from an external web page, and proceed to parse it to locate text found in a certain div on that page
All the data is plain text, save for a couple of specific queries which will return images. This data will be displayed without requiring a page refresh/redirect.
I understand that there is the same domain policy which prevents me from using Javascript/Ajax to grab this data. An option is to use PHP to do this, but my main concern is the server load.
So my concerns are:
Are there any workarounds to obtain this data client-side instead of server-side?
If there are none, is the optimum solution in my case to: obtain the data via my server, pass it on to the client for parsing (with Javascript/Ajax) and then proceed to display it in the appropriate form?
If the above is my solution, all my server is doing with PHP is obtaining the data from the external domains. In the worst (best?) case scenario, let's say a thousand or so requests are being executed in a minute, is it efficient for my web server to be handling all those requests?
Once I have a clear idea of the flow of events it's much easier to begin.
Thanks.
I just finish a project to do the same request like your req.
My suggestion is:
use to files, [1] for frontend, make ajax call to sen back url; [2] receive ajax call, and get file content from url, then parse xml/html
in that way, it can avoid your php dead in some situation
for php, please look into [DomDocument] class, for parse xml/html, you also need [DOMXPath]
Please read: http://www.php.net/manual/en/class.domdocument.php
No matter what you do, I suggest you always archive the data in you local server.
So, the process become - search your local first, if not exist, then grab from remote also archive for - 24 hrs.
BTW, for your client-side parse idea, I suggest you do so. jQuery can handle both html and xml, for HTML you just need to filter all the js code before parse it.
So the idea become :
ajax call local service
local php grab xm/html (but no parsing)
archive to local
send filter html/xml to frontend, let jQuery to parse it.
HTML is similar to XML. I would suggest grabbing the page as HTML and traversing through it with an XML reader as XML.

Getting a file out of a local web page

This is what I'd like to achieve from a local page:
Enter data in a textbox
Transform the data (the outcome may be not a text file)
Get the transformed data back.
Directly writing on a local file is clearly out of question for security reasons. I know HTML5 has a FileWriter API but it's not supported on many browsers (and I think for a good reason).
I thought about creating the data as the content of one of the page elements (say a <DIV>) but then I am at loss on how to send that data back.
In essence, I feel I had to mimic the usual http request/response process while always remaining on the client side.
I start thinking that this is not possible at all, any suggestion?
One way that would take you near your desired outcome is by using data: URI scheme (http://en.wikipedia.org/wiki/Data_URI_scheme)
window.open("data:text/plain;charset=utf-8,"+textToPrint);
That opens a new tab with the text you want to save, you just need to click save or ctrl+s to save the text in a .txt file.

How to retrieve images from database using jquery and servlet

I need to retrieve the images which is stored in my oracle 11g R2 database. I'm trying to retrive the images using Jquery and servlet, And placing into the css division. But I'm a newbie in this. I know using json objects we can do this stuff. Please anyone tell me how to do
Make a request that returns a list of the names of all images (so that you know what to retrieve)
Make a servlet that serves images - gets the name as GET parameter, looks it up in the database (e.g. via JDBC) and streams the result back into the response.getOutputStream(), and set the correct content type

Categories

Resources