Every now and then, I need to have a control on my page open a new tab. And for anchors, that’s easy–just stick a target=”_blank” on the anchor tab and get on with your life. Unfortunately, you very rarely build the anchor tabs in APEX; the engine handles all of that for you. And sometimes, it just doesn’t do what you expect.
In one of my current projects, I needed a button in the sidebar which opened a new tab. I created a region button, set its Action to “Redirect to URL” and the URL Target to what I needed; in order to get the new tab functionality, I added the good old target=”_blank” to the Button Attributes (our button templates are actually anchor tabs around CSS-styled spans).
Unfortunately, this…didn’t work. Chrome didn’t open a new tab; it just took over the existing one. Firefox opened a blank tab, but didn’t go anywhere. And IE opened a new tab with an error message (“Internet Explorer cannot display the webpage”). Not good.
At this point, I looked at what the engine had generated. The anchor tag had this for its HREF:
(where the ellipses was my desired URL). After some digging around–and careful testing in all three browsers–I found that I needed to set the URL target to this:
(Setting the Button Attributes isn’t necessary with this solution.) The part that took me the longest to figure–and the reason I’m writing this post–is the “var x=” section. Without it, all three browsers will happily open a new window, but Firefox went a step further, sending the original browser page to one that says, simply, “[object Window]”. This seems like a rather odd decision on the part of Firefox; I really struggle to picture an instance where this would be viewed as “the right thing to do”.
Regardless, now I know. And knowing is half the battle.