Javascript/Html.. push button goes into currect tab not new one - javascript

I'm trying to make a random link picker - When I press the button, the link goes in the current tab. I'm trying to make it so it will go into a new one.
<script type="text/javascript">
function randomlinks(){
var myrandom=Math.round(Math.random()*2)
var links=new Array()
links[0]="link1.com"
links[1]="link2.com"
links[2]="link3.com"
window.location=links[myrandom]
}
</script>
<form>
<input type="button" value="Random Episode!" onClick="randomlinks()">
</form>

Change it from:
//This sets the location of the current window, which you don't want
window.location = links[myrandom];
To this:
//This says to open a new tab or window, based on the user browser settings, to the url
window.open(links[myrandom]);

Just replace the line
window.location=links[myrandom]
with
window.open(links[myRandom], '_blank');

The target attribute specifies where to open the linked document.
You also have to check your browser settings first. If they are set to new window you can click your whole lifetime and it will still be opened in a new window instead of a new tab.
These are the right attributes.
The target attribute can have one of the following values:
_blank - Opens the linked document in a new window or tab
_self - Opens the linked document in the same window/tab as it was clicked (this is default)
_parent - Opens the linked document in the parent frame
_top - Opens the linked document in the full body of the window
framename - Opens the linked document in a named frame
So, this means that the default value (without the target attribute)
window.location=links[myrandom]
is also working. Hope it helps that your scripts work lovely.

Related

Window.open issue in firefox and IE

I am opening a new window on clicking a hyper link.
Issue:
After minimizing the window, again if I click on hyper link, the same window should be opened(In chrome minimized window will open up). But this is not happening in firefox and IE. Can anyone please help.
<html>
<head></head>
<body>
<p>Visit our HTML tutorial</p>
</body>
</html>
window.open allows you to specify a unique identifier to your popup; this allows you to open many links always in the same popup window.
If you use different identifiers on different links, it should open multiple popup windows.
<p>
Visit our HTML tutorial
</p>
<p>
Visit our HTML tutorial
</p>
If the strWindowFeatures parameter is used and no size features are defined, then the new window dimensions will be the same as the dimensions of the most recently rendered window.
you might want to check this link
window.open web api for mozilla
The idea of Unique ID in the parameter's list simply doesn't work as suggested in another answer.
You need a function for to do what you need in IE and FF. The trick is to get a function to see if it has opened a window before and do nothing if it has.
<script>
var opened = false;
function openWindow(){
if (!opened) {
w = window.open('', 'test', 'width=1500, height=900');
w.location = "http://www.google.com";;
w.onload = function() {
w.onunload = function() {
opened = false;
};
};
opened = true;
}
}
</script>
I'm using the opened global variable to track this. We set the newly created window to set false to this variable when it closes. Now the function can decide if it should really open a new window. Please note the following points:
We use onLoad function of the new window to set onUnload. Because IE seems to replace whatever the event handlers set here soon after it loads the page.
You can see that we first open a blank window and then set the url of it. This is because IE returns nothing when opening a new window if it is from another domain.

How to set the title for the new browser tab?

I have a question about the new tab for the link.
Is there anyway I can set the browser tab title before user clicks a link? It seems like there is no way to debate the title for the new tab if the html contained in the new tab doesn't have title attribute. Am I right? How do I set the title?
//the href is dynamic so I can't set them one by one because I have 100+ html file here
<a href="test.html" target="_blank">open me<a>
As you have it, this is not possible because your links are just normal HTML links. When the new page opens in a new tab, the current page will not have any reference to it and so cannot change it in any way. You will need to open the page using javascript and set the title that way.
You can dynamically set this up in window onload to find all a tags and add a click event whihc opens the window and sets the title.
If you want different titles for each page, you can store this in a data- attribute in the a tag.
Note tho that this will only work with pages in the same domain (for security), and that it does not handle people right clicking and pressing "Open in New Window". Middle click in Windows does seem to work however.
HTML
open me
JavaScript
window.addEventListener("load", function() {
// does the actual opening
function openWindow(event) {
event = event || window.event;
// find the url and title to set
var href = this.getAttribute("href");
var newTitle = this.getAttribute("data-title");
// or if you work the title out some other way...
// var newTitle = "Some constant string";
// open the window
var newWin = window.open(href, "_blank");
// add a load listener to the window so that the title gets changed on page load
newWin.addEventListener("load", function() {
newWin.document.title = newTitle;
});
// stop the default `a` link or you will get 2 new windows!
event.returnValue = false;
}
// find all a tags opening in a new window
var links = document.querySelectorAll("a[target=_blank][data-title]");
// or this if you don't want to store custom titles with each link
//var links = document.querySelectorAll("a[target=_blank]");
// add a click event for each so we can do our own thing
for(var i = 0; i < links.length; i++) {
links[i].addEventListener("click", openWindow.bind(links[i]));
}
});
Sample JsFiddle
You can pass the title with hash and get it on another page, if this another page is yours and you can modify its code.
1st page:
...
<a href="test.html#the_title_you_want" target="_blank">open me<a>
...
2nd page - modify the body opening tag like this:
<body onload="document.title=window.location.hash.replace('#','');">
If the page you are linking to isn't yours, you can use window.open method:
open me
I have not seen addEventListener work reliably, especially when opening a new page using javascript. The best way to change the tab title and have it work reliably is to set a timeout until the page loads. You may have to play with the timeout value, but it works.
var newWindow = window.open(url, '_blank');
setTimeout(function () {
newWindow.document.title = "My Tab Name";
}, 100);
You have two options. Using pure HTML, you can let the user open up links, then later on change the title. Or you can change the title with inline JavaScript. Here's how you do both:
Method 1
Change your links by assigning a target attribute, and then later on use that window name to control the document. For instance in your links it would be: <a href="whatever" target="theNewWindow">. Whenever you want to change the title for this page, you'd use JavaScript as such: window.open("", "theNewWindow").document.title = "New Page Title!"; The problem with this method however is that all links with that target/window name will open in that same window. In addition, after the first time the link is clicked, your browser won't automatically switch to the new tab/window.
Method 2
Change your links by assigning an onclick attribute, which would open the link manually and change the title of the page immediately. Basically it would come down to look like: <a href="whatever" onclick="var w=window.open(this.href, '_blank'); (w.onload=function(){w.document.title='New Page Title!';})(); return false;">. This opens the window based on the href attribute, immediately changes the title, and sets the window to change the title to that when it finishes loading (just in case there really was a title tag).
The problem with both of these methods (as mentioned by others) is your html files have to be on the same domain.
The simplest way is a follows:
var winTab = window.open("", "_blank")
//Open URL by writing iframe with given URL
winTab.document.write("write iframe with your url in src here")
//Set Title for the new tab
winTab.document.title = "Form Title"
You could make your own Page 2 that opens up the other pages (the ones you can't edit), in a frameset. You can then either change the title dynamically when loading your page 2, or as others have suggested if you use window.open you can control the title from the parent page.
If you are in page 1, and opening page 2 in a new tab, you can't set title for page 2 from page 1.
If you have access to page 2 then it's possible, otherwise not.

How to refresh another page using javascript without opening the same page in a new tab

Is it possible to refresh a page from another page using Javascript or JQuery without opening the same page in a new tab.
JS:
var newtab = window.open('http://localhost:8081/app/home');
newtab.document.location.reload(true);
I tried the above, but here, it will open a new tab, with the same page, which is already opened in the browser.
Please suggest a method.
I got the idea from a previous Question , here they used window Object Reference to reload the popup window, but for me it wont work, because, the parent window and child window runs in 2 different ports. So using the same trick, what i did is :
HTML:
<a onclick="openNewTab()">app2</a>
<a onclick="refreshExistingTab()">Refresh</a>
JS:
<script>
var childWindow = "";
var newTabUrl="http://localhost:8081/app/home";
function openNewTab(){
childWindow = window.open(newTabUrl);
}
function refreshExistingTab(){
childWindow.location.href=newTabUrl;
}
</script>
refreshExistingTab() this instend of refreshExistingTab
take a look at https://developer.mozilla.org/en-US/docs/Web/API/Window.open
basically if you do window.open and specify a window name it will overwrite that window with the url you provided.
so if you open the page each time with same window name, it should overwrite it each time you do it again from that other page.

How do identify whether the window opened is a pop up or a tab?

I have been facing a problem.I am able to open a window using window.open method.If I specify the height and width of the window,it opens as a pop up window.If no parameters is given for height or width,then it opens in a new tab.
Is there any property through which I can determine window opened was a pop up or a new tab?
Thank you
Malcolm X
Edit: I have been looking into this a little further.
Seems like there is no different "type" on these windows, simply different options.
A way I found to check if it was a tab or window is to check window.menubar.visible.
For the tab, which is a full and normal window it is true, and for the pop-up the menu is hidden and therefore false. Same applies to window.toolbar.visible.
Works in FF and Chrome at least. Unfortunately not in IE. (Testing done in IE8, which is the version I have installed. For testing of course..)
Example:
if(window.menubar.visible) {
//Tab
} else {
//"Child" Window
}
Found this thread: Internet Explorer 8 JS Error: 'window.toolbar.visible' is null or not an object
If you specify width and height, it means that you also have to specify the name parameter. This can be used in the same way target in an a tag is used, and defaults to _blank.
If you do not specify width and height I assume you also don't specify name and therefore it is opened with name=_blank, which means a new Tab.
If you specify width and height, are you setting a custom name? Doing so results in a child window. If you specify a name, or empty string as name, I suggest you try name:_blank if you want it to be a new tab.
If the window was opened with a name, you can always the window.parent from the child window. If you open with _blank I am not sure if you can get the window.parent
w3schools Window Open
I'm not quite sure what you mean in your question but from what I understand, you might want to use the HTML target attribute:
_blank Opens the linked document in a new window or tab
_self Opens the linked document in the same frame as it was clicked (this is default)
_parent Opens the linked document in the parent frame
_top Opens the linked document in the full body of the window
framename Opens the linked document in a named frame
Source: http://www.w3schools.com/tags/att_a_target.asp
You can detect that using onblur, by checking whether the focus is missed or not
<html>
<head>
<script>
function newTab() {
document.getElementById("demo").innerHTML = "New tab opened!<br><br>refesh this page to recheck ";
}
window.onblur = newTab;
</script>
</head>
<body>
<div id="demo">
Open a new tab and then check this page
</div>
</body>
</html>

Open link in new tab in firefox-extension

I have developed a webapp to use it as Firefox extension. In Firefox I include it with an iframe like this
<iframe src="http://mywebapp.com" flex="2" id="browserTable" name="table_frame"/>
Now I want to have some outgoing links in my app. If I just use normal link markup like
Contact
the link is opened in the iframe that is small in space since it is in the sidebar. Is there any way to open it in a new tab in the main browser window?
The target attribute allows you to specify which window to open a link in. You have these special keywords you can place in the attribute:
_blank - new window
_self - same window (default)
_parent - the window which opened the current window, or the parent frame in a frameset
_top - overload the entire page, usually used in a frame context
"string" - in the window with an id of "string", or a new window if "string" is not the id of a current window
So, here is your HTML:
Contact
EDIT Did some research after our discussion in comments, and found this snippet:
var myUrl = "http://mesh.typepad.com";
var tBrowser = top.document.getElementById("content");
var tab = tBrowser.addTab(myUrl);
// use this line to focus the new tab, otherwise it will open in background
tBrowser.selectedTab = tab;
Source: http://mesh.typepad.com/blog/2004/11/creating_a_new_.html
Let me know if that works out... curious myself, but my current FF environment is not one in which I can easily experiment with extension dev, and I don't want to change things to try.

Categories