views:

72

answers:

1

I have a bookmarklet that needs to open a new window/tab. In order to avoid the popup blocker, I need to call the window.open() method directly in the bookmarklet ie: at the browser-level.

However, I want to keep the bookmarklet updatable by loading external Javascript files. To do this, the bookmarklet needs to append script nodes to the DOM. If i were to put window.open() code in one of these externally loaded scripts, the popup blocker would block it since its page-level.

What I want to know is if I can create a wrapper function around window.open() in my bookmarklet, then call it from the externally loaded script? What is the scope and what are the permissions on a wrap such as this?

+1  A: 

I came up with a solution which isn't perfect but meets the requirements:

Here is the bookmarklet code:

javascript:window.open(window.location);window.location="http://www.google.com/";var%20s=document.createElement('script');s.setAttribute('src','http://my-script.js');document.body.appendChild(s);void(0);

The readable step-by-step equivalent being:

window.open(window.location);                // Clone the current tab
window.location = "http://www.google.com/";  // Navigate to the desired page url
var s = document.createElement('script');    // Create the script
s.setAttribute('src','http://my-script.js'); //
document.body.appendChild(s);                // Embed it into current document

Only one issue remains: the page you want to show isn't active by default. The cloned one is.

Arnaud Leymet