How to open a new script in the same tab [duplicate] - javascript

This question already has answers here:
window.open(url) not opening new web page in the same tab
(5 answers)
Closed 6 years ago.
I have the code
window.open("http://localhost/xyz.php")
This opens the PHP file on a new tab, although the same window. I want to open this on the same tab. How do I do it.
I have tried window.open("http://localhost/xyz.php","_self") and window.open("http://localhost/xyz.php";"_self") but that does not work. I have also tried window.location.hrefbut nothing seems to work. Is there any other way?

Just use window.location object:
window.location.href = 'http://localhost/xyz.php';
Changing href property, changes the current page address to the one you give as a parameter...
Note: you could also use window.location = 'http://localhost/xyz.php';, as suggested in comment, which is a bit shorter; though, changing location.href is a bit more standard, being implemented since JavaScript 1.0, so it's available in all browsers...

try this
window.location.replace("http://localhost/xyz.php");

Please refer to Open a URL in a new tab (and not a new window) using JavaScript .
It is a decision of the user, not the programmer, if a new page is opened in a new tab or a new window.

Try use this in your js:
location.href = "http://localhost/xyz.php";

Related

How do I open and type in another Tab Using only Javascript [duplicate]

This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed 4 years ago.
I need to open a URL, then type there, and I was wondering if you could do that in javaScript, how, and if not, are there any other languages I can do that in. Remember: No HTML Or CSS and I use Opera.
Thanks, - Beginning Coder
Elaborating, I am trying to open my email or Gmail ( I can do that part) and then type in who it is to, etc. Please Help. + Thanks for the feedback and tips though
The first part of your question (opening a new tab) is a duplicate of this question:
StackOverflow
I'm not entirely sure what you mean by "write," but I answered what I think you're asking.
As for writing into a new window/tab with JS, you can do that 2 ways:
Open a url on your website in a new window/tab
Make a new window and use document.write()
Tutorial: W3 Schools
Now that I know what you want to do specifically, you can do this:
window.open("https://mail.google.com/mail/u/0/#inbox?compose=new");
to open gmail in a new window/tab. You cannot write into any elements in that window (the subject line, for example) with JS because the same-origin policy does not allow it (you do not own google.com). Thus, you cannot accomplish what you want to do unless you are writing into a new window on your own website or file directory.
This StackOverflow post tells you how to set up a mailto that can auto set the subject and body. It's not exactly what you want, but maybe you could adapt it.
To open a new tab through JavaScript you can use window.open("link").
And to type in you could make a form in HTML and get the value from that form: document.getElementById("form id from html").value and store it in a variable so you can use it as link in windows.open;

how to add target blank to an href.location hyperlink [duplicate]

This question already has answers here:
Open a URL in a new tab (and not a new window)
(33 answers)
Closed 5 years ago.
I'm trying to get a link in javascript to open a url in a new tab. I've found a number of posts for target="blank" using attribute and a couple other ways but can't seem to get it to work. Basically, if v_virt = "invoices" I just need the url to open in a new tab. Does anyone know the proper syntax?
if(v_virt=="invoices"){
location.href=('https://www.example.com/invoices/invoice?ProjectID=[#field:ProjectID]&InvoiceID=[#field:InvoiceID]', '_blank');
}
You have to use window.open() rather than location.href.
location.href changes the URL of the current page;
window.open() opens a new pop-up window navigated to the specified page.
For example:
window.open('https://www.example.com/invoices/invoice?ProjectID=[#field:ProjectID]&InvoiceID=[#field:InvoiceID]');

How can I refresh a tab from another using Javascript

I'm looking to achieve something like Wordpress does when you create a new post. It allows you to preview your post by opening a new tab. If, after that, you edit the post and preview it again, rather than opening another tab, it refreshes the previously opened tab (provided it is still open).
From some research it seems I can open a new window and give it a name to identify it, like this:
window.open(url,"foobar");
But, how can I later refresh this tab? location.reload() does not seem to take a name as an argument.
If I remember correctly this is the way to do it:
var win = window.open(url, "foobar");
win.location.reload();
I think you're looking for the property window.opener. See http://www.w3schools.com/jsref/prop_win_opener.asp
You can do this by "storage" event. for more details follow
Communication between tabs or windows
https://developer.mozilla.org/en-US/docs/Web/Events/storage

Javascript: How do I change the URL in the address bar without refreshing the page? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I, with JavaScript, change the URL in the browser without loading the new page?
I noticed that web apps like GMail and GrooveShark can change the URL in the address bar of my browser without refreshing the page. Ajax is obviously used to change the content. How can I change the URL?
Gmail and Grooveshark only change the hash, this is done by changing:
location.hash = 'blah'
If you target HTML5 enabled browsers, you can use window.history.pushState and window.history.popState, see http://spoiledmilk.dk/blog/html5-changing-the-browser-url-without-refreshing-page
You may want to look into HTML5 push state. I know iQuery Mobile is using this feature to modify the location URL without triggering a page load and it might be the right solution for you.
https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history
Here is an example of how it works:
window.history.pushState(data, "Title", "/new-url");

Opening a new tab in JS

Is it possible to open a new tab in JS?? I tried googling it but most answers were answered before about years that it was not possible then, so is it possible now?!
Thank in advance :))
another question if possible, can we open to separate Urls using the same tag? I mean to open 2 diff tabs when clicking on one hyperlink
Yeah. You could do this in Javascript using window.open('target.html','mywindow','width=400,height=200') added to the onclick event.
See this for more info.
Opening a new tab in modern browsers is the same as opening a link in a new window by setting the target attribute. With HTML you do this by:
Click me
(See HTML a target attribute)
With javascript you do the same by window.open(url, '_blank'...) but remember that most browsers will block this unless it is done on the onClick event, so opening a new tab automatically after some timer has gone off for example is not a good idea.
You can also use link text if it's a link that should open in a new tab.
If you wanted to open both pages at the same time you could always combine both of the techniques mentioned above.
Double Link
This will open the pages thispage.html and thatpage.html simultaneously.
dont forget to add the title attribute to your link as opening new windows/tabs without warning users is generally frowned upon. Something like;
title="Clicking this link will open two new tabs"
should keep the standardistas off your back.
Also, you may want to separate your onclick event from your html as again munging them all together really isnt best practice. If you are using jquery then assign the onclick event by inserting a small piece of JavaScript at the top of your page as such;
$(function(){
$('#the-link-id').click(function(){
window.open('thatpage.html');
});
);

Categories

Resources