Navigate new page in same tab/window using button - javascript

I've been trying to navigate a page to another page in the same window (web browser = Google Chrome, Mozilla Firefox and Internet Explorer) using button. The steps are,
The page will popup message for confirmation.
If the user click ok, then the page will navigate to other page in the same tab/window.
If the user click cancel or x, it stays on the current page.
I have tried using
window.open("home.php","_self"); and window.location.href="home.php"; and document.location.replace("home.php"); and location.replace("home.php"); and location.assign("home.php");
but it doesn't work. How do I fix this?
<span title="Click to register the new user"><button type="submit" id="submit" onclick="myFunction()">Register User</button></span>
<script>
function myFunction() {
confirm("Proceed?");
window.open("home.php");
}
</script>

Problem seems to me is, possibly you are using form with button which leads to reload.
<button type="submit" id="submit" onclick="myFunction()">Register User</button>
Make the type="submit" to type="button". Also major browser do have popup blocker enabled where this line may fail window.open("home.php");
Small change in script as follows:
function myFunction() {
if(confirm("Proceed?")) //<--- ok/yes = true, no/cancel/close = false
location.href = "home.php";
}
Small demo:
var ask = function() {
if (confirm('Proceed ?'))
location.href = "http://espn.go.com/";
};
<button onclick="ask();">Watch ...</button>

window.location.href
is not a function . You have to use it as
window.location.href="home.php";
Updated your code and this works
<span title="Click to register the new user"><button type="submit" id="submit" onclick="myFunction()">Register User</button></span>
<script>
function myFunction() {
if( confirm("Proceed?") )
window.location.href="home.php";
}
</script>

Related

How to close the webpage on its own on clicking a button

I have a page where a <button> is kept on which onClick() event fires and redirects the new page in new tab. Meanwhile I want the parent page to close itself as soon as the <button> is clicked with opening the new page in new tab. Its basically for some security feature for the website. How can this be done?
Here is my <button>, let me know where can i correct myself.
<button formtarget="_blank" type="submit" name="submit" onClick="javascript:window.open('quiz.php?unit_id=<?php echo $fnc->encode($unit_id) ; ?>');self.close();" value="submit" class="btn btn-info" style='margin-left: 35%;margin-bottom: 10px;' ><i class="glyphicon"></i> Start Quiz</button>
you can close window by below function
function close(){
setTimeout("window.close()", 500);
}
But keep in mind that : Scripts may close only the windows that were opened by it.
The window close and open new tab
<script language="javascript">
function quitWindow(cmd)
{
if(window.open("your_new_tab_page.htm")){
if (cmd=='quit')
{
open(location, '_self').close();
}
}
return false;
}
</script>
<input type="submit" onclick="return quitWindow('quit');" value="Close This Window and open new Tab" />
If you use firefox, you should about:config by writing url bar and set
dom.allow_scripts_to_close_windows = true
otherwise does not work firefox
I think this might work.
<script>
function openWindow( url )
{
window.open(url, '_blank');
window.focus();
window.close();
}
</script>
<button onclick="javascript:openWindow(yourHref);return false;">Submit</button>
You cant close your webpage if the user clicked the link of your website from any other place other than your website, due to security reasons.

window.open('url', '_self') not working

So i wanted to open a new page replacing the current one, i found that the method should be putting the second parameter on _self but nothing happen...
By the way, if i use the _blank parameter or i left it empty it opens in a new page. The rest of the function works good, but i can't find a way to close the current page and open the new one that i want.
Here is the javascript and the html buttom that call the function.
<button id="rgstr_btn" type="submit" class="btn btn-info" onClick="store()">Register</button>
<script>
function store() {
localStorage.setItem('nome', nome.value);
localStorage.setItem('pw', pw.value);
window.open('url', '_self');
}
</script>
Button has a type attribute which defaults to submit: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-type
While this does not affect "everyday" buttons, if the button resides in a form, this way it will submit the form, and result in some page loading, which clashes with your own attempt.
You can just add a type="button" attribute to the button to avoid that:
<button id="rgstr_btn" type="button" class="btn btn-info" onClick="store()">Register</button>
^^^^^^^^^^^^^
windows.open() opens the URL in a new window.
To replace the URL in the current window, use:
window.location.href = 'http://example.com';

Popup blocker disables onclick

I have a button that is connected a JS function to operate SSO for another website. The button works when popup blocker is disabled, however it no longer works when popup block is turned on. How do I get onClick to work while the popup blocker is activated? Below is my code:
HTML
<input class="btn btn-primary btn-lg" type="button" value="Launch AdEase" onClick="postURL()" />
Javascript
function postURL() {
var idOfClientFirm = document.getElementById('mfID').value;
document.getElementById('<%= txtField2.ClientID%>').value = idOfClientFirm;
document.getElementById('<%= loginLinkButton.ClientID%>').click();
}

Javascript only runs alert

I'm trying to piece together a basic page using PHP & JS to take me to a google map related to a search box. However the JS script will only run with an alert, no other function type will run:
The address bar just changes to "Domainx.com/?searchButton="
The code for the button is:
<button class="btn btn-success btn-lg" name="searchButton" onclick="searchFunction()">Search </button>
and directly under it (within the body) is have my script:
<script>
function searchFunction() {
alert("pressed");
}
</script>
When i change the alert to anything else i.e. window.location.href = "http://www.google.com"
it simple stays on the page and only the address bar changes.
You probably wanted to use window.open("http://www.google.com/search?q=the_meaning_of_life");
If you wanted to use reload onclick, the following should work:
<input type="button" value="Reload Page" onClick="window.location.reload()">
There is an example of how to dow it here - http://www.mediacollege.com/internet/javascript/page/reload.html

window.close() not working on pop up with pop up

I have a Customer Info form that has anchor tag "close" that should close the current window. This customer form is being opened as pop up. The Customer form also has a search btn that when clicked it pop ups the search form that is being run by a javascript called searchOrder.js. So basically it is a pop up inside a pop up. The customer form's close btn is not working but when the reference to searchOrder.js is removed, it begins to work. Also, when the search pop up is open and I click the "close" anchor tag, it closes the search pop up but never the current window which is the customer form.
Ive tried a lot of solutions already but nothing is working. I used self.close(), window.opener.close(), made it a btn instead of a link etc.,c alled a function onclick
function closeWindow() {
var closeRef;
window.opener='x';
closeRef = window.open('','_parent','');
//or closeRef = window.open("",name);
closeRef.close();
}
heres my gsp code:
<g:form method="post" name="CustomerInfoForm" target="_parent" role="form">
<div class="id="closeLink">Close</div>
<button id="searchOrderButton" type="button" class="button" onclick="searchOrder(document.forms[0].summaryMessage.value,'summaryMessageText','${createLink(action:'searchOrder')}','${createLinkTo(dir:'images',file:'closeButton.gif')}')" value="Search Order">Check Order</button>
</g:form>
How do I make the current window (customer info ) close? tnx
You should use a function like this to close your popup window :
<g:form method="post" name="CustomerInfoForm" target="_parent" role="form">
<div class="id="closeLink">
Close
</div>
<button id="searchOrderButton" type="button" class="button" onclick="searchOrder(document.forms[0].summaryMessage.value,'summaryMessageText','${createLink(action:'searchOrder')}','${createLinkTo(dir:'images',file:'closeButton.gif')}')" value="Search Order">Check Order</button>
</g:form>
<script type="text/javascript">
function openpopup()
{
my_window = window.open("","myPopup","status=1,width=100,height=100");
}
function closepopup()
{
if(false == my_window.closed)
{
my_window.close ();
}
}
</script>
EDIT : Don't forget to open your popup with the openpopup() function :
Open Popup Window
You can shorten window.close() to just close() and shorten window.open() to open(). Maybe you'll find what you need here, though:
http://www.w3schools.com/js/tryit.asp?filename=try_win_closed
http://www.w3schools.com/jsref/obj_window.asp

Categories

Resources