PDA

View Full Version : Placing the current URL in a bookmark



DIYGUY
03-13-2009, 02:43 PM
I am brand new here and to JS ~ if this is the wrong forum for this type of post I will gladly take it elsewhere!

I would like to be able to have a user capture the page they are viewing quickly and efficiently into a bookmark. Bouncing around the net I found several different scripts where you could hardcode a bookmark - for example, a start page. What I want a user to do is something like this, navigate down to: http://www.example.com/firstpage/secondpage/thirdpage.htm and let the reader place that URL into their bookmarks, by clicking on an image perhaps.

This is my current approach (which is not working and thus why I am here). I have a Jscript dir sitting under my Code dir. In that Jscript dir I have the following script: "bookmark.js" and it contains this code:

function addbookmark() {
title = "document.title";
url = "window.location.href";
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( window.external ) {
window.external.AddFavorite( url, title); }
}
if (window.external) {
document.write('<a href =
"javascript:addbookmark()");">Add to Favorites</a>');
} else if (window.sidebar) {
document.write('<a href =
"javascript:addbookmark()");">Bookmark Page</a>');
}

In the head section of pages using this function:

<script language="JavaScript" type="text/javascript" src="Jscript/bookmark.js"></script>

This is the XHTML in the actual pages that use this function:

<a href="javascript:addbookmark(document.title, window.location.href)"><img src="images/bookmark.gif" /></a>

All of the above is pretty much cobbled together from bits and pieces I found. I don't pretend to be a programmer, nor JS literate. Any pointers you could offer me to make this work are muchly appreciated ...

magicyte
03-13-2009, 10:22 PM
Maybe change this:


function addbookmark() {
title = "document.title";
url = "window.location.href";
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( window.external ) {
window.external.AddFavorite( url, title); }
}
if (window.external) {
document.write('<a href =
"javascript:addbookmark()");">Add to Favorites</a>');
} else if (window.sidebar) {
document.write('<a href =
"javascript:addbookmark()");">Bookmark Page</a>');
}

to this:


function addbookmark() {
title = document.title;
url = window.location.href;
if (window.sidebar) {
window.sidebar.addPanel(title, url, "");
} else if (window.external) {
window.external.AddFavorite(url, title);
}
}
if (window.external) {
document.write('<a href="javascript:addbookmark();">Add to Favorites</a>');
} else if (window.sidebar) {
document.write('<a href="javascript:addbookmark();">Bookmark Page</a>');
}

DIYGUY
03-19-2009, 11:40 AM
Tried your suggestion Chris and thank you very much for your thoughts. Sadly, it did not change anything. What started me down this path is that I had some code left over from an earlier rev of the app I am building and it worked fine in FireFox, but not in IE. Thus, I went on a search for something that would work in either world.

Does anyone have something to share that might help me out?

speedy18us
03-28-2009, 08:08 AM
i hope this one will help you (i've test it in IE 6, FF 3 and Opera and it works):

1.put this on the head of your page:


<script type="text/javascript">
<!--
title = document.title;
url = window.location.href;
function CreateBookmarkLink() {
if( document.all ) {
window.external.AddFavorite(url,title);
} else if (window.sidebar) {
window.sidebar.addPanel(title,url,"");
} else if(window.opera && window.print) {
return true;
}
}
//-->
</script>

2.put this one in the body of your document:


<script language="JavaScript" type="text/javascript">
<!--
if (window.opera && window.print) {
document.write('<a rel="sidebar" href="' + url + '" title="' + title + '" class="favorites">Add to favorites</a>');
} else if (document.all) {
document.write('<a href="#" onclick="CreateBookmarkLink(); return false;" title="' + title + '" class="favorites">Add to favoritese</a>');
} else if (window.sidebar) {
document.write('<a href="#" onclick="CreateBookmarkLink(); return false;" title="' + title + '" class="favorites">Add to favorites</a>');
}
//-->
</script>

SOME TIPS:
-you should not access the file directly (the adress would be like "file:///C:/Documents and Settings/..."), it won't work.
-if you are still in doubt, checkout this adress: http://servicecalculatoare.z1.ro/