dqpdb82600 2011-06-19 11:43
浏览 91
已采纳

在javascript中制作书签

I am trying to make a bookmarklet for my website.

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

Now I am trying to make a bookmarklet that when I press it will: Do the GET to the php page, then display whatever the echo was back to the user in a small popup that disapears after 3 seconds.

How can I do this?

Instapaper bookmarklet does 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)
  • 写回答

1条回答 默认 最新

  • duanguochi6194 2011-06-19 11:58
    关注

    A bookmarklet is a piece of javascript that runs in the scope of the page you are on.

    It is not meant to just call arbitrary urls and cannot do so since that would be in violation of same origin practice. If your PHP can return JSONP, then you can write a bookmarklet that will insert a script on the page that envokes the script you return - that would be able to display a popup too

    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
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误