Is it possible to copy, paste, open new window in javascript? - javascript

At the moment to get to an external link from our intranet, we have to copy the link, and paste the link into a new window. Is there any way we can achieve this in a single function in javascript?
Thanks
UPDATE:
When users login from outside the network, urls are changed. This is what we need to code for. I think I the following is applied twice, from server side and client side (this code is not editable):
s=s.replace(/location.assign\(([^;]*)\)/g,"location.assign(alter_url($1))")
s=s.replace(/location.replace\(([^;]*)\)/g,"location.replace(alter_url($1))")
if(s.match(/location\s*=\s*([^;]*)(;?)/)!=null&&s.match(/\.open\(.+,.+,.*location\s*=.+\)/)==null)
s=s.replace(/location\s*=\s*([^;]*)(;?)/g,"location=alter_url($1)$2")
s=s.replace(/location\.href\s*=\s*([^;]*)(;?)/g,"location.href=alter_url($1)$2")
s=s.replace(/window\.open\(([^,]*)(,.*)?\)/g,"window.open(alter_url($1)$2)")
s=s.replace(/\.src\s*=\s*([^;]*)(;?)/g,".src=alter_url($1)$2")
s=s.replace(/\.action\s*=\s*([^;]*)(;?)/g,".action=alter_url($1)$2")
s=s.replace(/\.innerHTML\s*=\s*([^;]*)(;?)/g,".innerHTML=alter_html($1)$2")
s=s.replace(/\.outerHTML\s*=\s*([^;]*)(;?)/g,".outerHTML=alter_html($1)$2")
Actually, the more I look at this, the more unrealistic it's becoming..

Right-clicking and choosing "Open in new window" doesn't work? Because if not, I'm not seeing a Javascript workaround working either. But:
You can open a new window with a specific URL (e.g., link) in Javascript easily enough:
window.open("http://stackoverflow.com");
And there are ways of getting the text that's selected in a page, to feed into that. All of which can be wrapped up into a bookmarklet so that the action becomes "select the text, click a link on the bookmark toolbar".
But if "Open in new window" doesn't work, I wouldn't expect that to work either.

You can use the window.open to open the links on a new window. In fact, you could use a bookmarklet to set the target to _blank on every link on the site, in case you can't access the application source code.
BTW, if you hold the Shift key when opening the link it will open on a new window.

Related

Using open.window() to open js program in a new window

I have a program that is written in CSS/HTML/JavaScript all inline, and opened directly from the HTML file (no web URL). Right now, everyone I work with has their browser set to open any links in a new tab in the same window, which makes the program useless, as it is form that is filled out manually, meant to be on a second monitor.
Right now, I have the window.resizeTo(); and the window.moveTo(); functions working properly IF the open in tabs is changed to open links in new window, but I cant walk around doing that to over 500 computers (no exaggeration on the amount), not to mention, any changes to the settings are reset afte the computers are reset.
When I try the window.open();, it just opens up a blank page in a new window after the .html file is opened, I need it to open itself in a new window, and then resize/move after.
Use window.open(...) with the height and width parameters set. This will force the link to open in a new window:
Share Page
Demo
You can pass a URL to window.open() to open that URL.
You can pass the current URL using location.href.
You can also specify a size and location directly in the third parameter.
Opening a page on tab or a separate window is browser specify feature, and it cannot be controlled by a Web Application.
window.open() will open a popup window and can open any URL you specify when provided:
window.open('openthispage.html);
window.open(URL,name,specs,replace)
add the URL of the program and it should work

Redirect after clicking an external link and only open one tab/window

I have created a jsp for external clients of my application to use to link to the web client. They can specify which client they are in the URL so that when the link opens a window it is named and any other links from that client will open in the same window. Here's an example URL:
mysite.com/redirect.jsp?fromClient=mine&page=awesome
The problem is that when you click on this link, for example from an email digest, it opens the redirect.jsp page which uses window.open() to open the desired page. From the above link, the page that would open would be:
mysite.com/index.jsp?page=awesome
So then I have two pages open, when really I only want one. And since I'm not using javascript to open the first page (it's a link), I can't use window.close() to close it. I've tried working around using window.open('','_parent','') and window.open('','_self','') but neither works.
I know there has to be a better way to redirect than to open a second window from my jsp page, but I don't know what that better way is. Thoughts?
EDIT
Primary goal is for users of external clients to be able to click links in that client and have all the links take them to the same window/tab in a browser. I'm using window.open right now because I can parse the link and get the client name and then supply that in the window.open function.
EDIT 2
I always end up being too vague in these questions. Ok, here's the setup:
An email digest contains a link that looks like
mysite.com/redirect.jsp?fromClient=emailDigest&pageNum=4
Currently, when that link is clicked on, it opens a browser (duh) and then the redirect.jsp page parses that link to get the client name, in this case emailDigest. It then calls window.open('mysite.com/index.jsp?pageNum=4','emailDigest'). This creates a new window with the name emailDigest unless a window/tab with that name already exists, in which case it merely updates the URL in that window with the new one.
I want to either be able to close the original window (the one with the redirect.jsp address) or not have to open that initial window at all.
NOTE
As there are many different clients that want to be able to do this, I cannot guarantee that they will be creating the link with HTML so the target attribute is not the answer.
The correct answer is "No, it cannot be done". If you do not open a window with Javascript then you cannot close it with Javascript. I would always have to create at least 2 windows, which is not what we wanted.
I think you're looking for window.location instead of window.open.
Please use window.location.replace("http://mysite.com/index.jsp?page=awesome");
Reason :
replace(url):Replace the current document with the one at the provided
URL. The difference from the assign() method is that after using
replace() the current page will not be saved in session history,
meaning the user won't be able to use the Back button to navigate to
it.
As haynar says above, why do you open a window and not either:
<!-- content contains the refresh delay in seconds and the target url -->
<meta http-equiv="Refresh" content="1; URL="http://mysite.com/index.jsp?page=awesome">
or use javascript to do the same:
window.location.href = "http://mysite.com/index.jsp?page=awesome";
either works fine and will leave you with one window open.
To get the named window use a targeted link:
Mysite.com
This will automatically set the name property of the opened window.

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

Open url in new tab using javascript without click an anchor

i read a lot of solutions but all of them are clicking a url, and it works, but my client ask me to do is users opens his website, it automatically open a new tab with some special offers , so my question, is there any way to open a new tab without any user intervention? , maybe a jquery plugin?, i know the tabs rely on the web browser, but it have to be a way, a lot of web pages does it,but how?
Greetings
Without modifying the configuration of browser, the answer is no, only trusted event can open a new window (or tab)
You may ask if your client could change their browser's configuration to allow a popup window from an untrusted javascript program.
Write script in your page to open new page:
<script>
window.open("specialOffers.aspx");
</script>
and in the "specialOffers.aspx" page in the tag write:
<base target="_blank"/>

link running a javascript that open another page. how to make it do it in new tab if user uses middle click or right click menu

I have this link in my left navigation:
dashboard
That javascript opens a link based on the passed parameters.
All works fine, but I would like to be able to use the browser capabilities of opening the links in a tab (when user is using middle click or selects 'Open link in new tag' from right click menu). Though, this is not working for links handled with javascript code.
There are many reasons why this is not the default behaviour of the browser (e.g. javascript function might only do some validation and stay in the page ... browser can't know what the js might do or if a new window/dialog will result from that action so would make no sense to open new tag as a result of a middle click ...). But hopefully there is a workaround for the default behaviour.
Any idea how this could be done?
Cheers,
Stef.
Javascript links execute in context of the page where they are called. If you "open" the link in a new tab/window, the javascript code will be executed in the new window, i.e., empty, and will most probably fail.
A browser could try to add the feature you are asking for by cloning the page which contains the link, and executing the javascript code in the context of the cloned page. But this would most likely break some critical sites (imagine for example that your online banking site works with javascript, so when you open a link in a new tab/window, cloning the original window might lead to a duplicate transaction).

Categories

Resources