Jump to content

opening window in same window does not work


tommyt

Recommended Posts

Hello, I am relatively new to javascript although I have used some over recent years in my html web pages. I have a specific problem in which I have a form with an input box that I must validate as true or false. This part works. The call to another javascript will only work if tagged as target="_blank". I want to open the new url in the same window and nothing I have found on internet has worked.

 

Here is my code:

 

<script type="text/javascript"> function gotoProductWebPage() { var answer = document.getElementById("answer").value; if(answer==8){ nowbaby(); return true; } else { alert("Check your Math"); return false; } } </script>

<Script type="text/javascript"> function nowbaby() { window.open ("myurl,"_self",false); }​

 

and here is the call from form:

 

<form method="get" name="myname" onsubmit="gotoProductWebPage()">​

 

and here is html snippet:

 

<p style='color:blue;text-align:center' > Enter answer to prove you are human. A=3, B=5, A+B=<input type="number" id="answer" size="6" name="answer" /><br /> </p> <p align='center' > <input type="submit" name="Submit" value="Submit" /> </p>​

 

Like I said, if I make it _blank it will open in new window, but for the life of me, I cannot get it open in same window.

I have tried:

window.location.href = "myurl"

window.open("myurl/pid='0000","_self")

location.ref

and nothing seems to work. I am stumped. Please Help.

I am working in IE and Edge, sorry should have referenced that.

Link to comment
Share on other sites

Like?

<!DOCTYPE html><html>    <head>        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />        <title>Navigation</title>        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>        <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>        <script type="text/javascript">        </script>        <style type="text/css">        </style>    </head>    <body>        <script type="text/javascript">            function gotoProductWebPage() {                var answer = document.getElementById("answer").value;                if (answer == 8) {                    nowbaby();                    return true;                }                else {                    alert("Check your Math");                    return false;                }            }        </script>        <script type="text/javascript">            var myurl = "http://google.co.uk";            function nowbaby() {                var weblink = document.getElementById('link');                if (weblink.value !== "")                {                    myurl = weblink.value;                }                window.open(myurl, false);            }        </script>        <form method="get" name = "myname" onsubmit="gotoProductWebPage()" > ​            <p style='color:blue;text-align:center' >                Enter answer to prove you are human.A = 3, B = 5, A + B = <input type="number" id="answer" name="answer" /> <br />            </p>            <p style="text-align: center;" >                <input type="url" name="link" id="link">                <input type = "submit" name = "Submit" value = "Submit" />            </p>​        </form>    </body></html>
Link to comment
Share on other sites

Okay, here is what I have:

 

 <script type="text/javascript">            function gotoProductWebPage() {                 var answer = document.getElementById("answer").value;                if (answer == 8) {                     window.location.href = "myurl";                    return true;                }                else {                    alert("Check your Math");                    return false;                }            }        </script>​called from<form method="get" name="ProveHuman" onsubmit="gotoProductWebPage()">​ html<p style='color:blue;text-align:center' >                Enter answer to prove you are human.A = 3, B = 5, A + B = <input type="number" id="answer" name="answer" /> <br />            </p>            <p style="text-align: center;" >                              <input type = "submit" name = "Submit" value = "Submit" /></p>​

Thanks for looking.

Link to comment
Share on other sites

If this is ref to url variblewindow.location.href = "myurl";Then it should be window.location.href = myurl;Normally when you use return false and return true you expect onsubmit to beonsubmit="return gotoProductWebPage()";What you could do using this method is change action to the url you wish to go to<form method="get" id="myform" name="ProveHuman" onsubmit="return gotoProductWebPage()" action="#">​Then use document.getElementById("myform").action=myurl; // if referencing variableOrdocument.getElementById("myform").action="http://google.co.uk"; // using urlNote: Sorry! premature posting half way producing example using mobile device, fingers to big :-)

Edited by dsonesuk
  • Like 1
Link to comment
Share on other sites

Is that the actual code you're using? Are you trying to set the URL to the text "myurl" or are you replacing that value when you post it here? We can only comment on the code you're showing us, if you change things when we post then we're going to comment on the changed code, not the code you're actually using. I don't see where your code is trying to create a variable called myurl, so I doubt you're trying to use a variable for that.Also, again, keep an eye on your developer console for error messages from Javascript.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...