jcript to book,ark page

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Seen

    #1

    jcript to book,ark page

    I'm fairly new to javascript so please forgive my errors. I want to
    put a statement on a web page to let the user bookmark the page. I've
    seen several different versions of this - but I can't make mine work
    in IE. Here is the code:


    <script language="JavaS cript" type="text/javascript">
    <!--
    function cknav(){
    var IEstring = "<a
    href='javascrip t:window.extern al.addFavorite( 'http://la.fooweb.com', 'LA
    Homepage')";
    IEstring += "><\/a>" ;
    // IEstring += ";' title='Add to Favorites '><\/a>" ;
    // IEstring += "(location.href ,document.title );' title='Add to
    Favorites'>Favo rites<\/a>" ;
    var NSstring = "Click [Ctrl + D] ";
    var OPstring = "Click [Ctrl + T]";
    var NSOPstring = "by returning to the main page and "
    var OTHstring = "Bookmark this page!"
    var whichString = OTHstring ;
    var agt = navigator.userA gent.toLowerCas e();
    var app = navigator.appNa me.toLowerCase( );
    var ieAgent = agt.indexOf('ms ie');
    var nsAgent = app.indexOf('ne tscape');
    var opAgent = app.indexOf('op era');
    // How do I get IE to execute this ?? it doesn't seem to do
    anything.
    if (ieAgent!= -1) { whichString = IEstring;
    document.write( whichString);
    alert(whichStri ng) }
    else if (nsAgent!= -1){ whichString += NSOPstring + NSstring;
    alert(whichStri ng) }
    else if (opAgent!= -1){whichString += NSOPstring + OPstring;
    alert(whichStri ng) }
    }
    -->
    </SCRIPT>

    </head>

    <body>
    <table width="80%" frame="border" cellspacing="3" cellpadding="3"
    border="4" bgcolor="#fffff f" align="center" summary="Statem ent">
    <tbody><tr>
    <td>
    <br />
    <!-- Try One - IE only , gives an eror -->
    <h1 class="c1"><fon t size="+2" color="#ff0000" ><b>New Web
    Address.</b></font><br /><br />
    <font size="+1" color="#000000" > If you got here by the
    http://nu.net/lanet link, you were redirected the NEW home
    of LA http://la.fooweb.com. <U>
    <span style='color:re d;cursor:hand;' >
    <a
    href="javascrip t:window.extern al.AddFavorites ('HTTP://URL.COM','SITEN AME')">Bookmark
    Me</a>
    </span></u>
    <!-- Try 2 - Great for non IE but need to know how to make IE do the
    bookmark in the script -->
    <br />
    <h1 class="c1"><fon t size="+2" color="#ff0000" ><b>New Web
    Address.</b></font><br /><br />
    <font size="+1" color="#000000" > If you got here by the
    http://nu.net/lanet link, you were redirected the NEW home
    of LA <a
    href="http://la.fooweb.com"> http://la.fooweb.com</a>. <U>
    <span style='color:re d;cursor:hand;'
    onClick=cknav() >Bookmark this address.</span></u>
    </font><br />
    </h1>
    </td>
    </tr>
    </tbody>
    </table><!-- End of page content -->
    </body>
    </html>

    TIA
    Stephen
  • Fred Oz

    #2
    Re: jcript to book,ark page

    Seen wrote:[color=blue]
    > I'm fairly new to javascript so please forgive my errors. I want to
    > put a statement on a web page to let the user bookmark the page. I've
    > seen several different versions of this - but I can't make mine work
    > in IE. Here is the code:
    >
    >
    > <script language="JavaS cript" type="text/javascript">[/color]

    The language attribute on script tags has been depreciated, just use
    type.
    [color=blue]
    > <!--[/color]

    There is no need to hide scripts, don't bother.

    [color=blue]
    > function cknav(){
    > var IEstring = "<a
    > href='javascrip t:window.extern al.addFavorite( 'http://la.fooweb.com', 'LA
    > Homepage')";
    > IEstring += "><\/a>" ;[/color]

    Your quotes seem unbalanced, but I can't test right now. Also, there
    appears to be no text between the <a ...></a> tags, so even if it does
    work and you do create a link in the page, you likely can't see it
    anyway. And I think the capitalisation of "addFavorit e" should be
    "AddFavorit e".

    Try:

    var IEstring = "<a href=\"javascri pt:window.exter nal.AddFavorite ('"
    + "http://la.fooweb.com', 'LA Homepage')\""
    + ">Click to add LA Homepage to your favourites<\/a>";

    Adding "links" this way is considered an improper use of the <a> tag,
    which should load new content. Why not just use a button?

    [...]

    Why bother with all the browser detection?

    Have you considered writing help text to the page, and if you detect
    that AddFavorite is going to work, replace the text with a button that
    will add the link?

    In the HTML put:

    <span id="addBookmark Text" class="someClas s">To add a
    bookmark for this page in IE or Netscape, press Ctrl+D.
    If you are using Opera, press Ctrl+T</span>

    Then lower in the page (or fired from an onload) put:

    <script type="text/javascript">
    if (window.externa l.AddFavorite) {
    if (document.getEl ementById) {
    var ele = document.getEle mentById("addBo okmarkText");
    } else {
    var ele = document.all["addBookmarkTex t"];
    }
    var eStr = "<button "
    + "onclick=\"wind ow.external.Add Favorite(...);\ ""
    + ">Add page to favorites<\/button>";
    ele.innerHTML = eStr;
    }
    </script>

    Now any browser that supports AddFavorite() can take advantage of it.
    And if MS stops supporting it, the button won't appear.

    Again, untested as I don't have IE right now.

    [...][color=blue]
    > <h1 class="c1"><fon t size="+2" color="#ff0000" ><b>New Web[/color]

    Using <h1> this way is not how it's intended to be used. I don't know
    what you've defined in the class c1, however it seems that you are
    completely redefining the h1 tag, so why use it? Just put the
    appropriate attributes in the c1 class, or add them inline with
    style="...".

    <font> has been depreciated for years, use something like:

    <p class="c1">New Web ... </p>

    or

    <p class="c1"><spa n style="font-size: 120%; color: #ff0000;
    font-weight: bold;">New Web ... </span> ... </p>

    Hope that helps.


    --
    Fred

    Comment

    • Fred Oz

      #3
      Re: jcript to book,ark page

      Fred Oz wrote:[color=blue]
      > Seen wrote:[/color]
      [...]

      OK, had time to test this in IE now. Small fix:
      [color=blue]
      > Then lower in the page (or fired from an onload) put:
      >
      > <script type="text/javascript">
      > if (window.externa l.AddFavorite) {[/color]

      Can't test AddFavorite like this, presumably because it is calling an
      IE function rather than a JScript function. It would have been nice if
      MS had supported the ability to test it though. So test
      window.external and hope that if it's implemented, so is AddFavorite:

      if (window.externa l) {

      [...][color=blue]
      > var eStr = "<button "
      > + "onclick=\"wind ow.external.Add Favorite(...);\ ""
      > + ">Add page to favorites<\/button>";[/color]

      Is better written as:

      var eStr = "<button "
      + "onclick=\"wind ow.external.Add Favorite('"
      + document.locati on
      + "','"
      + document.title
      + "');\""
      + ">Add page to favorites<\/button>";
      [color=blue]
      > ele.innerHTML = eStr;[/color]

      I'm also making the bold assumption that if the browser has so far
      supported AddFavorite and window.external (and maybe document.all too),
      it will support innerHTML.

      Some people object to innerHTML, but using a Microsoft invented script
      extension to enable an MS IE-only API seems apt...


      --
      Fred

      Comment

      • Grant Wagner

        #4
        Re: jcript to book,ark page

        "Fred Oz" <OzFred@iinet.n et.auau> wrote in message
        news:JgiFd.121$ Ud4.10674@news. optus.net.au...[color=blue]
        > Fred Oz wrote:[color=green]
        >> Seen wrote:[/color]
        > [...]
        >
        > OK, had time to test this in IE now. Small fix:
        >[color=green]
        >> Then lower in the page (or fired from an onload) put:
        >>
        >> <script type="text/javascript">
        >> if (window.externa l.AddFavorite) {[/color]
        >
        > Can't test AddFavorite like this, presumably because it is calling an
        > IE function rather than a JScript function. It would have been nice
        > if
        > MS had supported the ability to test it though. So test
        > window.external and hope that if it's implemented, so is AddFavorite:
        >
        > if (window.externa l) {[/color]

        An alternate way to test for this is:

        if (window.externa l && 'unknown' == typeof window.external .AddFavorite)
        {
        alert('AddFavor ite supported');
        }
        else
        {
        alert('AddFavor ite not supported');
        }

        Even if an unsupporting browser has window.external defined, it's almost
        impossible for window.external to have an AddFavorite property that is
        typeof "unknown" using user code (although the browser DOM itself could
        certainly set this up and then fail to support AddFavorite anyway).

        Then again, it's entirely possible Microsoft will change the behaviour
        of AddFavorite in some future version of the DOM to return typeof
        "object", which would mean a modification of the above code.

        --
        Grant Wagner <gwagner@agrico reunited.com>
        comp.lang.javas cript FAQ - http://jibbering.com/faq


        Comment

        Working...