2011-06-19 42 views
4

我正在尝试为我的网站制作小书签。在javascript中制作小书签

我做了一个PHP页面发送例如www.website.com/index.html?a=banana一个GET时,它会返回echo 'stand';

现在我想做一个书签,当我按下它会: 执行GET到PHP页面,然后在3秒后消失的小弹出窗口中显示回显信息。

我该怎么做?

Instapaper的书签,做它...

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) 

回答

2

一个书签是一段JavaScript代码,在你的页面的范围内运行。

这并不意味着只是调用任意网址,因为这会违反同源的做法。如果你的PHP可以返回JSONP,那么你可以写一个书签,将插入envokes返回该脚本的网页上的脚本 - 这将是能够显示一个弹出太

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 

粘贴view-source:http://www.instapaper.com/j/deyNbbpjuSei?u=http://www.stackoverflow.com到位置酒吧看到作为页面的一部分返回的东西的数量

+0

如果这是真的,怎么instapaper可以做到这一点?他们给一个书签,你点击,它确实是这样的... – David19801

+0

我相信他们做我的建议。张贴小书签,我可以看到。花式是一样的 – mplungjan

+1

@ david19801 instapaper可能会使用JSONP作为@mplungjan提到。阅读AJAX和JSONP! – BGerrissen