Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nelly22

macrumors 6502
Original poster
Sep 29, 2009
366
5
In Safari when JavaScripts is turned from off to on, it loads js in all tabs in all windows.

Is there any extension/plugin/... which loads js only in current tab or only in new tabs?
 

GGJstudios

macrumors Westmere
May 16, 2008
44,556
950
In Safari when JavaScripts is turned from off to on, it loads js in all tabs in all windows.

Is there any extension/plugin/... which loads js only in current tab or only in new tabs?
You didn't mention what version of Safari you're using, but in Safari 5, if you have JavaScript disabled when you visit a page, then enable JavaScript, it does not load any JavaScript until you refresh the page or go to a new page. That behavior may or may not have changed with Safari 6. I use JavaScript Blocker to manage JavaScript on sites I visit.
 

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
A quick Google search doesn't find anything particularly informative.

However, creating a Safari extension to achieve this end should not be that hard.

This little bit of JavaScript will fetch the particular tab that is active (note that this only works in the context of a Safari Extension):
Code:
safari.application.activeBrowserWindow.activeTab

Combining that with a block content startup script, like this 2-part one:
Code:
function isItOkay() {
    var myMessageData = event.url;
    var theAnswer = safari.self.tab.canLoad(event, myMessageData);
    if (theAnswer == "block") {
        event.preventDefault();
    }
}
 
document.addEventListener("beforeload", isItOkay, true);

function blockOrAllow(event) {
    if (event.name === "canLoad") {
        var itsAnAd = event.message.match(/ads.example.com/i);
        if (itsAnAd) {
            event.message = "block";
        }
        else {
            event.message = "allow";
        }
    }
}
 
safari.application.addEventListener("message", blockOrAllow, true);
...and modifying the code accordingly to intercept and stop JS in inactive tabs from running. If you're not a coder like me, I wouldn't be surprised if you don't understand anything I posted. That's okay, though - I do, and I'd be willing to help address a need that I am sure others besides you are looking for.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.