Unobtrusive Bookmark Script Works Too Well - Not A Good Thing

Discussion in 'JavaScript' started by Jules Manson, Dec 23, 2005.

  1. #1
    My crossbrowser bookmark script does not wait for the onClick event to activate. The bookmark dialog box opens immediately when the page loads. It will not work at all if I remove the onload="bookFav()" from the BODY tag. It is supposed to wait for an onClick mouse event. My intent in writing this as I did is to completely seperate behavior from presentation and structure. My goal is to eventually link this script externally via a common javascript library file. I tested it with Internet Explorer 6.0 on Windows Me. Please, no solutions which require adding inline javascript to my bookmark link. I already know how to do that. I need to make this unobtrusive so that nonprogrammers can easily edit the HTML. This script will also serve as a model for many other unobtrusive scripts which I have planned. Please examine my script and see why it is not waiting for the onClick event. Thank you for your help.

    <HTML><HEAD>
    <TITLE>UNOBTRUSIVE BOOKMARK SCRIPT WORKS TOO WELL, NOT A GOOD THING</TITLE>
    <SCRIPT>
    function bookFav() {
    if (!document.getElementById || !document.all) /* object detection to prevent errors */
    {alert("Please use your browser menu to bookmark us!");} /* alert for non-supporting browsers */
    else {
    var el = document.getElementById("bookmark");
    myURL = window.location.href; /* gets url of page to bookmark */
    myTitle = document.title; /* gets title of page */
    el.onClick = addFav(myURL,myTitle); /* this event is supposed to open the bookmark dialog */
    }
    }
    function addFav(aURL,aTitle){
    if (document.all) /* is this MSIE? */
    {window.external.AddFavorite(aURL,aTitle);} /* if yes then call bookmark dialog for MSIE */
    else if(window.sidebar) {window.sidebar.addPanel(aURL,aTitle,"");} /* else call bookmark dialog for NS&FF */
    return false;
    }
    </SCRIPT>
    </STYLE>
    </HEAD>
    <BODY onload="bookFav();">
    Please <A id="bookmark" href="#">bookmark</A> this site.
    </BODY>
    </HTML>
     
    Jules Manson, Dec 23, 2005 IP
  2. Sham

    Sham Peon

    Messages:
    136
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can create a page event handler using javascript, which catches any click event.

    You can then get the id of the html node that was clicked using the the DOM. Then you can get the inner text of the node, and if it is "bookmark" for example, you can call a particular javascript method. All this without adding any javascript to the file. Of course, it needs to be included in the page somewhere, and you will need to create an array of actions to take, for each given link (identified by it's innertext)...

    no?
     
    Sham, Dec 23, 2005 IP
  3. Jules Manson

    Jules Manson Peon

    Messages:
    20
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Your solution sounds promising and a bit advanced for me. I just don't know how to go about it yet. Can you be more specific or better yet make this script work then I could use your solution as a model for future scripts?
     
    Jules Manson, Dec 23, 2005 IP
  4. Jules Manson

    Jules Manson Peon

    Messages:
    20
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I decided to rewrite my request with a few helpful links which demonstrate what i am looking for. Please follow this request instead:

    I want the code in this:

    http://members.aol.com/oysteinramfjord/bookmark1.html

    to behave like this:

    http://members.aol.com/oysteinramfjord/bookmark2.html

    without adding any javascript inside any tags whatsoever. All javascript must exist inside the head section of the HTML file. Both were tested with Firefox 1.5 and MSIE 6.0 with Windows Me (yes I know I am laughing too). It is more important for this script to work on MSIE.

    Please use the souce code in bookmark1.html as your solution template. Thank you for considering this problem.
     
    Jules Manson, Dec 24, 2005 IP
  5. FeelLikeANut

    FeelLikeANut Peon

    Messages:
    330
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I never understood bookmark scripts. I've got an add to favorites already, positioned very conveniently on my browser tool bar. Why is all this effort put in to creating another?
     
    FeelLikeANut, Dec 27, 2005 IP
  6. rederick

    rederick Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah, I'm not sure if you can do this without calling the javascript function onload in the body tag or elsewhere in the page. Using the js console the error is that "the element has no properties" when using the onload within the head.

    You do not want to use the <body onload="bookmark()"> ?

    Using this line to change the href on the link instead of the onclick seems to work.
    document.getElementById("bookmark").href = "javascript:addFav(url,title)";
    Code (markup):

    oh, i'm using firefox and I get the browser not supported error on you page, is the check nessesary?

    Hope this helps at all

    Red.
     
    rederick, Dec 29, 2005 IP