TiVO25
TiVO25

Reputation: 41

JS Bookmarklet Not Working in IE11

long time lurker, first time poster.

The basics are this:

Inspired by this blog post, I'm attempting to create a bookmarklet for our QA team to quickly populate a form.

The bookmarklet works perfectly on my machine (TM) in Chrome, IE 10, and IE 9 compatibility mode. I've tested the bookmarklet on the QA team's machines, and while it works fine on their Chrome browsers and on IE 9, the bookmarklet doesn't work on the two machines running IE 11.

Details:

Of the two machines running IE 11, one is on Win 7, the other is on Win 8.1. The machine running IE 11 on Win 7 is updated to IE version 11.0.9600.17207IS, and will also run the bookmarklet correctly in Chrome.

I've attempted a couple different variations, the first based on the CSS-Tricks blog post mentioned above.

<a href="javascript:
    (function(d) {
        var body = d.getElementsByTagName('body').item(0);
        var script = d.createElement('script');
        script.src = 'https://company.siteurl.com/unrelated/DisclosureRegister.js';
        body.appendChild(script);
    }(window.document));
    alert('Bookmarklet js fired!');">Disclosure Register Bookmarklet</a>

The second, based on a StackOverflow Response, is as follows:

<a href="javascript:
    (function(){
        var s=document.createElement('script');
        s.type='text/javascript';    
        s.src='https://company.siteurl.com/unrelated/DisclosureRegister.js';
        document.getElementsByTagName('head')[0].appendChild(s);
    }());">Disclosure Register 2</a>

Both codes work on my machine in the previously mentioned browsers, neither work in IE 11 on the other machines (I don't have IE 11, can't test it directly on my machine).

I get no error message in console in the developer tools, and the DisclosureRegister.js file isn't listed in the developer tools' file list. It appears that the bookmarklet simply isn't firing.

Neither one of those bookmarklets have a Web Documents tab when I right click > properties, however, a simple javascript alert bookmarklet I provided them with does have the Web Documents tab, and it works perfectly in all browsers (including IE 11).

<a href="javascript:alert('That tickles!');">Click me!</a>

I'm delivering the bookmarklets by writing the html in Codepen, emailing them a direct link to the Codepen (I don't have enough reputation points to post the link), and having them drag the link from the Codepen output to their browser favorites bar.

I came across something that gave me hope it was a bug with an older version of IE 11, but the IE version being used is even newer than the version that fixed the related bug.

Just in case there's something in the DisclosureRegister.js file that's causing IE 11 to abort the javascript, I'm including the code for that as well:

(function (win, doc, $, undefined) {

    //Don't run if jQuery isn't loaded
    if (typeof window.jQuery === 'undefined') {
        return;
    }

    alert("Script is being called!");

    }(window, window.document, window.jQuery));

The question:

While I'm certainly not ruling out an error with my code (and of course, please correct me if it's wrong), is there anything peculiar with IE 11 in the way it handles javascript bookmarklets that I need to account for in the code?

Thanks in advance for your time!

Upvotes: 4

Views: 5291

Answers (3)

Kyle
Kyle

Reputation: 887

I was able to resolve the same problem by removing all line breaks from the bookmarklet code. IE 11 apparently doesn't know how to handle them.

With line breaks in the bookmarklet code, after adding it to "Favorites" the properties showed a blank URL and nothing would happen when clicking it.

With the linebreaks removed (ran a regex to collapse all whitespace to a single space character), after adding it to the "Favorites" the properties show the correct javascript in the URL field and it runs correctly when clicked.

I wasn't able to find anything anywhere documenting this limitation.

Upvotes: 0

Alex Fedorov
Alex Fedorov

Reputation: 46

I think I found what is the problem with bookmarklets in IE11. I just added a bookmarklet in IE11 and it wasn't working so I started googling and found this page. Then I found bookmarklets at Microsoft's own website where they actually recommend to use it and it works. Then I decided to check if the syntax is correct and this where I found a problem. For some reason IE cuts part of long bookmarklets when it adds it to the toolbar by draggin-dropping. My bookmarklet that I was tried to add was several brackets short of original and because of that the syntax was incorrect. I then copy-pasted full code manually and it started working.

Upvotes: 1

Adam
Adam

Reputation: 4074

I wrote that article. Really glad you guys are finding it useful.

It seems there a bunch of people complaining about bookmarklets not working properly in IE11. One thing I'd test is how IE11 handles same origin policy. Check out this link, and see if you can temporarily disable same origin policy. Then test.

Obviously it won't solve the problem, but it could lead you in the right direction.

Hope that helps a bit. Let me know.

Upvotes: 1

Related Questions