Safari "TypeError - Undefined value" with bookmarklet, need advice

Discussion in 'Javascript' started by OtisUsenet, Aug 6, 2006.

  1. OtisUsenet

    OtisUsenet Guest


    I have a bookmarklet that works perfectly in Firefox, IE, Konqueror,
    and Opera, but in Safari 2.0.3 (417.9.2) it doesn't work. I enabled
    debugging and I can see "TypeError - Undefined value" reported in the
    Javascript console, but I'm not sure where the error is.

    The bookmarklet in question is here:

    javascript:if (parseInt(navigator.appVersion) >= 4) { if
    (navigator.userAgent.indexOf('MSIE') > 0) { q =
    document.selection.createRange().text; } else if
    (navigator.userAgent.indexOf('Safari') > 0) { q =
    document.selection.createRange().text; } else { q =
    document.getSelection(); } if (!q) { void(q = prompt('Enter
    keywords to search your Simpy Links:', '')); } if (q) {
    location.href='' +
    encodeURIComponent(q); }}

    I tried changing the "document.selection.createRange().text" bit in the
    case of Safari to "document.getSelection()", but that didn't work
    either, the error being "TypeError - Value undefined (result of
    expression document.getSelection) is not object".

    Does anyone know how to get the selected text under Safari?

    If anyone's interested, this bookmarklet is from Simpy -

    Any help would be very appreciated. Thank you!
    OtisUsenet, Aug 6, 2006
    1. Advertisements

  2. OtisUsenet

    OtisUsenet Guest

    A possible solution: window.getSelection() instead of
    document.selection.createRange().text and document.getSelection().

    Is that the right thing to do for Safari?

    OtisUsenet, Aug 6, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.