I manage an ASP.NET site where they want to open multiple reports at the click of a button (preferably each in its own tab). The reports are saved as URLs for now, so I just need to open multiple windows with those urls.
I am attempting to do so using javascript (namely window.open). Here is an example of what I am doing (though I removed the actual urls):
<html>
<body>
<button onclick="openLinks();return false;">Open both links</button>
<script type="text/javascript">
function openLinks(){
var FirstWindow = window.open('');
FirstWindow.location = 'https://myssrsReportURL/rs:Format=EXCEL';
window.open('https://myssrsReportURL/rs:Format=EXCEL');
}
</script>
</body>
</html>
The first 2 lines of the js method above has the advantage of forcing the url to open in a new tab versus a new window. However, in the example above, when I set the location after opening a blank window, I receive the file download error IE "blocked this site from downloading":
Where as the second url opens (in a new window) without issue (I get a file download prompt).
Why does the second method work (I receive a file download prompt) while the first doesn't (I receive an IE security warning) if they are opening the same url? Is there some way around this using javascript (or jQuery)?
The customer is requiring that a single button click open all these reports. I can not zip them all up in one request because the reports are generated by SSRS upon a get request to a specific url (the ones I am attempting to open in new tabs).
Note, the question is not, "how do I open multiple tabs" it's why does the behavior discrepancy exist between window.open('') versus window.open('url')
There is no JavaScript magic trick to trick browsers into opening multiple tabs. And if there is...it will be stopped as it is a security vulnerability (download/tabs spam denial of service).
There is a better solution, which works on every browser. You can even choose between JavaScript and ASP.NET.
Create multiple iframe elements and show one at a time by using on-page tabs/links/buttons (simple onclick action to reveal one iframe and hide the rest). For downloads, this method has the advantage of not losing the current page if a server side script fails (so instead of 500 Internal Error or blank page the user remains on the current page).
For multiple downloads, why not zip the files on the server to have a single download prompt? It makes sense to not annoy users with many many download prompts.
Related
I'm working on small automation and encountered one problem. At first attempt I wanted to send CORS request from one web to another but it is restricted on the domain I'm automating (redirect is not allowed). In this case I have to open the origin page first and then send the request. I'm gonna open new window window.open and send the request from there (Checked and it works). I'd like to make it as simple as possible so to do it I need to control the new tab from current tab - Force it to run some lines of code. Is it even possible in modern js ?
It should be, you'd utilize something along the lines like:
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
Control gets finicky but this should help.
This is a simplified example:
I use two online dictionaries, but each one has only a set of features I need. However it's a pain to click through both and type in the words in two places.
I would like to write my own javascript webpage, that loads elements from the two sites, i.e. the input field for the word, and then displays the results from the two sites on the same page.
My script would need to read elements from one page, and use that data to
manipulate the other page.
When I tried researching this I encountered the same origin policy, but apparently chrome there's a workaround. My question is is this possible via browsers and javascript or would I have to look at a completely different technology like Python and webscraping.
If it's just to consult the two websites for the same term at the same time, you could have your webpage with two iframes and set their src attributes like the following:
$("#search").click(function () {
var term = $("#term").val();
$("#dictionary").attr("src", "http://www.dictionary.com/browse/" + term);
$("#free").attr("src", "https://www.thefreedictionary.com/" + term);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="term" type="text" placeholder="search dictionaries">
<input id="search" type="button" value="Search"><br>
<iframe id="dictionary" src="about:blank" style="width:45vw;height:500px;"></iframe>
<iframe id="free" src="about:blank" style="width:45vw;height:500px"></iframe>
The URL of the website is their API. Just look at the address bar after you've done a search on their site to find out how to construct it yourself.
Bonus
I myself use different websites like this, but via custom commands to automatically open multiple tabs in the browser (which you can also do via Javascript).
I use commands on linux like the following (declared in .bashrc):
function d {
firefox "http://www.dictionary.com/browse/$1+$2+$3+$4+$5"&exit
}
which means, without firefox even being open, I can open a terminal (with a keyboard shortcut) and type d test to launch firefox and search a dictionary for test. If firefox is already running it'll be brought to the foreground and the website will open in a new tab.
In windows you can add batch files (*.bat) or command files (*.cmd) to do the same thing. In c:\windows you can create a file called d.cmd with:
start "" "http://www.dictionary.com/browse/%1+%2+%3+%4+%5"
then use the keyboard shortcut Win+R to open the run dialog and type d test.
The run dialog used to be in the Start Menu, they've probably hidden it on your version of windows.
Yes, you can do that with JS if the dictionaries have endpoints you can interact with. (Or at least that would be the easiest). And most of them do. You can often enough simply google dictionary name + API.
If you have access to that you can fire off 2 requests to fetch data which you can then display on your page once it is back.
I created an Asp.Net MVC Internet Aplication and in my Index view of the Home Controller I have this
This is the first line, before the script results.
<script type="text/javascript" src="~/Script/Teste.js"></script>
<br />
This line comes after the script.
In my Teste.js I have this:
document.write("Yes! I am now a JavaScript coder!");
But nothing happens. If I change the src attribute and put some random name src="aaaa", despite the fact "aaaa" doesnt exist, I get no error in runtime.
EDIT
Also, check your path again. The default MVC templates in VS create a folder called Scripts, not Script. ("~/Scripts/teste.js")
Per the comment below, this was not the root cause of the issue, but in other cases can easily bite new JavaScript developers.
Most likely, your document.write function is firing before the document is ready, leading to the appearance that nothing is happening. Try the following in your Teste.js file
window.onload = function ()
{
document.write("Yes! I am now a JavaScript coder!");
//or even better as a test
alert("This alert was called");
}
Check the source of your page as well, it could be the document is being written to, you just can't see it due to markup/page styling.
As for you second issue, there will be no 'Runtime Exception' thrown if you reference a non-existent file. If you are using tools like Firebug or Chrome's developer tools, you should see a request to http://siteDomain/Scripts/aaaa.js with a response of 404, not found.
You generally should avoid using document.write() unless you absolutely have to use it for some reason... I don't think I've ever come across such a situation, and write a lot of Javascript.
Try this:
1) Put this in your HTML:
<script src="/scripts/teste.js"></script>
2) Put this in your JS:
alert('Yes! I am now a JavaScript coder!');
3) Open Chrome since it makes it easy to look for external resources loading and open the Network tab in Developer Tools (click the menu button at top-right, Tools > Developer Tools, Network tab).
4) Run your project and copy/paste the URL in the browser that comes up into this Chrome window, and hit enter.
When your page loads one of 2 things will happen:
A) You'll get the alert box you wanted or
B) You'll find out why it isn't loading because the Network tab will show the browser attempting to fetch teste.js and failing in some fashion, for example a 404, which would indicate you've got a typo in the path, or the script isn't where you thought it was, etc.
Put the following line at the very end of your document. There should not be anything after. Then try to load the page.
<script type="text/javascript" src="~/Script/Teste.js"></script>
Also, try pressing F12 once the page loads to see the source. Check if you script is there.
In MVC, the tilde is used to refer to the root URL of your application. However, it cannot normally parse this information. If you write:
<script src="~/Script/Teste.js"></script>
The lookup will fail, because the ~ means nothing special in HTML. If you're using Razor as your view engine (not ASPX), you need to wrap that call in Url.Content like so:
<script src="#Url.Content(~/Script/Teste.js)"></script>
Doing this will ensure a valid URL is provided to the browser.
With that in mind, you need to check that you have the file name and folder name both correct. You also need to ensure that the file is being deployed with your application. You can do this my opening the properties panel while the file is selected in the Solution Explorer and pressing F4.
I have a classic ASP web page that used to work... but the network guys have made a lot of changes including moving the app to winodws 2008 server running iis 7.5. We also upgraded to IE 9.
I'm getting a Permission denied error message when I try to click on the following link:
<a href=javascript:window.parent.ElementContent('SearchCriteria','OBJECT=321402.EV806','cmboSearchType','D',false)>
But other links like the following one work just fine:
<a href="javascript:ElementContent('SearchCriteria','OBJECT=321402.EV806', 'cmboSearchType','D',false)">
The difference is that the link that is failing is in an iframe. I noticed on other posts, it makes a difference whether or not the iframe content is coming from another domain.
In my case, it's not. But I am getting data from another server by doing the following...
set objhttp = Server.CreateObject("winhttp.winhttprequest.5.1")
objhttp.open "get", strURL
objhttp.send
and then i change the actual html that i get back ... add some hyperlinks etc. Then i save it to a file on my local server. (saved as *.html files)
Then when my page is loading, i look for the specific html file and load it into the iframe.
I know some group policy options in IE have changed... and i'm looking into those changes. but the fact that one javascript link works makes me wonder whether the problem lies somewhere else...???
any suggestions would be appreciated.
thanks.
You could try with Msxml2.ServerXMLHTTP instead of WinHttp.WinHttpRequest.
See differences between Msxml2.ServerXMLHTTP and WinHttp.WinHttpRequest? for the difference between Msxml2.ServerXMLHTTP.
On this exellent site about ASP you get plenty of codesamples on how to use Msxml2.ServerXMLHTTP which is the most recent of the two:
http://classicasp.aspfaq.com/general/how-do-i-read-the-contents-of-a-remote-web-page.html
About the IE9 issue: connect a pc with an older IE or another browser to test if the browser that is the culprit. Also in IE9 (or better in Firefox/Firebug) use the development tools (F12) and watch the console for errors while the contents of the iFrame load.
Your method to get dynamic pages is not efficient i'm afraid, ASP itself can do that and you could use eg a div instead of an iframe and replace the contents with what you get from the request. I will need to see more code to give better advice.
I need to create a batch file that will stop a process and then refresh a defined tab in internet explorer 7. Just need some help/pointers on the tab refresh part or if it's even possible... I don't want IE to open another tab, and another browser is not an option as the web based program is only compatible with IE. Any ideas? I've experimented with a VBS file with no luck and seeing how it's a web based program I cannot add Java Script to the page...
I know you said you tried VBScript, but it really is the most suitable solution for what you're trying to achieve. See "Hey, Scripting Guy! Blog: How Can I Tell if Any Internet Explorer Windows Are Open to a Particular Web Site?"
See the code:
For i = 0 to objShellWindows.Count - 1
Set objIE = objShellWindows.Item(i)
strURL = objIE.LocationURL
If InStr(strURL, "http://www.microsoft.com/technet/scriptcenter")Then
blnFound = True
End If
Next
Each iteration returns an instance of an open Internet Explorer window's WebBrowser Control. Instead of the blnFound = True try objIE.Refresh2().
You could frame the site, then refresh it from the outer frame with javascript on a timer. This may or may not suit your needs.
This is doable, but it's a little tricky and it requires a constraint: the tab you want to refresh has to have been opened by a Javascript call to window.open and it has to have a name. Let's call that name foo. Then you need to simply load another web page in that same browser session to execute the following Javascript:
window.open('http://other.site.url/etc', 'foo');
This means you need to both know the name of the frame and the target URL. But it's certainly doable.
Doing this from a batch file requires some scripting. In VBScript the code would be something like:
Dim browser
Set browser = CreateObject("SHDocVw.InternetExplorer")
browser.visible = True
browser.navigate("http://mysite.org/refresh.html")
Where refresh.html is the page containing the above Javascript followed by a call to window.close()
Assuming you have control over the web page too...
I'm suprised no one brought up the age old meta refresh.
Rather than do some goofy iframe/javascript magic, or some crazy IE COM object mambo, you could always write a meta refresh tag into your given a certain querystring is passed (or all the time, I don't know what your needs are)
Again, I'm not sure this suits your needs, but it is quick and pretty clean.
put this in your and it will refresh the page once every 60 seconds:
<meta http-equiv="refresh" content="60">