Creating a bookmarklet in javascript

I am trying to make a bookmarklet for my site.

I made a php page that when sending a GET for example www.website.com/index.html?a=banana it will return echo 'stand';

Now I'm trying to make a bookmarklet, which when clicked on will be: Make a GET on the php page, then display everything that the echo was returned to the user in a small pop-up window that disappears after 3 seconds.

How can i do this?

Instapaper's bookmark letters do it ...

 javascript: function%20iprl5(){ var%20d=document,z=d.createElement('scr'+'ipt'),b=d.body,l=d.location; try{ if(!b) throw(0); d.title='(Saving...)%20'+d.title; z.setAttribute('src',l.protocol+'//www.instapaper.com/j/deyNbbpjuSei?u='+encodeURIComponent(l.href)+'&t='+(new%20Date().getTime())); b.appendChild(z); } catch(e){ alert('Please%20wait%20until%20the%20page%20has%20loaded.'); } } iprl5(); void(0) 
+4
source share
1 answer

A booklet booklet is a part of javascript that works in the area of ​​the page you are on.

It is not intended to call arbitrary URLs and cannot do this, as this would violate a similar origin practice. If your PHP can return JSONP, then you can write a bookmarklet into which the script will be inserted into the page that will run the returned script, which will also be able to display a popup

 javascript: function%20iprl5(){ var%20d=document; // shorten document object var z=d.createElement('scr'+'ipt'); // create a script tag var b=d.body; // get document.body var l=d.location; // get document.location - I would get document.URL instead try{ if(!b) throw(0); // if there is no body object available d.title='(Saving...)%20'+d.title; // set document.title z.setAttribute('src',l.protocol+'//www.yourserver.com/test.php?u='+encodeURIComponent(l.href)+'&time='+(new%20Date().getTime())); // create the script url b.appendChild(z); // append it to the body - I would append to head myself } catch(e){ // give an error alert('Please%20wait%20until%20the%20page%20has%20loaded.'); } } iprl5(); // call it void(0); // make sure it does not return a value to the window 

insert this view-source:http://www.instapaper.com/j/deyNbbpjuSei?u=http://www.stackoverflow.com in the location bar to see the amount of material returned as part of the page you are on.

+2
source

All Articles