首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用javascript制作书签小程序

用javascript制作书签小程序
EN

Stack Overflow用户
提问于 2011-06-19 19:43:51
回答 1查看 1K关注 0票数 4

我正在试着为我的网站做一个书签。

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

现在我正在尝试制作一个书签小程序,当我按下它时,它将:转到php页面,然后在一个3秒后消失的小弹出窗口中显示返回给用户的任何回声。

我该怎么做呢?

Instapaper bookmarklet做到了.

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-19 19:58:15

bookmarklet是在您所在的页面范围内运行的一段javascript。

它并不意味着只调用任意的urls,也不能这样做,因为这将违反同源实践。如果您的PHP可以返回JSONP,那么您可以编写一个书签小程序,该书签小程序将在页面上插入一个脚本,该脚本将调用您返回的脚本-它也可以显示弹出窗口

代码语言:javascript
复制
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粘贴到地址栏中,以查看作为页面的一部分返回的内容的数量

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6402152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档