How to close html file javascript - javascript

I'm opening a new html file on button press using this:
window.location = "menu.html";
I want to go back to the index.html file in my "menu" activity after a button press.
I tried using
window.location = "index.html";
But it creates a new screen and if I click the return buton to go to my homepage it returns to the previous activities. Also
window.opener.location = '/redirect.html';
window.close();
Doesn't work. So how do I close the menu activity to go back to my main activity.
PS: should I use window.location or window.location.href to open new html file
Thanks in advance!

Try using window.open("menu.html", "menu"). In addition to opening the file you can pass in a second parameter to the .open method and the next time you want to open the menu.html file you can using window.open("menu.html", "menu") it will open the already opened file in the browser.
https://developer.mozilla.org/en-US/docs/Web/API/Window/open

I fixed it using
history.go(-1);
navigator.app.backHistory();
sugested in this answer:
Phonegap - navigator.app.backHistory() not working on HTML back button
Also the href on the end of window.location.href is used for links so you should use just window.location

Related

To open the already opened tab using window.open() function without reloading the already opened tab

I was writing OnClick javascript code to open the new web page.
Expectation:
I want my to open the new page if that page is not already openend or open the already existing page without reload if that page is already opened.
My code:
window.open(url,'dialers').focus();
This code always reloads the child page if already opened. Please help to achieve my ask.
You can store the window object into a variable and call the focus() method multiple times like this:
let newWindow = window.open(url,'dialers')
newWindow.focus()
doThing()
newWindow.focus()
doAnotherThing()
newWindow.focus()
HTML5 introduced the history.pushState(page, caption, replace_url) that should not reload your page. OR if you want to modify while page load you can do this.
var currentURL = www.getitsol.com;
window.location.replace(currentURL + "?data=" + data.result, "_self");

I tried to open html in a new tab and tried //window.open("home.html", '_self'); but it did not work

I want to open new HTML page in the same tab. I tried using window.open('home.html','_self');, but this does not work. However, if I use window.open('home.html');, it opens in a new window.
How can I open HTML in the same page? I have tried all of these but none of them worked:
//window.open("home.html", '_top');
//window.location.replace("home.html");
//window.open('home.html','_self');
//window.location.assign("home.html")
Try using the window.location.href function.
E.g. window.location.href = "home.html";
I'm not sure you want to use this in HTML or JQuery...whatever:
HTML
Home
Jquery
location.href = "home.html";

How do you create a link which when clicked takes you to a page and also opens a new tab going to another url?

I want to have a link which when clicked preforms two actions:
redirects the current tab of the browser to url A.
opens a new tab directing the browser to url B
How can I do this? Is there HTML for this? Should I use javascript?
You would need to use javascript to achieve this, something along the lines of this:
$('#foo').click(function(e) {
e.preventDefault(); // stop the normal link behaviour so you can open a new window
window.open('http://foo.com/new-page'); // open new tab
window.location.assign($(this).prop('href')); // go to new page in current tab
});
Without jQuery
link // add onclick event
<script>
function f(){
document.location='/a.html'; // open in same tab
window.open('/b.html','_blank'); // open new tab
}
</script>

Javascript: Clicking Link to Download pdf

I am working on a JS program which should open a webpage www.mysite.com & click on a link inside that webpage to download a pdf.
The link to click looks like this:
<a onclick="download();return false;" href="#noWhere">Click to Download</a>
Ordinarily, manually clicking the link, calls the following function to download the pdf:
function download() {
document.forms[0].action = path + "/xxW04_sv_0140Action.do";
document.forms[0].target = "_self";
document.forms[0].submit();
}
My code is simplified javascript code to open the page & click on the "Click to Download" button is this:
<script>
var linkname = "http://www.mysite.com";
var windowname = "window_1"
// Opens a new window
var myWindow = window.open(linkname, windowname ,"width=400,height=600");
//should open a link to download pdf
myWindow.document.getElementById('href = \"#noWhere\"').click();
</script>
So far I can open the webpage "mysite.com" in a seperate window using but for some reason no button clicking is happening and certainly no pdf is downloaded.
Of course if I manually click the "Click to Download" button it downloads.
Can anyone tell me what i'm doing wrong? Why I cannot simulate a click with the above js code?
Or possibly give me some things to try. Any help much appreciated and Than you.
UPDATE:
From the initial answers below, possibly this method is doomed for failure! Can anyone suggest a better way I could be downloading these pdfs?
You'd better use:
<a href="http://www.mysite.com/mypdf.pdf">
This should download that pdf file.
It won't work. The same-origin policy will prevent you from accessing the content of any pages loaded from another domain.
Also, as #kamilkp pointed out, you have to provide the getElementById() function with an id value. You can't just plug any old stuff in there and expect it to work.
Another problem is your reliance on clicks for this to work. What about users that use the tab key to select links and then press Enter to follow the link?

Close child window and open link in a framed page

Trying to open a link in a framed page and close the child window. The link bellow is in a child window and when I click it opens the link in the framed page, but did not close the child window
<a target="Resultado" href="?Tela=1"
onClick="javascript:return confirm('TryMe');window.close();">
I have used a code like this to close the window... but couldn't get it to work with the above code.
<a href="javascript:window.opener='Resultado';window.close();">
Try this:
<a target="Resultado" href="?Tela=1" onclick="clickHandler(event, this);">Link</a>
And declare this:
function clickHandler(e, el) {
var choice = confirm('TryMe');
if (!choice) {
e.preventDefault();
}
window.close();
}
I wasn't sure of your original use of window.close() since it came after the return and would never execute, so it's up to you to move it to where you want.
Create another webpage on your webserver & use that as the Custom URL thankyou page for your form.
In that new webpage have just one line of code.
<body onload="javascript:window.opener.childClosed();window.close();">
--
That code will call a javascript function in the parent window and then close the child popup; you can then use that javascript function to do a redirect on your parent page.
I am not to sure where the URL of your 'Thank you for requesting...' page is, but here is some javascript to redirect to google on your parent page after the form is submitted.
function childClosed() {
window.location = "http://www.google.com/"
}
You can put that script before your closing <body> tag.
--
Hopefully I have understood your query OK, let me know if you have any questions or need any clarification on this possible solution.
-
I have made a very basic clone of your webpage & form here if you want to test out the functionality, 'Factoring Question?' is the link that contains this code.

Categories