Bookmarking with javascript - javascript

I want to make a bookmark of my webpage by using javascript code. I want a javascript code to make a bookmark that should be opened in a new window.
I am using firefox.

Use this function:
function bookmark_us(url, title){
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);
}

In Firefox there is currently no way to do this (and there are bugs filed in bugzilla tracking this defect)
By "no way to do this" I mean that you can use the function Dreas gave but you will be limited to adding a bookmark that will default to the sidebar. The end user will have to manually un-check the "open in sidebar" option.

Ask on one of the Mozilla user groups (probably mozilla.dev.tech.xul or mozilla.dev.tech.xpcom). You could conceivably create the right XPCOM object to access the browser's bookmark manager -- this requires enabling privileges (the user will have to explicitly approve this). Not portable to other browsers (obviously) and perhaps not a great way of doing this.
Most websites I've seen that facilitate bookmarking (Jesse Ruderman's bookmarklet page is a great example) just ask the user to drag the appropriate bookmark to the bookmark folder of their choice, and this seems so much more straightforward/simpler than trying to automatically add something to the bookmarks.

function bookmark_us(url, title){
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);
}
When used with:
Bookmark Us
The above function no longer works as desired in Opera 9.x. It simply navigates to www.yahoo.com.
To make a link that pops up the Add Bookmark dialog in Opera 9.x, use the following:
function bookmark_us(url, title) {
if (window.sidebar && window.sidebar.addPanel) // firefox
window.sidebar.addPanel(title, url, "");
else if (window.external && 'undefined' != typeof window.external.AddFavorite) // ie
window.external.AddFavorite(url, title);
}
Bookmark Us

Related

One link for Firefox and Chrome add-ons?

I have add-on for Chrome and Firefox, they are same just for different browsers. Is it possible to have one link/button for the add-on, once user clicks it, it will be recognized which browser user is on and opens Chrome vs Firefox add-on.
It would be possible to have in html 2 buttons and hide one for each browser, but I would like to have just one button.
Thanks!
You can extract browser name from navigator.userAgent then set needed link
Add-on Link
<script>
var link = document.querySelector("#link");
var userAgent = navigator.userAgent;
if (userAgent.indexOf("Firefox") > 0) {
link.setAttribute("href", "https://www.mozilla.org");
} else if (userAgent.indexOf("Chrome") > 0) {
link.setAttribute("href", "https://google.com");
}
</script>

Redirect Safari To Chrome

I'm trying to redirect user's of my mobile webapp to use Chrome rather than Safari. I tried using the following:
<script>
javascript:location.href="googlechrome"+location.href.substring(4);
</script>
However this constantly opens tabs in Chrome in a loop. Do you know how I can successfully do this?
Cheers,
Dara
This will cause the page to open every time the webpage is loaded, regardless if you are in Safari or Chrome. This is also very poor User Experience to just forward the user to another browser without their input.
It would be better to have some way for the user to open your site in Chrome and also to have an explanation why it is needed.
There are other schemes for https and callbacks: https://developer.chrome.com/multidevice/ios/links#uri_schemes
<p>This webapp is best viewed in Google Chrome</p>
<button type="button" onclick="openInChrome()">Open in Chrome</button>
<script>
var openInChrome = function() {
if (/^((?!Chrome).)*(Safari)+.*$/.test(navigator.userAgent)) {
var protocol = location.href.substring(0,location.href.indexOf(':'));
var url = location.href.substring(location.href.indexOf(':'));
if (protocol === "http") {
location.href = "googlechrome" + url;
}
else {
location.href = "googlechromes" + url;
}
}
}
</script>
Edit:
Added a check to verify they are in Safari.
Well, the reason is pretty obvious; Chrome is instructed to open Chrome too. You just want a userAgent conditional.
if (navigator.userAgent.indexOf("CriOS") == -1) {
location.href="googlechrome"+location.href.substring(4);
}
I would go on my standard rant about user agent checking being bad, but I trust what you're saying about this being a private webapp. Since iOS doesn't let you change your default browser, I guess this is a fair workaround.

How to programmatically add list of web-pages to browser favourites on click?

I currently have a list of useful webpages that I am adding to my web-page. I would like to add a link next to each URL that will add it as a bookmark in the users browser. How can I do this?
Further to this, how can I add a "Bookmark all links" button to my page ?
Bookmarking for the user isn't supported by some major browsers. You might want to just let the users do it themselves. If you insist, however, this post has some code Bookmark on click using jQuery
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("a.jQueryBookmark").click(function(e){
e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
var bookmarkUrl = this.href;
var bookmarkTitle = this.title;
if (window.sidebar) { // For Mozilla Firefox Bookmark
window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
} else if(( window.external && window.external.AddFavorite) || document.all) { // For IE Favorite
window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
} else if(window.opera) { // For Opera Browsers
$("a.jQueryBookmark").attr("href",bookmarkUrl);
$("a.jQueryBookmark").attr("title",bookmarkTitle);
$("a.jQueryBookmark").attr("rel","sidebar");
} else { // for other browsers which does not support
alert('Your browser does not support this bookmark action');
return false;
}
});
});
</script>
This Code is taken from Developersnippets!
Chrome does not support such actions, since the security level could be broken.

Is there a way to request permission in IE to use window.external.AddFavorite?

The JavaScript security settings on Internet Explorer for my customers disallow using window.external.AddFavorite, and it generates (best case) an error in the status bar when the users click the "Add Bookmark" link on my website. Is there a way to explicitly request permission to use the window.external.AddFavorite method from the user in Internet Explorer, when the security settings don't allow use of the rest of the window.external methods?
EDIT
Here's the code I'm working with:
<script type="text/javascript">
function addToFavorites() {
if (window.sidebar) { // Mozilla uses sidebar
window.sidebar.addPanel( document.title, window.location , "");
} else if (window.external) { // IE uses window.external
window.external.AddFavorite( window.location, document.title );
} else { // Who knows ? Only have to support IE & Moz anyhow.
alert("Sorry! Your browser doesn't support this function.");
}
}
</script>
Bookmark This Page
It will work, but it HAS to be triggered by a user driven event. (e.g. the onclick of a link/button)
This is to stop spam/adware/pr0n sites from auto stuffing your bookmarks with garbage.
I'd just pull the "Add Bookmark" link off the site. Users know how to do that if they really want to.

A Firefox javascript bookmarking problem

I'm using the following JavaScript code:
<script language="JavaScript1.2" type="text/javascript">
function CreateBookmarkLink(title, url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( window.external ) {
window.external.AddFavorite( url, title); }
else if(window.opera && window.print) {
return true; }
}
</script>
This will create a bookmark for Firefox and IE. But the link for Firefox will show up in the sidepanel of the browser, instead of being displayed in the main screen. I personally find this very annoying and am looking for a better solution. It is of course possible to edit the bookmark manually to have it not show up in the side panel, but that requires extra steps. I just want to be able to have people bookmark a page (that has a lot of GET information in the URL which is used to build a certain scheme) the easy way.
I'm afraid that it might not be possible to have Firefox present the page in the main screen at all (as Googling this subject resulted in practically nothing worth using), but I might have missed something. If anyone has an idea if this is possible, or if there's a workaround, I'd love to hear about it.
For Firefox no need to set any JavaScript for the bookmark an page by script, only an anchor tag with title and rel="sidebar" can do this functionality
Bookmark This Page
I have tested it on FF9 and its working fine.
When you click on the link, Firefox will open an dialog box New Bookmark and if you wish to not load this bookmark on side bar then un-check Load this bookmark in the sidebar from dialog box.
I think that's the only solution for Firefox... I have a better function for that action, it works even for Opera and shows a message for other "unsupported" browsers.
<script type="text/javascript">
function addBookmark(url,name){
if(window.sidebar && window.sidebar.addPanel) {
window.sidebar.addPanel(name,url,''); //obsolete from FF 23.
} else if(window.opera && window.print) {
var e=document.createElement('a');
e.setAttribute('href',url);
e.setAttribute('title',name);
e.setAttribute('rel','sidebar');
e.click();
} else if(window.external) {
try {
window.external.AddFavorite(url,name);
}
catch(e){}
}
else
alert("To add our website to your bookmarks use CTRL+D on Windows and Linux and Command+D on the Mac.");
}
</script>
You have a special case for
if (window.sidebar)
and then a branch for 'else' - wouldn't firefox land in the first branch and hence only add the panel?
Hojou,
It seems that is the only way to add a bookmark for Firefox. So FF needs to land in the first branch to have anything happening at all. I Googled some more but I'm really getting the idea this is impossible to properly address in FF...

Categories