i have another problem :(
this is my script
<script type="text/JavaScript">
function first()
{
var first = confirm("Are you sure you want to navigate away from this page?");
if (first)
window.location = "http://www.yahoo.com.sg"
else
window.close;
}
function second()
{
var second = confirm("Are you sure you want to navigate away from this page?");
if (second)
window.location = "http://www.google.com"
else
window.close;
}
</script>
<p><b>Click the following link to enter yahoo</b></p>
<p>yahoo</p>
<p><b>Click the following link to check google</b></p>
<p>google</p>
its doesnt open in a new window.
please i need some help. any suggestion or recommendation?
Try window.open. This will open a new window:
window.open("http://www.google.com");
See also:
MDN: window.open usability issues
Use the window.open method and specify _blank as the target:
window.open('http://www.yahoo.com.sg', '_blank');
This will open the page in a new window or a new tab, depending on the user settings in the browser.
If you want it to open in a new popup window always, you can specify a parameter string with width and height for the window. However, most browsers will block such a popup that opens a page from a different domain.
If you are opening in a new window, you don't need to check with the user about navigating away.
function first()
{
window.open("http://www.yahoo.com.sg")
}
function second()
{
window.open"http://www.google.com")
}
Related
I want to open a window using window.open(). but i have to make sure that if that same window has already opened previously , i have to first close that window then i have to open a new one .
try this code
<button onclick="openNewWindow()">Open New Window</button>
var popup_window;
function openNewWindow(){
if(popup_window != undefined){
popup_window.close();
}
popup_window = window.open("");
}
Try following code
let openedWindow;
let url = "www.example.com"
function openWindow() {
if(openedWindow) {
openedWindow.close();
}
openedWindow = window.open(url);
}
NOTE : The Window.close() 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.
Check MDN web docs
I want to create a jQuery script that opens a specific url in a new tab if the user clicks somewhere (no matter where). After that, the user should be able to click anywhere without getting a new tab at every click again.
Unfortunately I have no idea how to create such a script, and therefore I would appreciate all your answears :)
You can use the .one() event like
$(window).one('click', function() {
var url = "http://google.com";
window.open(url, "_blank");
})
_blank in window.open() is used to open the link in a new tab.
DEMO
Refer to following links for documentation on .one() and window.open()
$(window).one('click', function() {
var url = "http://google.com";
window.open(url, "_blank");
})
I have a page (let's say page1) that open the page2 using:
showModalDialog(page2, '', 'dialogWidth:55em; dialogHeight:50em; status:0');
I am facing 2 problems (in this post i am going to write the first one):
1) when i want to close the second page using javascript code it always asks me that the page is trying to close itself do i want to allow that or no. I want to force close it without asking me anything. I have tried the following:
function CloseSelection(id) {
window.opener = "page1"
window.returnValue = id;
window.close();
}
And in the Form tag target="_self". But nothing happened, the popup keeps on showing.
can anyone help me please?
You dont need to set the window.opener as it is set by default to the object of opener page.
On Page2 the code will be:
function CloseSelection(id) {
window.returnValue = id;
window.close();
}
On Page1
var returnVal = showModalDialog("page2.html", '', 'dialogWidth:55em; dialogHeight:50em; status:0');
// here returnVal will contain the data which is return by page2
Hope it helps.
You can not force the window to open in a new window. It might work in some browsers but not all.
In order to close a window you can do this:
// open
var win = window.open("someurlgoeshere");
// close
win.close();
I believe you only get the "are u sure you wish the leave this page" message if the window itself tries to close itself (window.close())
I have a button:
<button>Click</button>
I want click it and open a new window in the same page, I bind the click event an event handle:
$('button').click(function () {
var newurl="http://" + window.location["host"] + ";
window.open(newurl, '_parent');
})
But is always open in new tab or new window, the second parameter I have try _self _top.
I even have try window.location.href(newurl)
So how can I solve this problem?Does it matter with browser or OS? I view it in Mac OS's firefox and chrome.
Well that's not possible technically, it also depends on browser tab settings, window settings and third party tab manipulation plugins or addons.
Update
OP has cleared the confusion of all through his comment below his question, this is what you need to set a new url for current window:
$('button').click(function () {
var newurl="http://" + window.location["host"];
window.location.href = newurl; // or simply window.location
})
window.location =newurl; will open a new window replacing the parent
Have you tried this?
<script type="text/javascript">
window.open ('test.htm','_self',false)
</script>
If you mean you want to replace the current page, window.location = newLocation; will do.
If you want a modal popup, try JQuery UI Dialog.
I'm using a the confirmit java script function to redirect to a new url with a positive response. It doesn't do exactly what I want - and maybe I can't have exactly what I want.
Here's what it does - onunload, with a positive response it opens the new url in a new window, which can be blocked by a pop-up blocker without the user even knowing, giving the impression that nothing happened. When it does open the new URL it opens in a new window making the desktop crowded. Its just not a great solution.
What I want it to do - onunload I'd like it to open the new url in the same window and have it not be classified as a "pop-up" to be blocked. I tried switching to onbeforeunload.... nuthin.
Is there a simple solution?
here's my current code:
<script language=javascript>
function confirmit()
{
var closeit= confirm("My message");
if (closeit == true)
{window.open("http://MyDestinationURL");}
else
{window.close();} } window.onunload = confirmit
</script>
Instead of opening a new window you can set the location of the current window:
<script type="text/javascript">
function confirmit() {
if (window.confirm("My message")) {
window.location.href = 'http://MyDestinationURL';
} else {
window.close();
}
}
window.onunload = confirmit;
</script>
I am not sure if you actually want the else case where it tries to close the current window. If you just want to stay on the current page on a negative response, remove the else part.