Launch JavaScript and new page at same time - javascript

When a user clicks on a hyperlink I would like to launch a JavaScript function and also launch a page in a new window. My preference if possible is to use HTML for the new page in case the person lacks JavaScript as fallback.
The following code launches page but not JavaScript.
click
Can someone recommend correct way to so this. Thx

Related

Continue JavaScript after open new Tab via Button Click

I'm quiet new to JavaScript and need your help concerning the following problem:
I've written a JavaScript to subscribe automatically to a gym class every monday at 7 a.m..
I have to click a button that's opening a new tab where I have to fill my personal data. The URL of the new tab is variable so I cant do it via
window.open(URL,"_self")
Is there a way to continue the script in the new tab or open the website in the same tab?
Thanks!
Because of the objections mentioned, that doesn't sound feasible to me - at least in this form. If you want to run a script on the external website, then simply use a browser solution such as:
Tampermonkey (https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo?hl=en)
Greasemonkey (https://addons.mozilla.org/de/firefox/addon/greasemonkey/)
There you can install your script and have it automatically fill out your registration.
If you want to automate this completely without doing anything on your part, then you will not be able to avoid an (external server-side) solution such as a NodeJS application with puppeteer (https://github.com/puppeteer/puppeteer) running every X days.

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"/>

window.open() does not open when clicked more than once

In my JSP application, I have a javascript function that opens a new window. The reportURL is a call to an asp application that produces a pdf file. So, if you call that asp page, it will produce pdf data.
The javascript that I have is:
function openReport(id)
{
window.open('<%=reportUrl%>&id='+id,
'_blank',
'width=900 height=900 scrollbars=yes status=yes'
);
}
Now my application runs fine, when the report link is clicked, it opens a new window and I can view my pdf. However, when I close the pdf report window and click the link again to generate report, it opens a new window (without the pdf). This new window does not look like a blank window as the background is not white, rather it looks like the pdf plugin for the browser recognizes it is pdf as I can see grey background but I cannot view the document.
Can anyone please let me know where is the problem?
My guess is its a caching issue of some kind. Whether its caching the window itself, or the results of the URL I can't tell without investigating it. Two things I would try, naming the window with a random name that changes every time you click it so instead of '_blank' use (new Date()).toString() (or similar I haven't tested it). The second thing to try is to ensure that the URL is unique every time to avoid caching there, so choose a querystring variable thats not used by the reporting system and change that every time its clicked e.g. + 'version='+(new Date()).toString().
This jquery popup window code works if you click it twice, so be helpful in determining what works and what doesn't http://swip.codylindley.com/popupWindowDemo.html
In fact you can conduct your own test, change the URL in your popup to a regular website and see how it works, if it works normally you know its the content not the window.
Hopefully that gives a starting point - unfortunately its hard to give a exact answer without being able to reproduce the problem.

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).

Is it possible to copy, paste, open new window in 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.

Categories

Resources