← Desenvolvimento

Bookmark this site

Lida 1748 vezes

Offline

nandotx 
Membro
Mensagens 1012 Gostos 1
Feedback +9

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Mobile User Windows User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Alguém sabe algum código JavaScript para criar um botão para se adicionar o site aos favoritos?

Convém que funcione no Firefox, IE, Opera e Safari. :)

Já testei uns poucos mas nenhum de jeito! :(
Offline

muiomuio 
Membro
Mensagens 324 Gostos 0
Feedback +1

Troféus totais: 23
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

Colocas isto dentro de um ficheiro .js tipo favoritos.js

Código: [Seleccione]
function fav() {
var newT = document.createTextNode('Add to Favourites');
var s = document.getElementById('fav');
if (window.sidebar) {
s.appendChild(newT);
s.style.color = '#00f';
s.style.cursor = 'pointer';
s.onclick = function() {window.sidebar.addPanel(document.title,self.location,'')};
} else if (window.external) {
s.appendChild(newT);
s.style.color = '#00f';
s.style.cursor = 'pointer';
s.onclick = function() {window.external.AddFavorite(self.location,document.title)};
} else if (window.opera) {
s.appendChild(newT);
s.style.color = '#00f';
s.style.cursor = 'pointer';
s.onclick = function() {
     var e = document.createElement('a');
     e.setAttribute('href',self.location);
     e.setAttribute('title',document.title);
     e.setAttribute('rel','sidebar');
     e.click();
  }
}
}

var pageLoaded = 0;
window.onload = function() {pageLoaded = 1;}
function loaded(i,f) {
if (document.getElementById && document.getElementById(i) != null) f();
else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}
loaded('fav',fav);

Depois no html crias um div com a id fav <div id="fav"></div> e pronto.

Link: http://javascript.about.com/library/blufav.htm
Offline

muiomuio 
Membro
Mensagens 324 Gostos 0
Feedback +1

Troféus totais: 23
Trófeus: (Ver todos)
Super Combination Combination Topic Starter Poll Voter Level 4 Level 3 Level 2 Level 1 100 Posts 50 Posts

Ou então tens sempre a possibilidade de usar isto:


Código: [Seleccione]
function bookmarksite(title,url){
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}

No html metes
Código: [Seleccione]
<a href="javascript:bookmarksite('ADVICE', 'http://www.advice.pt')">Adicionar aos favoritos</a>
Este acho que funciona como é esperado :P
Offline

nandotx 
Membro
Mensagens 1012 Gostos 1
Feedback +9

Troféus totais: 28
Trófeus: (Ver todos)
Apple User Mobile User Windows User Super Combination Combination Topic Starter Poll Voter Level 5 Level 4 Level 3

Colocas isto dentro de um ficheiro .js tipo favoritos.js

Código: [Seleccione]
function fav() {
var newT = document.createTextNode('Add to Favourites');
var s = document.getElementById('fav');
if (window.sidebar) {
s.appendChild(newT);
s.style.color = '#00f';
s.style.cursor = 'pointer';
s.onclick = function() {window.sidebar.addPanel(document.title,self.location,'')};
} else if (window.external) {
s.appendChild(newT);
s.style.color = '#00f';
s.style.cursor = 'pointer';
s.onclick = function() {window.external.AddFavorite(self.location,document.title)};
} else if (window.opera) {
s.appendChild(newT);
s.style.color = '#00f';
s.style.cursor = 'pointer';
s.onclick = function() {
     var e = document.createElement('a');
     e.setAttribute('href',self.location);
     e.setAttribute('title',document.title);
     e.setAttribute('rel','sidebar');
     e.click();
  }
}
}

var pageLoaded = 0;
window.onload = function() {pageLoaded = 1;}
function loaded(i,f) {
if (document.getElementById && document.getElementById(i) != null) f();
else if (!pageLoaded) setTimeout('loaded(\''+i+'\','+f+')',100);
}
loaded('fav',fav);

Depois no html crias um div com a id fav <div id="fav"></div> e pronto.

Link: http://javascript.about.com/library/blufav.htm

O único problema é o que já me tinha acontecido antes, no Firefox ele adiciona aos favoritos na boa, mas quando vais a abrir o site através dos favoritos ele abre numa sidebar. :(
Offline

goodsound 
Membro
Mensagens 2485 Gostos 0
Troféus totais: 31
Trófeus: (Ver todos)
Super Combination Combination Topic Starter 50 Poll Votes 10 Poll Votes Poll Voter Poll Starter Level 5 Level 4 Level 3

Tenta este.
Ou então tens sempre a possibilidade de usar isto:


Código: [Seleccione]
function bookmarksite(title,url){
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}

No html metes
Código: [Seleccione]
<a href="javascript:bookmarksite('ADVICE', 'http://www.advice.pt')">Adicionar aos favoritos</a>
Este acho que funciona como é esperado :P
Já usei algo parecido antes, e resultou.