new window/tab from href - javascript

I have access to a joomla edition but not the source, and I need add a link which must be opened in other window/tab.
I only can change the href field content.
I try with
javascript:window.open('http://url/')
this work with chrome but not with firefox.
any suggestion?

According to MDN, window.open() requires a minimum of two arguments. It also returns the object for the newly opened window, which would cause [object Window] to appear in the original window. Returning undefined avoids this second issue. This would work:
javascript:window.open('http://url/', '_blank');undefined
Older code would commonly use the void operator, which evaluates its only operand and yields an undefined result no matter what. So this would also work:
javascript:void(window.open('http://url/', '_blank'))
In either case, the special window name _blank just tells the browser to open a new window or tab. The browser's settings determine the exact behavior because tabbed browsing was introduced after _blank was.

Just use a _blank target attribute, which opens the link in a new window/tab (you can't control which one, the browser will pick based on a click event algorithm):
Link
Not sure why that wouldn't work in FF using the "javascript:" protocol but the JS, in a regular script tag, would probably work. The MDN entry on window.open might be helpful to read as well.

Related

window.close fails when clicking links within opened window

I am opening a new window with window.open(). If I do NOTHING else on this page, and click the "close window" link, the window closes. This works perfectly; however, if I navigate between pages(all under the same domain) window.close() no longer works.
Is there a way to fix this?
Here is how I am opening pages in this example...
<a href="###" target="_blank">
Here is my close link:
close
I use 2 methods of changing pages within the opened windows.
<select onchange="if (this.value) window.location.href=this.value"> AND STANDARD <a href="####"> tag
Do I need to navigate links within this window a certain way to still maintain my window.close() ability?
You should favour using window.open() to open a new window if you want to close it using window.close(). E.g.
<script>
function newWindow() {
window.open('foo.html', 'myWindow');
return false;
}
</script>
link
From MDN:
The Window.close() method closes the current window, or the window on which it was called. This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.
I understand the behaviour you're describing where you can close the window as long as you don't navigate. I can replicate this in Google Chrome.
I believe this is because (from the spec):
A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.
Your new window is considered a "top-level browsing context", not an "auxilliary browsing context". Up until the point that you navigate, the history contains one document, so can be closed using window.close(). As soon as you navigate, the history has more than one document so it's not valid to close the window.
You should also have a read about window.opener and the security risks it poses.
window.opener provides a reference to the window object that opened the current window.
Recent advice is to use rel="noopener" on all links that open in new windows. Again, this is because setting window.opener without knowing what you're doing poses a security risk. Have a read about it here:
Links to cross-origin destinations are unsafe
Browsers are now starting to treat all target="_blank" links as if rel="noopener" had been set, even if you as a developer don't set it. You can see that this is now in most major browsers.
Therefore, you could use link (explicitly setting window.opener) and I think you'd get the behaviour you want. However, this might not be reliable across browsers, and also has security implications as described in the web.dev article.

window.opener issue with window.open() when replacing a window

I have a web application where page #1 opens a popup window using
window.open(myUrl, "fixedApplicationTargetId", "");
Then page #2 overwrites the same popup window with a call to window.open using the same target value
window.open(anotherUrl, "fixedApplicationTargetId", "");
At this point the content of the popup originally created by page #1 shows the new content created by page #2. So far so good with any browser.
Then the popup itself detects who last opened the popup and updated the content using window.opener. Prior to calling window.open both page #1 and page #2 create a global variable globalPageId and assign a unique number each. The popup checks the value of window.opener.globalPageId and detects which window last updated the popup content.
This is where things fall apart: the above works fine with chrome and firefox that update window.opener in the popup each time the content is updated with window.open. Instead, IE and opera always point the popup window.opener to the first window that used window.open.
Any suggestion, in a context where multiple pages call window.open on the same target, how to detect from the popup itself which window last opened the window?
window.opener is supposed to be read-write (except in Internet Explorer 3), so you could set it to the appropriate window yourself. Some browsers, however, restrict this operation and only allow setting opener to null to prevent security issues.
An alternate solution would be to use a custom property instead of opener. You could set it by hand:
window.open(myUrl, "fixedApplicationTargetId", "").realOpener = window;
Then use window.realOpener.globalPageId instead of window.opener.globalPageId in the rest of your code.

window.open() still returns null

I am using IE8 on Windows 7. Referred to several threads and understand that in IE8 when I am using window.open to popup a new window, the JavaScript window.open is returning null value.
If I run IE as administrator or disable the protected mode, I see the window.open returns the expected object.
I am looking out for a solution apart from the options mentioned above. For such a small feature (opening a popup) I cannot ask customer to run IE as administrator or disable the protected mode.
If there is any work around, please let me know. It will be a great help.
Primarily, I want to make sure that only one window is opened when user clicks multiple times on the link and give the focus to the window which is already open. To achieve this I need to get the object from window.open so that I can check if the window is already open and give the focus to the already opened window. Otherwise open a new window.
For IE10, window.open returns a NULL reference object if Enable Protected Mode is checked under Internet Options->Security->Security Level for this zone and the ZONE is different i.e. in my case local file opening a popup from Intranet.
window.open returns a reference object even if Enable Protected Mode is checked when yoursite.com opens someothersite.com in popup window i.e. Internet->Internet
You can use window.showModalDialog as a alternative or replacement for window.open method.
It is more secure then window.open. It will not allow user clicking the Parent page.
Example Usage:
var myFeatures = "dialogWidth:1060px;dialogHeight:550px;resizable:yes";
window.showModalDialog(url,window,myFeatures);
//Here window is an object, no need to assign or declare.
If you want more detail explanation see Here.
//Fifth Question.

how to force a link to be opened in a new tab using javascript

i want to make sure that in all tab enabled browsers, when a user clicks a link, it opens in a new tab. All i have got so far is the target keyword in the anchor, but is there some new html attribute that supports that function?
There is no guaranteed way for that because you can change window opening behavior and tabbing options from within browser options.
The best you can do is to write your code using either target="_blank" or window.open().
There is a CSS3-property target-new
Unfortunately it isn't supported yet by any browser(I don't know any). But maybe you could already implement it for future use.
Your best bet is .
There is no standardized semantic way of telling the browser to open a new tab. This is because not all browsers have tabs. Take most mobile browsers, for example, they don't have tabs.
There is also no vendor specific way of doing this that I know of.
No, there's no HTML attribute that tells tab-enabled browsers to do this if they can, you're stuck with target using either "_blank" for new window or a specific name if you want to reuse a window. It would sure be nice, but there's not even anything in the current HTML5 working draft, at least not under a or target (e.g., there's no "context name" for "new tab").
Edit: But look at (and vote up) Dr.Molle's answer. CSS to the rescue (someday)!

How can I modify this bookmarklet to work on a tab instead of a new window?

When you put this in your browser it opens a simple notepad to type in.
I don't know enough about javascript to alter it but I would like to make it so it DOESN'T open in a new window but rather in the current window or another tab.
Is this even possible?
javascript:pwin=window.open('','_blank','menubar=yes,scrollbars=yes,location=no,height=450,width=350');pwin.document.body.contentEditable='true';pwin.document.designMode='on';void(0);if(window.focus){pwin.focus()};
Technically, window.open does not always open in a new window. You can use _self instead of _blank, and it will open in the same window. Or use _parent and it will open in the current frame's parent.
Window.open always opens this in a new window.
Instead I think you can try window.location. Not tried tough.
Ok did a little digging and there is nothing you can do that will guarantee that it will open in a new tab. However if you remove the width and height parameters some browsers will open in a new tab if user preferences haven't overridden this behavior. See this post (2nd answer in list) Stackoverflow post
If you aren't looking to open a window, then don't window.open()
Setting window.location should do the trick, as pointed out by Sachin

Categories