簡體   English   中英

用JavaScript制作一個書簽

[英]Making a bookmarklet in javascript

我正在嘗試為我的網站制作一個書簽。

我已經制作了一個php頁面,當發送GET ,例如www.website.com/index.html?a=banana ,它將返回www.website.com/index.html?a=banana 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)

小書簽是在您所在頁面范圍內運行的一段javascript。

這並不意味着只能調用任意網址,而不能這樣做,因為這將違反相同的來源慣例。 如果您的PHP可以返回JSONP,那么您可以編寫一個書簽,該書簽將在頁面上插入一個腳本,該腳本將調用您返回的腳本-也可以顯示彈出窗口

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粘貼到位置欄中,以查看作為您所在頁面一部分返回的內容數量view-source:http://www.instapaper.com/j/deyNbbpjuSei?u=http://www.stackoverflow.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM