views:

28

answers:

1

Hello,

I'm creating a modal window bookmarklet that allows you to quickly add content to my website. It all works fine and dandy except for non-html content, because then it cannot append data to the body. Instead, I'd like to do a simple location.href.

Current code:

if (typeof jQuery == 'undefined') {
    var jQ = document.createElement('script');
    jQ.type = 'text/javascript';
    jQ.onload=runthis;
    jQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
    document.body.appendChild(jQ);
} else {
    runthis();
}

function runthis() {
    if ($("#baasframe").length == 0) {

            $("body").append("\
            <div id='baasframe'>\
            <div id='baasframe_veil' style=''>\
                <p>Loading...</p>\
            </div>\
            <iframe src='http://example.com/submit.php?url="+encodeURIComponent(location.href)+"&amp;title="+encodeURIComponent(document.title)+"' onload=\"$('#baasframe iframe').slideDown(500);\">Enable iFrames.</iframe>\
            <style type='text/css'>\
                #baasframe_veil { display: none; position: fixed; width: 100%; height: 100%; top: 0; left: 0; background-color: rgba(0,0,0,.50); cursor: pointer; z-index: 900; }\
                #baasframe_veil p { color: white; font: normal normal bold 20px/20px Helvetica, sans-serif; position: absolute; top: 50%; left: 50%; width: 10em; margin: -10px auto 0 -5em; text-align: center; }\
                #baasframe iframe { display: none; position: fixed; top: 10%; left: 10%; width: 80%; height: 80%; z-index: 999; border: 10px solid rgba(0,0,0,.5); margin: -5px 0 0 -5px; }\
            </style>\
        </div>");
        $("#baasframe_veil").fadeIn(750);
    }

$("#baasframe_veil").click(function(event){
    $("#baasframe_veil").fadeOut(750);
    $("#baasframe iframe").slideUp(500);
    setTimeout("$('#baasframe').remove()", 750);
});
}

function getSelText() {
var s = '';
if (window.getSelection) {
    s = window.getSelection();
} else if (document.getSelection) {
    s = document.getSelection();
} else if (document.selection) {
    s = document.selection.createRange().text;
}
return s;
}

How can I make it detect that the appending of data didn't work and direct it to a location.href instead?

Much obliged,

Dennis

A: 

Use document.documentElement.

Eli Grey
Hi Eli, I tried this but it doesn't work, have you tried this yourself to see if it works?
FLX
I think your problem is that you're using `onload` on the script tag, which I doubt many browsers would support.
Eli Grey