i tried below javascript and it does open the url in open window but it clears the url in the current window as well points to the new URL.
window.open('http://www.google.com','_blank');
This is occurring because the second spot where you have _blank is where the name should be.
I would recommend as the link above that Nathan post suggests doing this. :
window.open(url, windowName, "height=200,width=200");
Then doing some javascript triggering on whatever event is causing the new window to open.
So, lets say it is an image, set the Target="_blank" for the image and then set an OnClick event to call a function with the window.open code in it.
That will do the trick. HTH
Related
I am using the code shown in the images below. I have tried many ways like closing login window, before opening a new one. I have also tried many ways like window.open("index.HTML", '_self', false) and window.location.replace("index.HTML") but they are not working at all:
I usually use window.open("http://www.example.com", "_blank")
Also check your pop-up blocker since it will most probably detect this as popup and block it.
If not then use a monkey patch which is create an html anchor tag with target="_blank". Hide it with CSS visibility: hidden, then invoke the click event using JS.
Read more about popups and blocking in this answer. If your window.open event was not triggered by a click then most probably it would be blocked.
What you want to do, if I am not mistaken, is open a new webpage but using the same window (or tab)... To do this, you should modify the window.location property.
For example:
window.location = "new_page.html"
This will take you to new_page.html.
If your current location is: /users/muhammad/index.html
Then the page will take you to: /users/muhammad/new_page.html
Leave a comment if you have any questions!
=)
If I have a script which calls window.open, loading a new page into the same window, then that new page's JavaScript needs to identify the Previous Page's URL - what is the best way to find it?
You can use window.opener.location.href , it will give you the URL of the parent page from which you opened a new window.
You can use window.opener to obtain a reference to the window that opened the current window. Not sure if this is what you are after. When using window.open it will result in a new window, not sure what you mean by "open new window in the same screen".
Using Window.opener you can get it, like
var win=window.open('http://heera.it');
console.log(win.opener.location.href); // will output the url
Example.
Example of what I want to do: On Facebook, clicking a link to open it in the current tab will trigger some JavaScript rather than opening the link. However, opening it in a new tab (either by right-clicking or by holding Ctrl/Cmd) will open the link without calling any JavaScript.
I'd like to do the same with my links (have link behaviour dependent on the target). I have the onclick event handler return false; to avoid opening the link; but this causes Ctrl+clicking to fail to open the link. How do I achieve this?
EDIT: I do not want links to forcefully open in new windows. IF a link is opened in a new window, I want it to follow the href as usual. However, IF the link is opened in the current window, rather than following the link in the current window, I want to have some JavaScript run.
Facebook does this to open an image in a popup "theatre" if you open it in the current window, and open it in a full page if you open it in a new window.
Note that capturing the click event on links and using preventDefault() or return false causes Cmd+Click (open in new tab) to fail. In any case, this should work regardless of how the link is opened - click, Enter key, etc.
EDIT 2: It seems that hashchange / HTML5 pushState is the correct way to go about this
You can set the href of the anchor pointing to correct url. This way right click and open in a new tab or window will work fine.
Use jQuery to bind the click event handler to the anchor like this.
$('a').click(function(e){
//Do whatever javascript operation you want.
if(!e.ctrlKey){
e.preventDefault();//Stop the page to navigate to the url set in href
}
});
Demo
does anybody know such a problem, window.open()do not work with Firefox suddenly.
<a href="javascript: void(0)"
onclick="window.open('popup.html',
'windowname1',
'width=200, height=77');
return false;">Click here for simple popup window</a>
this always open in a new window (or a new tab), but not open in a popup window.
Thanks,
I find that your code works perfectly. I pasted it into a new HTML page, clicked the link (using Firefox) and voila, new window.
My guess is that you're trying to use the link from a window that already has the name of the window you're trying to create. If the page is presented in a window whose name is already "windowname1", then the browser will put the results of you javascript action in that window instead of popping up a new one.
For example, if the code above is on a page named "popup.html" (the name of the file you open in window.open statement) then it will work the first time (since you haven't yet created a window named "windowname1". Then if you try to click the link again in the new window that popped up (whose name is windowname1), it will just refresh the same window instead of popping a new one.
I don't see why this would happen in firefox only though. I found identical results in Firefox, Chrome, and IE.
if you want to open a pop up window, it's alert('message'). window.open always opened a full window/tab.
(Edited: Even if you specify width/height, most browser allow you to allow javascript but not allow it to resize your windows, and firefox also allows you to force new windows to new tabs)
Maybe you need to add some extra variables to the window.open() method
<a href="#"
onclick="window.open('http://url','windowname1', 'width=200,height=100,scrollbars=yes,toolbar=yes,location=yes'); return false">Link</a>
Add attribute target="_blank" to <a> tag as below. This will open as popup.
<a target="_blank" href="javascript:void(0)">...</a>
how do i call a link to itunes (itms://...) without opening a new window.
with window.open there comes a new window (as the function says :))
$.get('itms://...'); doesn't work.
Any other possibilitys :)?
would be thankful for help!
Didn't try, but two ideas come to mind:
iframe
set location to the URL, it shouldn't replace your page anyway.
To clarify, you are referring to not opening a new browser window, as opposed to trying to open iTunes within the browser.
In that case, Nickolay's 2nd option is your best bet - this JavaScript will work (using actual example URL):
var url = 'itms://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=307538288';
location.href = url;